#K82077. Magic Square Checker

    ID: 35895 Type: Default 1000ms 256MiB

Magic Square Checker

Magic Square Checker

You are given a square matrix (grid) of size (n \times n) which contains distinct integers ranging from 1 to (n^2). A magic square is a grid where the sum of all rows, all columns, and both main diagonals are equal to the magic constant (M = \frac{n(n^2+1)}{2}).

Determine if the given grid forms a magic square. If it does, output YES; otherwise, output NO.

inputFormat

The input is read from standard input (stdin). The first line contains an integer (n) indicating the size of the square grid. This is followed by (n) lines, each containing (n) space-separated integers representing the grid.

outputFormat

Output a single line to standard output (stdout) containing either YES if the grid is a magic square, or NO otherwise.## sample

3
8 1 6
3 5 7
4 9 2
YES