Snuke has two boards, each with a grid of N rows and N columns. The first board has lowercase English letters in each square, denoted as S(i,j). The second board is initially empty. Snuke will write letters on the second board by choosing two integers A and B (0 ≤ A, B < N) and writing the letter from the first board's square (i+A, j+B) into the corresponding square (i,j) on the second board. The second board is considered "good" if, for every i and j (1 ≤ i, j ≤ N), the letter in square (i,j) is equal to the letter in square (j,i). The task is to find the number of ways to choose integers A and B such that the second board is "good". The constraints are 1 ≤ N ≤ 300 and S(i,j) (1 ≤ i, j ≤ N) is a lowercase English letter. The input format consists of N followed by the letters in the grid, and the output is the number of ways to choose A and B. Examples of input and output are provided.

# Buggy version:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    S = [input().strip() for _ in range(N)]
    print(sum(N if all(S[(i - a) % N][j] != S[(j - a) % N][i] for i in range(N) for j in range(N)) else 0 for a in range(N)))


if __name__ == "__main__":
    main()


# Fixed version:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    S = [input().strip() for _ in range(N)]
    print(sum(N if all(S[(i - a) % N][j] == S[(j - a) % N][i] for i in range(N) for j in range(N)) else 0 for a in range(N)))


if __name__ == "__main__":
    main()

# Buggy version:
"""
不変なもの
- 移動した行/列内の順番
○ AとBの差によって、線対称(対角線上)のものが決まる
"""
n = int(input())
fs = []
for i in range(n):
    rs = list(input())
    fs.append(rs)
cnt = 0
for b in range(n):
    flg = True
    for i in range(n):
        for j in range(n):
            flg &= fs[i][j] == fs[i][(j+b)%n]
    cnt += flg
print(cnt*n)

# Fixed version:
"""
不変なもの
- 移動した行/列内の順番
○ AとBの差によって、線対称(対角線上)のものが決まる
"""
n = int(input())
fs = []
for i in range(n):
    rs = list(input())
    fs.append(rs)
cnt = 0
for a in range(n):
    flg = True
    tmp = []
    for r in fs:
        r_ = r[a:] + r[:a]
        tmp.append(r_)
    for i in range(n):
        for j in range(n):
            flg &= tmp[i][j] == tmp[j][i]
    
    cnt += flg
print(cnt*n)

# Buggy version:
I,R=input,range
n=int(I())
s=[I()for i in R(n)]
a=0
for i in R(n):
 if all(s[(i+j)%n][k]==s[(i+j)%n][j]for j in R(n) for k in R(i+1,n)):a+=n
print(a)

# Fixed version:
I,R=input,range
n=int(I())
s=[I()for i in R(n)]
a=0
for i in R(n):
 if all(s[(i+j)%n][k]==s[(i+k)%n][j]for j in R(n)for k in R(j+1,n)):a+=n
print(a)

# Buggy version:
def solve():
    N = int(input())
    S2 = [0]*N
    for i in range(N):
        S2[i] = input()
    ans = 0
    for k in range(N):
        cum = [[0]*(N+1) for _ in range(N+1)]
        for i in range(1,N+1):
            for j in range(1,N+1):
                cum[i][j] = cum[i-1][j]+cum[i][j-1]-cum[i-1][j-1]+(S2[i-1][j-1]==S2[(j-1+k)%N][(i-1-k)%N])
    for k in range(N):
        ans += (cum[-1][-1]==N*N)*N
    return ans
print(solve())

# Fixed version:
def solve():
    N = int(input())
    S2 = [0]*N
    for i in range(N):
        S2[i] = input()
    ans = 0
    for k in range(N):
        cum = [[0]*(N+1) for _ in range(N+1)]
        for i in range(1,N+1):
            for j in range(1,N+1):
                cum[i][j] = cum[i-1][j]+cum[i][j-1]-cum[i-1][j-1]+(S2[i-1][j-1]==S2[(j-1+k)%N][(i-1-k)%N])
                if i==N and j==N:
                    ans += (cum[i][j]==N*N)*N
    return ans
print(solve())

# Buggy version:
N = int(input())
grid=[[v for v in input()] for _ in range(N)]

