Problem:
 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.

Solution (Python):
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):
    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)

Solution (Python):
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)

Solution (Python):
import numpy
N = int(input())
a = []
for i in range(N):
    a.append(list(input()))
nda = numpy.array(a)
count = 0
for i in range(N):
    rolled = numpy.roll(nda, i, axis=1)
    transed = rolled.T
    if numpy.all(rolled == transed):
        count += 1
print(count * N)

Solution (Python):
n = int(input())
A = [input() for _ in range(n)]
out = 0
for k in range(n):
    a = A[k:][:] + A[:k][:]
    if ''.join(a) == ''.join(map(''.join, zip(*a))):
        out += n
print(out)

Solution (Python):
n = int(input())
A = [input() for _ in range(n)]
out = 0
for k in range(n):
    a = A[k:][:] + A[:k][:]
    if ''.join(a) == ''.join(list(map(''.join, zip(*a)))):
        out += n
print(out)

Solution (Python):
n = int(input())
A = [input() for _ in range(n)]
out = 0
for k in range(n):
    a = A[k:][:] + A[:k][:]
    aT = ''.join(map(''.join, zip(*a)))
    if ''.join(a) == aT:
        out += n
print(out)

Solution (Python):
n = int(input())
A = [input() for _ in range(n)]

count = 0
for a in range(n):
    arr = A[a:][:] + A[:a][:]
    arrT = list(map(''.join, zip(*arr)))
    for i in range(n):
        if arr[i][i+1:] != arrT[i][i+1:]:
            break
    else:
        count += n
print(count)

Solution (Python):

import numpy as np


N = int(input())

S = [list(map( ord, list(input()) )) for _ in range(N)]
Sarr = np.array(S)



Sext = np.tile(Sarr, [2,1])[:-1,:]



cnt = 0
for i in range(N):
    
    
    
    
    
    ar = Sext[i:i+N, 0:N]
    cnt += N * (ar == ar.T).all()

print(cnt)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]

cnt = 0

def is_symmetry():
    for k in range(n):
        for l in range(k+1, n):
            if s_[k][l] != s_[l][k]:
                return False
    return True

for i in range(n):
    i_ = (n+i)%n
    s_ = s[i_:] + s[:i_]
    if is_symmetry():
        cnt += 1

print(cnt*n)

Solution (Python):
n = int(input())
s = [input() for i in range(n)]

cnt = 0

def is_symmetry(s_):
    for k in range(n):
        for l in range(k+1, n):
            if s_[k][l] != s_[l][k]:
                return False
    return True

for i in range(n):
    i_ = (n+i)%n
    s_ = s[i_:] + s[:i_]
    if is_symmetry(s_):
        cnt += 1

print(cnt*n)

Solution (Python):
N = int(input())
src = [input() for i in range(N)]

def isok(b):
    for i in range(N-1):
        for j in range(i+1, N):
            i2 = (i+b)%N
            j2 = (j+b)%N
            if src[i][j2] != src[j][i2]:
                return False
    return True

ans = 0
for i in range(-N+1,N):
    if isok(i):
        ans += N-abs(i)
print(ans)

Solution (Python):
from itertools import combinations, product
n=int(input())
s=[["a" for _ in range(n)] for _ in range(n)]
for i in range(n):
  s[i]=list(input())

res=0
for a in range(n):
  for i,j in combinations(range(n),2):
    if(s[(i+a)%n][j%n]!=s[(j+a)%n][i%n]):
      break
  else:
    res+=1

print(res*n)

Solution (Python):
N = int(input())
S = [list(input()) for i in range(N)]

ans = 0
flg = False
for a in range(N):
    S2 = S[a:] + S[:a]
    for i in range(N):
        for j in range(N):
            if S2[i][j] != S2[j][i]:
                flg = True
                break
        if flg:
            flg = False
            break
    else:
        ans += 1

print(ans * N)

