#K85517. Matrix Spiral Sum
Matrix Spiral Sum
Matrix Spiral Sum
Given a 2D matrix of integers, your task is to compute the spiral sum of its elements. The spiral sum is obtained by traversing the matrix in a spiral order starting from the top-left corner, moving right along the top row, then down along the rightmost column, left along the bottom row, and up along the leftmost column, continuing this process until all elements are traversed.
If the matrix is empty (i.e. has zero rows or zero columns), the output should be 0. Even though the spiral order is used for traversal, note that the result is simply the sum of all elements in the matrix.
inputFormat
The first line of input contains two integers n and m which represent the number of rows and columns respectively.
Then follow n lines, each containing m space-separated integers representing the matrix elements.
outputFormat
Output a single integer denoting the sum of the matrix elements in spiral order.
## sample3 3
1 2 3
4 5 6
7 8 9
45