Sum of Matrix DiagonalsEasy

Sum of Matrix Diagonals

EasyPractice library · +10 XPmatrixtraversal

Given a square matrix of integers, compute the sum of all elements lying on its main diagonal and its anti-diagonal. If an element lies on both diagonals (the center element of an odd-sized matrix), it should be counted only once.

Input

The first line contains an integer N (1 <= N <= 1000), the dimension of the square matrix. The next N lines each contain N integers, representing the matrix elements.

Output

Print a single integer: the sum of elements on the main and anti-diagonals, with the center element counted only once.

Constraints

1 <= N <= 1000. Matrix elements are integers between -1000 and 1000.

Time limit 1 s (native code; slower languages get more)Memory 256 MB11 hidden tests

Example 1

Input
3
1 2 3
4 5 6
7 8 9
Output
25

N=3. Main diagonal: 1, 5, 9. Anti-diagonal: 3, 5, 7. Sum = 1+5+9 + 3+5+7 = 30. Center element 5 is counted once: 1+5+9+3+7 = 25.

Example 2

Input
2
10 20
30 40
Output
100

N=2. Main diagonal: 10, 40. Anti-diagonal: 20, 30. Sum = 10+40+20+30 = 100. No center element overlap.

Hints