ans=0
for a in range(N):
    for b in range(1):
        new_grid=[]
        new_grid.extend(grid[a:])
        new_grid.extend(grid[:a])
        for i in range(N):
            new_grid[i] = new_grid[i][b:]+new_grid[i][:b]
        print(new_grid)

        flag=True
        for i in range(N):
            for j in range(N):
                if new_grid[i][j]==new_grid[j][i]:
                    continue
                else:
                    flag=False
                    break
        if flag:
            ans+=1
print(ans)


# Fixed version:
N = int(input())
grid=[[v for v in input()] for _ in range(N)]

ans=0
for a in range(N):
    new_grid=[]
    new_grid.extend(grid[a:])
    new_grid.extend(grid[:a])

    flag=True
    for i in range(N):
        for j in range(N):
            if new_grid[i][j]==new_grid[j][i]:
                continue
            else:
                flag=False
                break
    if flag:
        ans+=1
print(ans*N)

# Buggy version:
n = int(input())
s = [list(input()) for i in range(h)]
s += s

def ok(array):
    for i in range(1,n):
        for j in range(i):
            if array[i][j] != array[j][i]:
                return False
    return True
ans = 0
for i in range(n-1):
    if ok(s[i:i+n]):
        ans += n
print(ans)


# Fixed version:
n = int(input())
s = [list(input()) for i in range(n)]
s += s

def ok(array):
    for i in range(1,n):
        for j in range(i):
            if array[i][j] != array[j][i]:
                return False
    return True
ans = 0
for i in range(n):
    if ok(s[i:i+n]):
        ans += n
print(ans)

# Buggy version:
n=int(input())
l=[]
for i in range(n):
    s=list(input())
    l.append(s+s)
ans=0
for i in range(n):
    f=0
    for h in range(n):
        for w in range(n):
            #0,0+i 1,1+i h,h+i
            if l[h][w+i]!=l[h-w][h+i]:
                f+=1
    if f==0:
        ans+=1
print(ans*n)

# Fixed version:
n=int(input())
l=[]
for i in range(n):
    s=list(input())
    l.append(s+s)
ans=0
for i in range(n):
    f=0
    for h in range(n):
        for w in range(n):
            #0,0+i 1,1+i h,h+i
            if l[h][w+i]!=l[w][h+i]:
                f+=1
    if f==0:
        ans+=1
print(ans*n)

# Buggy version:
# 解説AC
import numpy as np

def is_symmetric(a):
    """
    対象行列判定 O(N * N)
    """
    return np.array_equal(a, a.T)

def main():
    N = int(input())
    S = [[ord(s) - ord("a") for s in list(input())] for _ in range(N)]

    S = np.array(S)

    ans = 0
    for B in range(N):
        if is_symmetric(np.roll(S, B, axis=1)):
            ans += N

    print(ans)

# Fixed version:
# 解説AC
import numpy as np

def is_symmetric(a):
    """
    対象行列判定 O(N * N)
    """
    return np.array_equal(a, a.T)

def main():
    N = int(input())
    S = [[ord(s) - ord("a") for s in list(input())] for _ in range(N)]

    S = np.array(S)

    ans = 0
    for B in range(N):
        if is_symmetric(np.roll(S, B, axis=1)):
            ans += N

    print(ans)

if __name__ == "__main__":
    main()

# Buggy version:
N, *S = open('0').read().split()
N = int(N)
ans = 0
for p in range(N):
  flag = False
  for i in range(N):
    for j in range(N):
     x = i
     y = (j-p)%N
     a,b = y, (x+p)%N
     if S[i][j] != S[a][b]:
       flag = True
       break
    if flag:
      break
  else:
    ans+= N
print(ans)

# Fixed version:
N, *S = open(0).read().split()
N = int(N)
ans = 0
for p in range(N):
  flag = False
  for i in range(N):
    for j in range(N):
     x = i
     y = (j-p)%N
     a,b = y, (x+p)%N
     if S[i][j] != S[a][b]:
       flag = True
       break
    if flag:
      break
  else:
    ans+= N
print(ans)

