#1485. 方格取数 (Grid Numbers Picking)

方格取数 (Grid Numbers Picking)

Description

You are given an n×mn \times m grid having an integer in each cell of the grid. A little bear wants to walk from the top left corner to the bottom right corner of the grid. In each step, the bear can only move one cell up, down or right. It can't go to a cell that it has already visited earlier, and it can't go out of the boundary of the grid. The bear will calculate the sum of integers in all the squares it walks through. Find the maximum sum of integers it can obtain.

Input Format

The first line contains two integers nn and mm.

ii-th of the next nn lines contains mm integers, which represent the integers in the ii-th row of the grid.

Output Format

Print a single integer denoting the maximum sum of integers the bear can obtain.

3 4
1 -1 3 2
2 -1 4 -1
-2 2 -3 -1
9

Taking the first path in the image below gives a sum of 9, which is the maximum possible. The second path is invalid because the cell in row 2 and column 2 has been visited twice, which is not allowed. The third path is also invalid, because it does not end at the bottom right corner.

2 5
-1 -1 -3 -2 -7
-2 -1 -4 -1 -2
-10

Taking the path shown in the image below gives a sum of -10, which is the maximum possible. Therefore, the maximum value of the sum may also be negative.

Constraints

For 20%20\% of the data, n,m5n, m \le 5.
For 40%40\% of the data, n,m50n, m \le 50.
For 70%70\% of the data, n,m300n, m \le 300.
For 100%100\% of the data, 1n,m1031 \le n, m \le 10^3.

The absolute value of each integer in the grid doesn't exceed 10410^4.