Problems > Magic Square

Magic Square

Difficulty: 1200 2000 ms 128 MB

implementation

Is the N×N matrix a magic square — all rows, all columns and both diagonals sharing the same sum? (Values need not be distinct.)

Input: the first line has N and N; the next N lines each have N integers.

Output: YES or NO.

Constraints:

  • 1 ≤ N ≤ 100

Sample Tests

Input 1
3 3
2 7 6
9 5 1
4 3 8
Output 1
YES
Input 2
2 2
1 2
3 4
Output 2
NO

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.