Solution (Python):
exec("def nWord(): nWord.cache = raw_input() if not hasattr(nWord, 'cache') or nWord.cache=='' else nWord.cache ; pos = nWord.cache.find(nWord.sep) ; ret, nWord.cache = (nWord.cache, '') if pos==-1 else (nWord.cache[:pos], nWord.cache[pos+1:].lstrip(nWord.sep)) ; return ret") ; exec("nWord.sep = ' '") ; exec("def ___nLine2(): ret, nWord.cache = (raw_input(), nWord.cache if hasattr(nWord, 'cache') else '') if not hasattr(nWord, 'cache') or nWord.cache=='' else (nWord.cache, '') ; return ret") ; exec('def nInt(): return int(nWord())') ; exec('def nFloat(): return float(nWord())') ; exec("def ___nAuto2():\n ret = nWord()\n try: return int(ret)\n except: pass\n try: return float(ret)\n except: return ret") ; exec("def nAuto(n=None): return ___nAuto2() if n is None else [___nAuto2() for i in range(n)]") ; exec("def nLine(n=None): return ___nLine2() if n is None else [___nLine2() for i in range(n)]")
 
N = nAuto()
S = nAuto(N)
 
H = N
W = N
 
def fH(p, H2):
    for k in range(1, p):
        for i in range(H2):
            for j in range(W):
                if S[i][j]!=S[i+k*H2][j]:
                    return False
    return True
 
def fW(p, W2):
    for k in range(1, p):
        for i in range(H):
            for j in range(W2):
                if S[i][j]!=S[i][j+k*W2]:
                    return False
    return True
 
rate = 1
for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 39, 41, 43, 47]:
    while H % p == 0:
        H2 = H / p
        if fH(p, H2):
            H = H2
            rate *= p
        else:
            break
    while W % p == 0:
        W2 = W / p
        if fW(p, W2):
            W = W2
            rate *= p
        else:
            break
 
D = {}
for  i in range(H):
    for j in range(W):
        c = S[i][j]
        if c in D:
            D[c] += 1
        else:
            D[c] = 1
 
max_c = 'a'
max_count = 0
for c in D:
    if max_count < D[c]:
        max_count = D[c]
        max_c = c
 
L = []
for  i in range(H):
    for j in range(W):
        c = S[i][j]
        if c!=max_c:
            L.append((i, j, c))
 
def check(A, B):
    for i2, j2, c in L:
        i = (i2-A+N) % N
        j = (j2-B+N) % N
        if c!=S[(A+j) % N][(B+i) % N]:
            return False
    return True
 
ng = 0
count = 0
for A in range(H):
    if check(A, 0):
        count += W
    else:
        ng += W
print count * rate

Solution (Python):
exec("def nWord(): nWord.cache = raw_input() if not hasattr(nWord, 'cache') or nWord.cache=='' else nWord.cache ; pos = nWord.cache.find(nWord.sep) ; ret, nWord.cache = (nWord.cache, '') if pos==-1 else (nWord.cache[:pos], nWord.cache[pos+1:].lstrip(nWord.sep)) ; return ret") ; exec("nWord.sep = ' '") ; exec("def ___nLine2(): ret, nWord.cache = (raw_input(), nWord.cache if hasattr(nWord, 'cache') else '') if not hasattr(nWord, 'cache') or nWord.cache=='' else (nWord.cache, '') ; return ret") ; exec('def nInt(): return int(nWord())') ; exec('def nFloat(): return float(nWord())') ; exec("def ___nAuto2():\n ret = nWord()\n try: return int(ret)\n except: pass\n try: return float(ret)\n except: return ret") ; exec("def nAuto(n=None): return ___nAuto2() if n is None else [___nAuto2() for i in range(n)]") ; exec("def nLine(n=None): return ___nLine2() if n is None else [___nLine2() for i in range(n)]")
 
N = nAuto()
S = nAuto(N)
 
def check(A):
    for i in range(N):
        for j in range(N):
            if S[(A+i) % N][j]!=S[(A+j) % N][i]:
                return 0
    return N
 
count = 0
for A in range(N):
    count += check(A)
print count

Solution (Python):



