MaxSum (visit all columns)#5858

MaxSum (visit all columns)

2 seconds
512 megabytes
Easy

You have a rectangular table with rows and columns, where each cell contains an integer. You can traverse the table from top to bottom, starting from any cell in the top row. From each cell at position , you can move to one of the "lower neighboring" cells: (i+1, j-1), (i+1, j), or (i+1, j+1). However, if j=M, you cannot move to (i+1, j+1), and if j=1, you cannot move to (i+1, j-1). Your path must end in any cell of the bottom row.

Your task is to write a program that determines the maximum possible sum of the values of the cells traversed in a path that passes through each column at least once.

Input

The first line contains two integers, and , representing the number of rows and columns, respectively (2 \leq N \leq 1024, 2 \leq M \leq 768, N \geq M). Each of the following lines contains exactly space-separated integers, each not exceeding 10^6 in absolute value, representing the values in the table cells.

Output

Output a single integer, which is the maximum sum found among all paths that pass through every column at least once.

Examples

Example 1

Loading
Loading

Note

The correct answer is 28 = 15+5+2+6 because any path with a larger sum does not pass through all columns.

Please note that the input file size is large. In C++, it is advisable not to use cin with synchronization enabled, and in Java, it is advisable not to use Scanner, as this may prevent the program from reading the input data in time.