#P7160. Maximum Ring Sum
Maximum Ring Sum
Maximum Ring Sum
You are given an (n \times m) grid where the cell in the (i)-th row and (j)-th column has a strange value (a_{i,j}). Due to external constraints, you can only examine a ring of the matrix. In particular, you must choose a submatrix whose top row is the first row of the entire grid and whose bottom row is the last row. The top and bottom borders (each of thickness 1) are fixed as these rows, while you are free to choose the left and right boundaries (with (1 \leq l < r \leq m)).
The ring is defined as the border of the submatrix spanning rows (1) to (n) and columns (l) to (r). Its sum is computed as follows:
[ \text{Sum} = \sum_{j=l}^{r} a_{1,j} + \sum_{j=l}^{r} a_{n,j} + \sum_{i=2}^{n-1} \Bigl(a_{i,l} + a_{i,r}\Bigr) ]
Your task is to determine the maximum possible ring sum over all valid choices of (l) and (r).
inputFormat
The first line contains two integers (n) and (m) (with (n \ge 2) and (m \ge 2)). Each of the following (n) lines contains (m) integers representing the grid values (a_{i,1}, a_{i,2}, \ldots, a_{i,m}).
outputFormat
Output a single integer, the maximum ring sum achievable.
sample
2 2
1 2
3 4
10