PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT00000054. Đường qua sân (Crossing the Schoolyard)

Bài tập này chưa được xếp vào lộ trình.

Đường qua sân (Crossing the Schoolyard)

Crossing the Schoolyard

Version: Phuoc Hung OJ Extended

Problem Statement

A schoolyard is divided into a grid of NN rows and MM columns. Cell (i,j)(i,j) contains an integer Ai,jA_{i,j}, which may be negative, zero, or positive. Rows are numbered from top to bottom, and columns from left to right, starting at 11.

A student starts at (1,1)(1,1) and must reach (N,M)(N,M). Each ordinary move may only be:

  • Right: from (i,j)(i,j) to (i,j+1)(i,j+1).
  • Down: from (i,j)(i,j) to (i+1,j)(i+1,j).

In addition, during the entire route, the student may make at most one diagonal down-right move, from (i,j)(i,j) to (i+1,j+1)(i+1,j+1). A move is valid only if its destination is inside the grid. The diagonal move is optional.

The route's score is the sum of values in the cells actually visited, including the starting and ending cells. When moving diagonally from (i,j)(i,j) to (i+1,j+1)(i+1,j+1), cells (i,j+1)(i,j+1) and (i+1,j)(i+1,j) are skipped by that move, so their values are not added. Negative values are counted normally.

Find the maximum possible score of a valid route from (1,1)(1,1) to (N,M)(N,M).

Input

  • The first line contains two positive integers N,MN,M: the number of rows and columns.
  • Each of the next NN lines contains exactly MM integers Ai,1,Ai,2,…,Ai,MA_{i,1},A_{i,2},\ldots,A_{i,M}, describing row ii from left to right.

Output

Print one integer: the maximum total score of any valid route from (1,1)(1,1) to (N,M)(N,M). The answer may be negative.

Subtasks

The following general constraints apply to all subtasks: 1≤N,M≤1031\le N,M\le 10^3; NM≤106NM\le 10^6; ∣Ai,j∣≤106|A_{i,j}|\le 10^6.

  • Subtask 1 (30%): NM≤100NM\le 100.
  • Subtask 2 (30%): N=1N=1 or M=1M=1.
  • Subtask 3 (40%): No additional restrictions beyond the general constraints.

Examples

Example 1

Input

3 3
2 -100 5
4 8 -100
3 -100 10

Output

24

Explanation

One maximum-scoring route is (1,1)→(2,1)→(2,2)→(3,3)(1,1)\to(2,1)\to(2,2)\to(3,3). The first three cells have values 2,4,82,4,8; the final move is diagonal to the cell valued 1010.

The two cells (2,3)(2,3) and (3,2)(3,2), both valued −100-100, are skipped by that diagonal move. The score is 2+4+8+10=242+4+8+10=24, so the answer is 2424.

Example 2

Input

2 3
1 2 3
4 5 6

Output

16

Explanation

A route with no diagonal move is (1,1)→(2,1)→(2,2)→(2,3)(1,1)\to(2,1)\to(2,2)\to(2,3), scoring 1+4+5+6=161+4+5+6=16. In this grid, no route with a diagonal move scores more than 1616 either. Therefore the answer is 1616.

Example 3

Input

1 1
-7

Output

-7

Explanation

The grid consists of one cell, (1,1)(1,1), whose value is −7-7. It is both the starting and ending cell, and the score is −7-7, not 00.