IN = 'sample-1.in'  
def local_test():
    try:
        from pcm.utils import set_stdin
        import sys
        import os
        if len(sys.argv) == 1:
            set_stdin(os.path.dirname(__file__) + '/test/' + IN)
    except:
        pass


def preprocess():
    import sys
    import os
    import time
    import re
    from pydoc import help
    import string
    import math
    import numpy as np
    from operator import itemgetter
    from collections import Counter
    from collections import deque
    from collections import defaultdict as dd
    import fractions
    from heapq import heappop, heappush, heapify
    import array
    from bisect import bisect_left, bisect_right, insort_left, insort_right
    from copy import deepcopy as dcopy
    import itertools
    sys.setrecursionlimit(10**7)
    INF = 10**20
    GOSA = 1.0 / 10**10
    MOD = 10**9+7
    def LI(): return [int(x) for x in sys.stdin.readline().split()]
    def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
    def LF(): return [float(x) for x in sys.stdin.readline().split()]
    def LS(): return sys.stdin.readline().split()
    def I(): return int(sys.stdin.readline())
    def F(): return float(sys.stdin.readline())
    def DP(N, M, first): return [[first] * M for n in range(N)]
    def DP3(N, M, L, first): return [[[first] * L for n in range(M)] for _ in range(N)]


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()

Solution (Python):
exec("def nWord(): nWord.cache = raw_input() if not hasattr(nWord, 'cache') or nWord.cache=='' else nWord.cache ; pos = nWord.cache.find(nWord.sep) ; ret, nWord.cache = (nWord.cache, '') if pos==-1 else (nWord.cache[:pos], nWord.cache[pos+1:].lstrip(nWord.sep)) ; return ret") ; exec("nWord.sep = ' '") ; exec("def ___nLine2(): ret, nWord.cache = (raw_input(), nWord.cache if hasattr(nWord, 'cache') else '') if not hasattr(nWord, 'cache') or nWord.cache=='' else (nWord.cache, '') ; return ret") ; exec('def nInt(): return int(nWord())') ; exec('def nFloat(): return float(nWord())') ; exec("def ___nAuto2():\n ret = nWord()\n try: return int(ret)\n except: pass\n try: return float(ret)\n except: return ret") ; exec("def nAuto(n=None): return ___nAuto2() if n is None else [___nAuto2() for i in range(n)]") ; exec("def nLine(n=None): return ___nLine2() if n is None else [___nLine2() for i in range(n)]")
 
N = nAuto()
S = nAuto(N)

def check(A):
    for i in range(N):
        for j in range(N):
            if S[(A+i) % N][j]!=S[(A+j) % N][i]:
                return 0
    return N
 
count = 0
for A in range(N):
    count += check(A)
print count

Solution (Python):
N = int(raw_input())
S = [list(raw_input()) for t in range(N)]
ans = 0
for n in range(N):
  t = S[n:]+S[:n]
  t_t = list(map(list, zip(*t)))
  if t == t_t:
    ans += 1












print ans*N

Solution (Python):
n = int(input())
s = [input() for i in range(n)]
s,s2,ans = s+s,[],0
for i in range(n):
	x = ""
	for j in s: x+=j[i]
	s2.append(x)
for i in range(n):
	flag = True
	for j in range(i,i+n):
		if s[j][1:n]!=s2[j-i][1+i:n+i]:
			flag = False
			break
	if flag: ans += n
print(ans)

Solution (Python):


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))

Solution (Python):


import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq





def solve(f):
    n = f.read_int()
    s = [f.read_str() for _ in xrange(n)]

    ans = 0
    for a in xrange(n):
        si = s[a:] + s[:a]

        flg = True
        for i in xrange(n):
            for j in xrange(n):
                if si[i][j] != si[j][i]: flg = False

        if flg: ans += 1

    return ans * n


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case 
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Solution (Python):
import numpy as np
n=int(input())
s=[chr(i) for i in range(97, 97+26)]
 
aa_cij=np.array([[s.index(i)+1  for i in list(input())] for _ in range(n)],dtype='int8')

