Max has found himself in Valentine's Land, a field represented as an grid with either cookies or boulders. Each cookie has a particular value and Max starts on the top-left corner.
In one second, Max can either move directly up, down, left, or right as long as he stays on the grid and does not move into a boulder. Additionally, if he chooses to do so, he can pick up the cookie corresponding to the cell he is currently at and instantly teleport back to the top-left corner. However, if he picked up a cookie before, then this cookie must have a strictly higher value than the last.
Max can spend at most seconds moving. What is the largest sum of cookies he can pick up?
Constraints
Input Specification
The first line contains three integers , , and .
The next lines each contains integers, which describes the grid. For each cell, if the value listed is nonnegative, then it is a cookie with the same value. Otherwise, it is a boulder. The top left corner will be a 0
.
Output Specification
One integer, the maximum total value of cookies Max can get.
Sample Input
3 4 7
0 0 -1 8
1 0 -1 0
0 0 0 0
Sample Output
8
Sample Input
3 4 4
0 0 -1 8
1 0 -1 0
0 0 0 0
Sample Output
1
Comments