atcoder#AGC023B. [AGC023B] Find Symmetries

[AGC023B] Find Symmetries

Score : 500500 points

Problem Statement

Snuke has two boards, each divided into a grid with NN rows and NN columns. For both of these boards, the square at the ii-th row from the top and the jj-th column from the left is called Square (i,j)(i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j)(i,j) is Si,jS_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

  • First, choose two integers AA and BB ( 0A,B<N0 \leq A, B < N ).
  • Write one letter in each square on the second board. Specifically, write the letter written in Square (i+A,j+B)( i+A, j+B ) on the first board into Square (i,j)(i,j) on the second board. Here, the kk-th row is also represented as the (N+k)(N+k)-th row, and the kk-th column is also represented as the (N+k)(N+k)-th column.

After this operation, the second board is called a good board when, for every ii and jj ( 1i,jN1 \leq i, j \leq N ), the letter in Square (i,j)(i,j) and the letter in Square (j,i)(j,i) are equal.

Find the number of the ways to choose integers AA and BB ( 0A,B<N0 \leq A, B < N ) such that the second board is a good board.

Constraints

  • 1N3001 \leq N \leq 300
  • Si,jS_{i,j} ( 1i,jN1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

NN

S1,1S1,2..S1,NS_{1,1}S_{1,2}..S_{1,N}

S2,1S2,2..S2,NS_{2,1}S_{2,2}..S_{2,N}

::

SN,1SN,2..SN,NS_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers AA and BB ( 0A,B<N0 \leq A, B < N ) such that the second board is a good board.

2
ab
ca
2

For each pair of AA and BB, the second board will look as shown below:

The second board is a good board when (A,B)=(0,1)(A,B) = (0,1) or (A,B)=(1,0)(A,B) = (1,0), thus the answer is 22.

4
aaaa
aaaa
aaaa
aaaa
16

Every possible choice of AA and BB makes the second board good.

5
abcde
fghij
klmno
pqrst
uvwxy
0

No possible choice of AA and BB makes the second board good.