ans=0
for i in range(n):
  aa_cij=np.concatenate( [ aa_cij[1:,:], aa_cij[[0],:] ], axis=0)
  if np.allclose(aa_cij, aa_cij.T):
    ans+=1 
print(ans*n)

Solution (Python):
import numpy as np
n=int(input())
s=[chr(i) for i in range(97, 97+26)]
 
aa_cij=np.array([[s.index(i)+1  for i in list(input())] for _ in range(n)],dtype='int8')
z=np.zeros((n,n),dtype='int8')
ans=0
for i in range(n):
  aa_cij=np.concatenate( [ aa_cij[1:,:], aa_cij[[0],:] ], axis=0)
  if np.allclose(aa_cij - aa_cij.T, z ):
    ans+=1 
print(ans*n)

Solution (Python):
def check(mat):
    frag = True
    for i in range(N):
        for j in range(i + 1, N):
            if mat[i][j] != mat[j][i]:
                frag = False
        if not frag:
            break
    return frag

N = int(input())
S = [input() for i in range(N)]
ans = 0
for i in range(N):
    mat = S[i:] + S[:i]
    if check(mat):
        ans += N
print(ans)

Solution (Python):
N = int(input())
S = [list(input()) for i in range(N)]

def is_ok(mat):
    n = len(mat)
    for i in range(n):
        for j in range(i, n):
            if mat[i][j] != mat[j][i]:
                return False
    return True

count = 0
A = 1
for B in range(N):
    next_S = [S[(i+B) % N] for i in range(N)]
    if is_ok(next_S):
        count += 1

print(count * N)

Solution (Python):
import numpy as np

n = int(input())
s = np.array([list(map(ord, input())) for _ in range(n)])
s = np.hstack((s, s))
s = np.vstack((s, s))
ans = 0
for i in range(n):
    p = s[0:n, i:i + n]
    if (p == p.T).all():
        ans += n

print(ans)

Solution (Python):
import itertools

N = int(input())
table = [input() for _ in range(N)]

count = 0

for b in range(N):
    symmetric = True
    new_table = []
    for i in range(N):
        string = table[i][b:] + table[i][:b]
        new_table.append(string)
    
    for i in range(N-1):
        string1 = new_table[i][i+1:]
        string2 = ''.join([s[i] for s in new_table[i+1:]])
        if string1 != string2:
            symmetric = False
            break
    
    if symmetric:
        count += N

print(count)

Solution (Python):

N = int(input())
S_list = list()
for _ in range(N):
    S_list.append(list(input()))

res = 0

def is_symmetric(M):
    res = True
    for i in range(N):
        for j in range(i+1, N):
            if M[i][j] != M[j][i]:
                return False
    return True

for _ in range(N):
    if is_symmetric(S_list):
        res += 1
    for i in range(N):
        S_list[i] = S_list[i][1:] + S_list[i][:1]
print(res*N)

Solution (Python):
import collections
N = int(input())
S = []
for _ in range(N):
    S.append(list(input()))
def taikaku(A):
    n = len(A)
    b = True
    for i in range(n):
        for j in range(i+1):
            if A[j][i] != A[i][j]:
                b = False
                break
        if not b:
            break
    return(b)
ans = 0
for i in range(N):
    if taikaku(S[i:]+S[:i]):
        ans += N
print(ans)

Solution (Python):
import numpy as np

def shift(xs, n):
    e = np.empty_like(xs)
    if n > 0:
        e[:n] = xs[-n:]
        e[n:] = xs[:-n]
    elif n < 0:
        e[n:] = xs[:-n]
        e[:n] = xs[-n:]
    else:
        return xs
    return e

d = {chr(x):x for x in range(ord('a'), ord('z')+1)}
n = int(input())
stemp = ''
temp = []
for i in range(n):
    s = input()
    for c in s:
        stemp+=str(d[c])+' '
    stemp = stemp[:-1] + ';'

m = np.matrix(stemp[:-1])
for i in range(n):
    temp.append(shift(m, i))

ans = 0
for matrix in temp:
    if np.all(matrix == matrix.T):
        ans += 1


