#K37227. Compute the Sum of Diagonals of a Square Matrix
Compute the Sum of Diagonals of a Square Matrix
Compute the Sum of Diagonals of a Square Matrix
You are given a square matrix of size \(n \times n\). Your task is to compute the sum of the primary (main) diagonal and the secondary (anti) diagonal. Note that if \(n\) is odd, the center element (which lies on both diagonals) will be counted twice.
For example, consider the matrix:
1 2 3 4 5 6 7 8 9
The primary diagonal is \(1, 5, 9\) and the secondary diagonal is \(3, 5, 7\). The sum is computed as \(1+5+9+3+5+7 = 30\).
You can assume that the input matrix is non-empty and square.
inputFormat
The input begins with an integer \(n\) (\(1 \leq n \leq 1000\)), which denotes the size of the matrix. The next \(n\) lines each contain \(n\) integers separated by spaces, representing the rows of the matrix.
outputFormat
Output a single integer: the sum of the primary and secondary diagonals of the matrix.
## sample3
1 2 3
4 5 6
7 8 9
30