Sum of Matrix Diagonals
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.
Example 1
3 1 2 3 4 5 6 7 8 9
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
2 10 20 30 40
100
N=2. Main diagonal: 10, 40. Anti-diagonal: 20, 30. Sum = 10+40+20+30 = 100. No center element overlap.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
3 1 2 3 4 5 6 7 8 9
2 10 20 30 40