print(ans*n)
"""
2
ab
ca


4
aaaa
aaaa
aaaa
aaaa


5
abcde
fghij
klmno
pqrst
uvwxy


"""

Solution (Python):
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()

Solution (Python):


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


def is_good(S):
    
    
    
    
    
    
    T = list(map(list, zip(*S)))

    if T == S:
        return True
    else:
        return False


ans = 0
for a in range(N):
    T = []
    for i in range(N):
        T.append(S[(i+a) % N])
    if is_good(T):
        ans += N

print(ans)

Solution (Python):
import numpy as np

N = int(input())
lisS = [list(map(ord, list(input()))) for _ in range(N)]
arS = np.array(lisS)
arExt = np.tile(arS, [2,2])[:-1,:-1]

cnt = 0
for i in range(N):
    ar = arExt[i:i+N, 0:N]
    cnt += N * (ar == ar.T).all()

print(cnt)

Solution (Python):

from itertools import combinations
def inpl(): return map(int, input().split())

N = int(input())
S = [list(input()) for _ in range(N)]
ans = 0

for k in range(N):
    for i, j in combinations(range(N), 2):
        if S[i][(j+k)%N] != S[j][(i+k)%N]:
            break
    else:
        ans += N

print(ans)

Solution (Python):
n = int(input())
s = [list(input()) for _ in range(n)]

def check(a):
    t = s[a:] + s[:a]
    t_t = list(map(list, zip(*t)))
    
    return t == t_t

ans = [check(a) for a in range(n)]
print(ans.count(True) * n)

Solution (Python):
N = int(input())
m = [input() for _ in range(N)]
mt = list(map(''.join, zip(*m)))

ans = 0
for z in range(N): 
  for y in range(N):
    if m[y][y+1:] != mt[y][y+1:]:
      break
  else:
    ans += N
  m = [l[-1]+l[:-1] for l in m]
  mt = [mt[-1]] + mt[:-1]

print(ans)

Solution (Python):

import numpy as np
N = int(input())
alphabets = list('abcdefghijklmnopqrstuvwxyz')
A = np.zeros((N, N), dtype=int)
for i in range(N):
    for j, c in enumerate(input()):
        A[i,j] = alphabets.index(c)

B = np.concatenate([A, A], axis=0)
cnt = 0
for i in range(N):
    C = B[i:i+N, :]
    if (C==C.T).all():
        cnt += 1
print(cnt*N)

Solution (Python):
N = int(input())
board = []

def isGoodBoard(b):
	for i in range(N):
		for j in range(i,N):
			if b[i][j] != b[j][i]:
				return False
	return True
for i in range(N):
	board.append(list(input()))

ans = 0
for i in range(N):
	board2 = [board[(x+i) % N] for x in range(N)]
	if isGoodBoard(board2):
		ans += N
print(ans)

Solution (Python):


import sys, math, copy

import numpy as np


HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = int(input())
    sl = [[c for c in input()] for i in range(n)]

    s = np.char.array(sl)
    assert s.shape == (n, n)

    res = 0
    for i in range(n):
        sr = np.roll(s, i, axis=1)
        if np.all(sr.T == sr):
            res += n

    print(res)

main()

Solution (Python):
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)

Solution (Python):
N = int(input())
S = [input() for _ in range(N)]

def solve(N, S):
    cs1 = [tuple(x) for x in S]
    cs2 = list(zip(*cs1))
    r = 0
    for d in range(N):
        if cs1 == cs2:
            r += 1
        cs1 = cs1[-1:] + cs1[:-1]
        cs2 = [row[-1:] + row[:-1] for row in cs2]
    return r * N

print(solve(N, S))

Solution (Python):
n = int(input())
ban = []
for i in range(n):
    ban.append(input())
def hantei(data):
    for i in range(1,n):
        for k in range(i):
            if data[i][k] != data[k][i]:
                return(0)
    return(1)
ans = 0
for _ in range(n):
    tmp = ban[_:] + ban[:_]
    ans += hantei(tmp)
print(ans * n)

Solution (Python):
N = int(input())
S = [input() for i in range(N)]

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

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