# Buggy version:
N = int(input())
S = [input().strip() for _ in range(N)]
cnt = 0
for k in range(N):
    flag = 0
    T = [S[i][k:]+S[i][:k] for i in range(N)]
    for i in range(N-1):
        for j in range(i+1,N):
            if T[i][j]!=T[j][i]:
                flag = 1
                break
        if flag==1:break
    if flag==0:
        cnt += 1
print(cnt**2)

# Fixed version:
N = int(input())
S = [input().strip() for _ in range(N)]
cnt = 0
for k in range(N):
    flag = 0
    T = [S[i][k:]+S[i][:k] for i in range(N)]
    for i in range(N-1):
        for j in range(i+1,N):
            if T[i][j]!=T[j][i]:
                flag = 1
                break
        if flag==1:break
    if flag==0:
        cnt += 1
print(cnt*N)

# Buggy version:
n = int(input())
a = [list(input()) for _ in range(n)]
ans = 0
for s in range(n):
  f = 1
  for i in range(n):
    if not f:
      break
    for j in range(n):
      if a[(i+s)%n][j] != a[j][(i+s)%n]:
        f = 0
        break
  if f:
    ans += n
print(ans)

# Fixed version:
n = int(input())
a = [list(input()) for _ in range(n)]
ans = 0
for s in range(n):
  f = 1
  for i in range(n):
    if not f:
      break
    for j in range(n):
      if a[(i+s)%n][j] != a[(j+s)%n][i]:
        f = 0
        break
  if f:
    ans += n
print(ans)

# Buggy version:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
grid = []

for _ in range(n):
    s = ns()
    s = s+s
    grid.append(s)
    
for i in range(n):
    grid.append(grid[i])
    
vert = []
hrzn = []

for i in range(2*n):
    hrzn.append(grid[i])
    
for j in range(2*n):
    temp = ""
    for i in range(2*n):
        temp += grid[i][j:j+1]
        
    vert.append(temp)
    
    
ans = 0

for a in range(n):
    for b in range(n):
        
        ok = True
        for i in range(1,n):
            if vert[b+i][a+i:a+n] != hrzn[a+i][b+i:b+n]:
                ok = False
                break
                
        if ok:
            ans += 1
            
print(ans)

# Fixed version:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
 
n = ni()
grid = []
 
for _ in range(n):
    s = ns()
    s = s+s
    grid.append(s)
    
for i in range(n):
    grid.append(grid[i])
    
vert = []
hrzn = []
 
for i in range(2*n):
    hrzn.append(grid[i])
    
for j in range(2*n):
    temp = ""
    for i in range(2*n):
        temp += grid[i][j:j+1]
        
    vert.append(temp)
    
    
ans = 0
 
for a in range(n):
    for b in range(1):
        
        ok = True
        for i in range(n-1):
 
            if vert[b+i][a+i:a+n] != hrzn[a+i][b+i:b+n]:
                ok = False
                break
                
        if ok:
            ans += 1
            
print(ans*n)

# Buggy version:
N=int(input())
S=[list(input()) for i in range(N)]
def issym(IL):
    for i in range(N):
        for j in range(i+1,N):
            if IL[i][j]!=IL[j][i]:
                return False
    return True
def changelist(A,B):
    T=[["" for i in range(N)] for j in range(N)]
    for i in range(N):
        for j in range(N):
            T[i][j]=S[(i+A)%N][(j+B)%N]
    return T
ans=0
for i in range(N):
    L=changelist(i,N-1-i)
    if issym(L):
        ans+=N
print(ans)


# Fixed version:
N=int(input())
S=[list(input()) for i in range(N)]
def issym(IL):
    for i in range(N):
        for j in range(i+1,N):
            if IL[i][j]!=IL[j][i]:
                return False
    return True
def changelist(A,B):
    T=[["" for i in range(N)] for j in range(N)]
    for i in range(N):
        for j in range(N):
            T[i][j]=S[(i+A)%N][(j+B)%N]
    return T
ans=0
for i in range(N):
    L=changelist(i,0)
    if issym(L):
        ans+=N
print(ans)

# Buggy version:
n = int(input())
A = [input() for _ in range(n)]
A_t = [''] * n
for c in A:
    for i in range(n):
        A_t[i] += c[i]

