B - Find Symmetries Editorial /

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

すぬけ君は、NN 列からなるマス目状に区切られた盤面を 2 つ持っています。 どちらの盤面についても、上から i 行目、左から j 列目のマスをマス (i,j) と呼ぶことにします。

1 つめの盤面には、すべてのマスに 1 つの英小文字が書かれています。 マス (i,j) に書かれている文字は S_{i,j} です。 2 つめの盤面には、まだ何も書かれていません。

すぬけ君は、次のような手順で 2 つめの盤面に文字を書き込みます。

  • まず、2 つの整数 A, B ( 0 \leq A, B < N ) を選ぶ。

  • すべてのマスに文字を書き込む。 具体的には、2 つめの盤面のマス (i,j) に、1 つめの盤面のマス ( i+A, j+B ) の文字を書き込む。 ここで、N+k 行目と表される行は k 行目を意味する。 同様に、N+k 列目と表される列は k 列目を意味する。

操作後、2 つめの盤面がよい盤面であるとは、すべての i, j ( 1 \leq i, j \leq N ) に対して、 マス (i,j) とマス (j,i) にかかれている文字が等しいことを意味します。

2 つめの盤面がよい盤面になるような整数 A, B ( 0 \leq A, B < N ) の選び方が何通りあるかを求めてください。

制約

  • 1 \leq N \leq 300
  • S_{i,j} ( 1 \leq i, j \leq N ) は英小文字

入力

入力は以下の形式で標準入力から与えられる。

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

出力

2 つめの盤面がよい盤面になるような整数 A, B ( 0 \leq A, B < N ) の選び方が何通りあるかを出力せよ。


入力例 1

2
ab
ca

出力例 1

2

A, B の組に対して、2 つめの盤面は図のようになります。 2 つめの盤面がよい盤面となっているのは (A,B) = (0,1)(A,B) = (1,0) のときです。 よって答えは 2 です。


入力例 2

4
aaaa
aaaa
aaaa
aaaa

出力例 2

16

どのように A, B を選んでも、2 つめの盤面はよい盤面になります。


入力例 3

5
abcde
fghij
klmno
pqrst
uvwxy

出力例 3

0

どのように A, B を選んでも、2 つめの盤面はよい盤面になりません。

Score : 500 points

Problem Statement

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

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

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

  • First, choose two integers A and B ( 0 \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 ) on the first board into Square (i,j) on the second board. Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

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

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

Constraints

  • 1 \leq N \leq 300
  • S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

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


Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

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


Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.


Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.