#K64637. Maximum Boundary Sum
Maximum Boundary Sum
Maximum Boundary Sum
You are given a grid (matrix) of integers with ( m ) rows and ( n ) columns. Your task is to compute the sum of all the boundary elements of this grid. The boundary of the grid consists of the elements in the first row, the last row, and the first and last elements of each intermediate row. Note that if the grid has only one row or one column, then all the elements are considered as boundary elements.
For example, consider the grid below:
[
\begin{bmatrix}
1 & 2 & 3 \
4 & 5 & 6 \
7 & 8 & 9
\end{bmatrix}
]
The boundary elements are: 1, 2, 3, 4, 6, 7, 8, 9. Their sum is 40.
Write a program that reads the grid from standard input, calculates the sum of its boundary elements, and prints the result to standard output.
inputFormat
The first line of input contains two space-separated integers ( m ) and ( n ) representing the number of rows and columns in the grid. Each of the next ( m ) lines contains ( n ) space-separated integers, representing the grid elements.
outputFormat
Output a single integer — the sum of the boundary elements of the grid.## sample
3 3
1 2 3
4 5 6
7 8 9
40