def checkTable(shift):
    for i in range(n):
        if A_t[i] != A[i][shift:] + A[i][:shift]:
            return False
    return True

count = 0
for i in range(n):
    if checkTable(i):
        count += 1
print(count * n)

# Fixed version:
n = int(input())
A = [input() for _ in range(n)]
A_t = [''] * n
for c in A:
    for i in range(n):
        A_t[i] += c[i]

def checkTable(shift):
    for i in range(n):
        if A_t[(i + shift) % n] != A[i][shift:] + A[i][:shift]:
            return False
    return True

count = 0
for i in range(n):
    if checkTable(i):
        count += 1
print(count * n)

# Buggy version:
N = int(input())
S = []
for i in range(N):
    S.append(input())

def check(A, B):
    for i in range(N):
        for j in range(i+1, N):
            if S[i][j] != S[j][i]:
                return 0
    return 1

cnt = 0
for i in range(N):
    cnt += check(0, i)
print(cnt * N)

# Fixed version:
N = int(input())
S = []
for i in range(N):
    S.append(input())

def check(S, A, B):
    for i in range(N):
        for j in range(i+1, N):
            if S[i][j] != S[j][i]:
                return 0
    return 1

cnt = 0
for i in range(N):
    cnt += check(S[i:] + S[:i], 0, i)
print(cnt * N)

# Buggy version:
n = int(input())
s = [input() for i in range(n)]
st = [str().join(s_) for s_ in zip(*s)]

cnt = 0

for i in range(n):
    i_ = (n+i)%n
    for k in range(n):
        if s[(i_+k)%n] != st[(i_+k)%n]:
            break
    else:
        cnt += 1

print(cnt*n)

# Fixed version:
n = int(input())
s = [input() for i in range(n)]
st = [str().join(s_) for s_ in zip(*s)]

cnt = 0

for i in range(n):
    i_ = (n+i)%n
    for k in range(n):
        if s[k] != st[k]:
            break
    else:
        cnt += 1
    s = [s_[-1]+s_[:-1] for s_ in s]
    st = [st[-1]] + st[:-1]

print(cnt*n)

# Buggy version:
def is_symmetric(board):
    for i in range(N):
        for j in range(N):
            if board(j, i) != board(i, j):
                return False
    return True


def translate(board, a, b):
    def get(i, j):
        return board[(i + a) % N][(j + b) % N]
    
    return get


def solve(board):
    ans = 0
    for a in range(N):
        if is_symmetric(translate(board, a, 0)):
            ans += N - a
    for b in range(N):
        if b == 0:
            continue
        if is_symmetric(translate(board, 0, b)):
            ans += N - b
    return ans


N = int(input())
board = []
for i in range(N):
    board.append(input())

# Fixed version:


def is_symmetric(board):
    for i in range(N):
        for j in range(i, N):
            if board(j, i) != board(i, j):
                return False
    return True


def translate(board, a, b):
    def get(i, j):
        return board[(i + a) % N][(j + b) % N]
    
    return get


def solve(board):
    ans = 0
    for a in range(N):
        if is_symmetric(translate(board, a, 0)):
            ans += N - a
    for b in range(N):
        if b == 0:
            continue
        if is_symmetric(translate(board, 0, b)):
            ans += N - b
    return ans


N = int(input())
board = []
for i in range(N):
    board.append(input())


print(solve(board))

# Buggy version:
def solve():
    global S, N
    N = int(input())

    S = []
    for n in range(N):
        S.append(list(input()))


    res = 0
    for n in range(N):
        if check(n):
            res += N

    print(res)

def check(move):
    for i in range(N):
        for j in range(i+1, N):
            if S[(i-move)%Nj] != S[(j-move)%N][i]:
                return False
    return True


if __name__ == "__main__":
    solve()

# Fixed version:
def solve():
    global S, N
    N = int(input())

    S = []
    for n in range(N):
        S.append(list(input()))


    res = 0
    for n in range(N):
        if check(n):
            res += N

    print(res)

def check(move):
    for i in range(N):
        for j in range(i+1, N):
            if S[(i-move)%N][j] != S[(j-move)%N][i]:
                return False
    return True


if __name__ == "__main__":
    solve()