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

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

Solution (Python):
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):
            
            if l[h][w+i]!=l[w][h+i]:
                f+=1
    if f==0:
        ans+=1
print(ans*n)

Solution (Python):

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

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7

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

ans=0
for i in range(n):
    flag=0
    for j in range(n):
        for k in range(n):
            
            if s[(i+j)%n][k]!=s[(i+k)%n][j]:
                flag=1
    
    if flag==0:
        ans+=n
    
print(ans)

Solution (Python):
n = int(input())
l = []
for i in range(n):
    l.append(list(input()))

def check():
    for i in range(n):
        for j in range(n):
            if i==j:
                continue
            if l[i][j] != l[j][i]:
                return False
    return True

def sft(old):
    newl=[]
    for lis in old:
        last = lis.pop()
        lis.insert(0,last)
        newl.append(lis)
    return newl

cnt=0
for i in range(n):
    if check():
        cnt+=1
    l=sft(l)
print(cnt*n)

Solution (Python):
N=int(input())
X=range(N)
S=[input()for _ in X]
print(sum(all(S[(i+k)%N][j]==S[(j+k)%N][i]for i in X for j in X)for k in X)*N)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
S = [list(input().rstrip()) for _ in range(N)]
row = [[0] * 26 for _ in range(N)]
col = [[0] * 26 for _ in range(N)]
for i in range(N):
    for j in range(N):
        c = ord(S[i][j]) - ord('a')
        row[i][c] += 1
        col[j][c] += 1
ans = 0
same = [[row[i] == col[j] for j in range(N)] for i in range(N)]
for i in range(N):
    for j in range(N):
        if all(same[(i+k) % N][(j+k) % N] for k in range(N)):
            ans += 1
print(ans)

Solution (Python):
N = int(input())
G = [input() for k in range(N)]
ans = 0
for B in range(N):
    temp = [[""]*N for k in range(N)]
    f = 1
    for i in range(N):
        for j in range(N):
            temp[i][j] = G[i][(j+B)%N]
            if temp[j][i] != "":
                if temp[i][j] != temp[j][i]:
                    f = 0
                    break
    ans += f*N

print(ans)

Solution (Python):
import numpy as np
N = int(input())
S = np.array([list(input()) for _ in range(N)], dtype="U1")
S = np.concatenate([S, S], axis=1)


def check(x):
    tmp = S[:, x: x + N]
    if (tmp == tmp.T).all():
        return N
    return 0


ans = 0
for x in range(N):
    ans += check(x)

print(ans)

Solution (Python):
import sys
import numpy as np

read = sys.stdin.read
readline = sys.stdin.readline

N, *S = map(str, read().split())
S = np.array(list(map(list, S)), np.str)
S = np.hstack((S, S))
S = np.vstack((S, S))
N = int(N)
answer = 0
for y in range(N):
    a = S[:N, y:y + N]
    if np.all(a == a.T):
        answer += N

print(answer)

Solution (Python):
import sys
import numpy as np

read = sys.stdin.read
readline = sys.stdin.readline

N, *S = map(str, read().split())
S = np.array(list(map(list, S)), np.str)
S = np.hstack((S, S))
S = np.vstack((S, S))
N = int(N)
answer = 0
for y in range(N):
    for i in range(N):
        a = S[i, y:y + N]
        b = S[:N, i + y]
        if np.any(a != b):
            break
    else:
        answer += N

print(answer)

Solution (Python):
import sys
import numpy as np

read = sys.stdin.read
readline = sys.stdin.readline

N, *S = map(str, read().split())
S = np.array(list(map(list, S)), np.str)
N = int(N)
answer = 0
for y in range(N):
    for i in range(N):
        a = S[i, y:].tolist() + S[i, :y].tolist()
        b = S[:, (i + y) % N].tolist()
        if a != b:
            break
    else:
        answer += N

print(answer)

Solution (Python):
import sys
from itertools import product

read = sys.stdin.read
readline = sys.stdin.readline

N, *S = map(str, read().split())
N = int(N)
x = 0
answer = 0
for y in range(N):
    for i, j in product(range(N), repeat=2):
        if S[(i + x) % N][(j + y) % N] != S[(j + x) % N][(i + y) % N]:
            break
    else:
        answer += N

print(answer)

Solution (Python):
def main():
    N=int(input())
    S=[input()for _ in range(N)]
    T=["".join(s) for s in zip(*S)]
    ans=0
    for i in range(N):
        if all(s==t for s,t in zip(S,T)):
            ans+=1
        S=[s[-1]+s[:-1] for s in S]
        T=[T[-1]]+T[:-1]
    print(ans*N)

if __name__ == "__main__":
    main()

Solution (Python):
import sys


def main():
    N = int(input())
    s = [list(map(str,input())) for _ in range(N)]
    s += s
    ans = 0
    for i in range(N):
        b = True
        for x in range(N):
            for y in range(x,N):
                if s[x+i][y] != s[y+i][x]:
                    b = False
        if b:
            ans += N
    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

import numpy as np

def main():
    N = int(input())
    s = [list(map(str,input())) for _ in range(N)]
    s = np.array(s)
    ans = 0
    for i in range(N):
        T = np.vstack((s[i:],s[:i]))
        if np.all(T==T.T):
            ans += N
            
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):
import numpy as np
n = int(input())
S = np.array([ list(input()) for _ in range(n) ])
ans = 0
for j in range(n):
  M = np.roll(S, j, axis=0)
  if (M==M.T).all():
    ans += 1
print(ans*n)

Solution (Python):
import numpy as np
n = int(input())
S = np.array([ list(input()) for _ in range(n) ])
ans = 0
for j in range(n):
  M = np.roll(S, j, axis=0)
  if (M==M.T).all():
    ans += n
print(ans)

Solution (Python):
import numpy as np
n = int(input())
S = np.array([ list(input()) for _ in range(n) ])
ans = 0
for j in range(n):
  M = S[:, [(l+j)%n for l in range(n)]]
  if (M==M.T).all():
    ans += n
print(ans)

Solution (Python):
def check(c):
    l = [['.']*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            l[(i+c)%n][j] = s[i][j]
    for i in range(n):
        for j in range(n):
            if l[i][j]!=l[j][i]:
                return False
    return True
n = int(input())
s = [input() for _ in range(n)]
ans = 0
for c in range(n):
    if check(c):
        ans+=n
print(ans)

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

ans = 0
for B in range(N):
    flag = True
    for i in range(N):
        for j in range(i+1, N):
            if S[i][(j-B)%N] != S[j][(i-B)%N]:
                flag = False
                break
    if flag:
        ans += N-B

for A in range(1, N):
    flag = True
    for i in range(N):
        for j in range(i+1, N):
            if S[(i-A)%N][j] != S[(j-A)%N][i]:
                flag = False
                break
    if flag:
        ans += N-A
print(ans)

Solution (Python):
n=int(input())
lists=[]
for i in range(n):
    s=input()
    lists.append(s)
is_good=[[False for i in range(n)] for j in range(n)]
for i in range(n):
    for j in range(n):
        p=lists[i][j:]+lists[i][:j]
        flag=True
        for k in range(n):
            if p[k]!=lists[(i+k)%n][j]:
                flag=False
                break
        if flag:
            is_good[i][j]=True

counter=0
for i in range(n):
    for j in range(n):
        flag=True
        for k in range(n):
                if is_good[(i+k)%n][(j+k)%n]==False:
                    flag=False
                    break
        if flag:
            
            counter+=1
print(counter)

Solution (Python):



def main():
        
    N = int(input())

    S = [input() for _ in range(N)]

    """
    あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
    """
    S2 = [["" for _ in range(N)]  for _ in range(N)]
    cnt = 0
    for a in range(N):
        
        for i in range(N):
            for j in range(N):
                if 0 <= i + a <= N-1:
                    y = i + a
                else:
                    y = (i + a) - N
                S2[y][j] = S[i][j]
        
        flag = True
        for i in range(N):
            if not flag:
                break
            for j in range(N):
                if i == j:
                    continue
                if S2[i][j] != S2[j][i]:
                    flag = False
                    break
        if flag:
            cnt += 1

    print(cnt * N)


if __name__ == "__main__":
    main()

Solution (Python):

from bisect import *
from collections import *
from heapq import *




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

N=int(input())
S=[list(input()) for i in range(N)]
count=0


for i in range(N):
    if main():
        count+=N
    for m in range(N):
        a=S[m].pop()
        S[m].insert(0,a)
print(count)

Solution (Python):
n=int(input())
s=[list(input()) for i in range(n)]
ans=0
for i in range(n):
  ct=0
  for j in range(n):
    for k in range(n):
      if s[(j+i)%n][k]==s[(k+i)%n][j]:
        ct+=1
  if ct==n**2:
    ans+=1
print(ans*n)

Solution (Python):
n=int(input())
s=[list(input()) for i in range(n)]
ans=0
for i in range(n):
  ct=0
  for j in range(n):
    for k in range(n):
      if s[(j+i)%n][k]!=s[(k+i)%n][j]:
        ct+=1
  if ct==0:
    ans+=1
print(ans*n)

Solution (Python):
import numpy as np
N = int(input())
S = [[ord(i) for i in input()] * 2 for _ in range(N)]
S = np.array(S)

ans = 0
for i in range(N):
    tmp = S[:, i: i + N]
    if (tmp == tmp.T).all():
        ans += 1

ans = ans * N
print(ans)

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

ans = 0

for k in range(N):
    symmetry = True
    
    for i in range(N-1):
        for j in range(i+1,N):
            
            if S[(k+i)%N][j]!=S[(k+j)%N][i]:
                symmetry = False
                
    if symmetry:
        ans += N
        
print(ans)

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, S: "List[str]"):

    A = 0
    count = 0
    for B in range(N):
        
        flag = True
        
        
        for i in range(N):
            for j in range(i, N):
                
                if S[i][j-B] != S[j][i-B]:
                    flag = False
                    break
            if flag is False:
                break
        if flag:
            count += 1
    print(count*N)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = [next(tokens) for _ in range(N)]  
    solve(N, S)


if __name__ == '__main__':
    main()

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def build_grid(H, W, intv, _type, space=True, padding=False):
    
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: input()
    _list = lambda: list(map(_type, _input()))
    
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H+offset*2, W+offset*2, intv)
    for i in range(offset, H+offset):
        row = _list()
        for j in range(offset, W+offset):
            grid[i][j] = row[j-offset]
    return grid

N = INT()
grid = build_grid(N, N, '', str, space=0)

def check(i, j):
    for k in range(N):
        for l in range(k+1, N):
            if grid[(k+i)%N][(l+j)%N] != grid[(l+i)%N][(k+j)%N]:
                return False
    return True


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

Solution (Python):
N = int(input())
S = [list(map(lambda x: ord(x) - ord("a") + 1, list(input()))) for _ in range(N)]


yoko, tate = [], []
for i in range(N):
  tmp = 0
  for j in range(N):
    tmp += S[i][j] * 2 ** ((N - j - 1) * 5)
  yoko.append(tmp)
for j in range(N):
  tmp = 0
  for i in range(N):
    tmp += S[i][j] * 2 ** ((N - i - 1) * 5)
  tate.append(tmp)



answer = 0
yi, ti = 0, 0
for t in range(N):
  flag = 0
  for i in range(N):
    if yoko[(i + yi) % N] != tate[(i + ti) % N]:
      flag = 1
      break
  if flag == 0:
    answer += N
    
  
  for i in range(N):
    tate[i] = (tate[i] << 5 | tate[i] >> (N - 1) * 5) & (2 ** (5 * N) - 1)
  yi += 1
  

print(answer)

Solution (Python):
import numpy as np
n=int(input())
s=np.array([list(input()) for i in range(n)])
s=np.concatenate([s,s])
ans=0
for i in range(n):
    l=s[i:i+n]
    if (l==l.T).all():
        ans+=n
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = [list(input().rstrip()) for i in range(n)]
ans = 0
for k in range(n):
    for i in range(n):
        for j in range(i+1,n):
            if a[(i+k)%n][j] != a[(j+k)%n][i]:
                break
        else:
            continue
        break
    else:
        ans += n
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    s = [input() for i in range(n)]
    ans = 0
    for i in range(n):
        s = [[s[i][(j+1)%n] for j in range(n)] for i in range(n)]
        for j in range(n):
            for k in range(j):
                if s[k][j] != s[j][k]:
                    break
            else:
                continue
            break
        else:
            ans += n
    print(ans)
    return


if __name__ == "__main__":
    solve()

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


def check(a, b):
    checkgrid = [ [ grid[(i+a)%n][(j+b)%n] for j in range(n)] for i in range(n) ]
    
    ret = True
    for i in range(n):
        for j in range(n):
            if checkgrid[i][j] != checkgrid[j][i]:
                ret = False
                break
    return ret

ans = 0

for i in range(n):
    if check(i, 0):
        ans += n
print(ans)

Solution (Python):
def main():
    import numpy as np
    c = np.concatenate
    n = int(input())
    ss = [list(input()) for _ in [0]*n]
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    d = {j: i for i, j in enumerate(alphabet)}
    s = np.array([[d[i] for i in j] for j in ss], dtype=np.int8)
    cnt = 0
    for a in range(n):
        for i in range(n):
            ia_n = (i+a) % n
            if not (c([s[i, i+a:n], s[i, :a]]) == c([s[i:n-a, ia_n], s[n-a:n, ia_n]])).all():
                break
        else:
            cnt += 1
    print(n*cnt)


main()

Solution (Python):
import collections
import sys
import numpy as np
input = sys.stdin.readline
table = collections.defaultdict(set)
N = int(input())
S = [list(input().rstrip()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*N)]
table2 = [[set() for j in range(N)] for i in range(N)]

for i in range(2*N):
    if i < N:
        T[i] = S[i] + S[i]
    else:
        T[i] = S[i - N] + S[i-N]
ans = 0
T = np.array(T)
for i in range(N):
    tmp = T[0:N, i:i+N]
    if (tmp == tmp.T).all():
        ans += N

    tmp = T[i+1:i+N+1, N-1-i:2*N-1-i]
print(ans)

Solution (Python):
import collections
import sys
import numpy as np
input = sys.stdin.readline
table = collections.defaultdict(set)
N = int(input())
S = [list(input().rstrip()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*N)]
table2 = [[set() for j in range(N)] for i in range(N)]

for i in range(2*N):
    if i < N:
        T[i] = S[i] + S[i]
    else:
        T[i] = S[i - N] + S[i-N]
ans = 0
T = np.array(T)
for i in range(N):
    tmp = T[i:i+N, N-1-i:2*N-1-i]

    if (tmp == tmp.T).all():
        ans += N - (N // 2)

    tmp = T[i+1:i+N+1, N-1-i:2*N-1-i]

    if (tmp == tmp.T).all():
        ans += N // 2
print(ans)

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

Solution (Python):
def examA():
    N = I()
    A = LI()
    d = defaultdict(int)
    d[0] = 1; cur = 0
    for i in A:
        cur +=i
        d[cur] +=1

    ans = 0
    for i in d.values():
        ans +=i*(i-1)//2
    print(ans)
    return

def examB():
    N = I()
    S = [SI() for _ in range(N)]
    cur = 0
    for k in range(N):
        flag = True
        for i in range(N):
            for j in range(i+1,N):
                if S[i][j-k]!=S[j][i-k]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            cur +=1
    ans = cur*N
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Solution (Python):
N = int(input())

S = []

ans = 0

for i in range(N):

    s = list(input())
    S.append(s)


for A in range(N):

    flag = True

    for i in range(N):

        for j in range(i):

            if S[(i+A) % N][j % N] != S[(j+A) % N][i % N]:
                flag = False
                break

        if not flag:
            break

    if flag:
        ans += N

print (ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from itertools import product

def resolve():
    n=int(input())
    G=[input() for _ in range(n)]
    ans=0
    for i in range(n):
        now=G[i:]+G[:i]
        ans+=all(now[i][j]==now[j][i] for i,j in product(range(n),repeat=2))

    print(n*ans)
resolve()

Solution (Python):
import numpy as np

N = int(input())
S = [list(input()) for _ in range(N)]
S = np.array(S, dtype=object)

S = np.concatenate([S, S])
ans = 0
for B in range(N):
    T = S[B:B+N]
    if (T == T.T).all():
        ans += N
print(ans)

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

def generate(A, B):
    T = [[""] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            ni = (i + A) % N
            nj = (j + B) % N
            T[ni][nj] = S[i][j]
    flag = True
    for i in range(N):
        for j in range(N):
            flag &= T[i][j] == T[j][i]
    return flag

ans = 0

for A in range(N):
    ans += N * generate(A, 0)

print(ans)

Solution (Python):
import sys
import numpy as np

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip() 

N = ri()
S = np.array([list(rs()) for _ in range(N)])

S = np.concatenate([S, S], axis=0)
answer = 0
for b in range(N):
    S2 = S[b:b+N]
    if (S2 == S2.T).all():
        answer += N

print(answer)

Solution (Python):
n = int(input())
arr = []
for i in range(n):
    a = list(map(ord,list(input())))
    arr.append(a)


ans = 0

for i in range(n):
    ok = True
    for j in range(n):
        for k in range(n):
            if j == k:
                continue
            l = j + i
            if l > n-1:
                l = l-n

            m = k + i
            if m > n-1:
                m = m-n
            
            
            if arr[l][k] != arr[m][j]:
                ok = False
                break

    
    if ok:
        ans += n



print(ans)

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



N = int(sys.stdin.buffer.readline())
S = [list(sys.stdin.buffer.readline().decode().rstrip() * 2) for _ in range(N)] * 2
S = np.array(S, dtype='U1')

ans = 0
for h in range(N):
        s = S[h: h + N, 0: 0 + N]
        if np.all(s == s.T):
            ans += N
print(ans)

Solution (Python):
n,*o=open(0)
n=int(n)
r=list(range(n))
print(sum((n-a-b)*all(o[(i+b)%n][(a+j)%n]==o[(j+b)%n][(a+i)%n]for i in r for j in r[i:])for a,b in zip(r+[0]*n,[0]*~-n+r)))

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]
ans = 0
for j in range(n):
    for k in range(j + 1, n):
        if s[j][k] != s[k][j]:
            break
    else:
        continue
    break
else:
    ans += n
for i in range(1, n):
    for j in range(n):
        for k in range(j + 1, n):
            if s[j + i - n][k] != s[k + i - n][j]:
                break
        else:
            continue
        break
    else:
        ans += n
print(ans)

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

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

ret = 0
for a in range(N) :
    if check(a) :
        ret += N
            
print(ret)

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

ans=0
for k in range(N):
  ju=1
  for i in range(N):
    for j in range(N):
      if S[i][(k+j)%N]!=S[j][(k+i)%N]:
        ju=0
        
  if ju==1:
    ans+=1

print(N*ans)

Solution (Python):
def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    def check(a):
        for i in range(n):
            for j in range(i):
                if s[i%n][(j+a)%n]!=s[j%n][(i+a)%n]:
                    return 0
        return 1
    print(n*sum([check(i) for i in range(n)]))
main()

Solution (Python):
def main():
    n=int(input())
    nn=2*n-1
    s=[input() for _ in [0]*n]
    s=[i+i[:-1] for i in s]+[i+i[:-1] for i in s][:-1]
    ans=[[0]*n for _ in [0]*n]
    for i in range(1,nn):
        m=min(i+1,n)
        for j in range(nn-1):
            for k in range(max(i-j-n,0)+1,min(m,nn-j,i-j+n)):
                ii=i-k
                if s[i][j]!=s[ii][j+k]:
                    l=min(n-k+1,ii,j)
                    width=l-max(-1,ii-n,j-n)
                    ans[ii-l][j-l]=max(ans[ii-l][j-l],width)
    for i in range(1,n):
        for j in range(1,n):
            ans[i][j]=max(ans[i][j],ans[i-1][j-1]-1)
    print(sum([sum([i==0 for i in a]) for a in ans]))
main()

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

ans = 0
for n in range(N):
    m = S[n:]+S[:n]
    flg = True
    for i in range(N):
        for j in range(N):
            if m[i][j] != m[j][i]:
                flg = False

    if flg:
        ans += N

print(ans)

Solution (Python):

from collections import defaultdict


def getlist():
	return list(map(int, input().split()))


def main():
	N = int(input())
	S = []
	for i in range(N):
		s = input()
		S.append(s)
	ans = 0
	for k in range(N):
		judge = "Yes"
		for i in range(N):
			for j in range(N):
				if S[i][(j + k) % N] != S[j][(i + k) % N]:
					judge = "No"
					break
		if judge == "Yes":
			ans += 1
	print(ans * N)

if __name__ == '__main__':
	main()

Solution (Python):
def is_good_grid(sft):
    for r in range(n):
        r_ = (r - sft) % n  
        for c in range(r):
            c_ = (c - sft) % n  
            if s[r][c_] != s[c][r_]:
                
                
                
                return False
    return True


n = int(input())
s = tuple(input() for _ in range(n))





ans = 0
for k in range(n):
    if is_good_grid(k):
        ans += n
print(ans)

Solution (Python):
def is_good_grid():
    for r in range(n):
        for c in range(r):
            if t[r][c] != t[c][r]:
                return False
    return True


n = int(input())
s = tuple(input() for _ in range(n))





ans = 0
for k in range(n):
    t = [[''] * n for _ in range(n)]
    for r in range(n):
        for c in range(n):
            t[r][c] = s[r][(c + k) % n]
    if is_good_grid():
        ans += n
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
s = [list(input())[: -1] for _ in range(N)]
res = 0
for a in range(N):
  f = 1
  for i in range(N):
    for j in range(N):
      if s[(i + a) % N][j] != s[(j + a) % N][i]:
        f = 0
        break
  if f: res += N
print(res)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
S = []
for _ in range(N):
    S.append(str(input()))

ans = 0
for b in range(N):
    flag = True
    for i in range(N):
        for j in range(i+1,N):
            if S[i][(j+b)%N] != S[j][(i+b)%N]:
                flag = False
                break
        else:
            continue
        break
    if flag:
        ans += N

print(ans)

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

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

ans = 0
SS = []
for i in range(N):
  SS = []
  for j in range(1,N):
    SS.append(S[j])
  SS.append(S[0])
  
  ans += check(SS)
  S = SS
print(ans)

Solution (Python):
N=int(input())
L=[]
for i in range(N):
    l=list(input())
    L.append(l)
    

F=0
ans=0
for i in range(N):
    F=0
    for a in range(N):
        if F==1:
            break
        for b in range(N):
            if L[a][(b+i)%N]!=L[b][(a+i)%N]:
                F=1
                break
    if F==0:
        ans+=N
        
print(ans)

Solution (Python):
n = int(input())
s = []
for _ in range(n):
    s.append(input())
st=list(map(''.join,zip(*s)))

def plusa(s,a):
    ns = []
    for ss in s:
        ns.append(ss[a:]+ss[:a])
    return ns

def plusb(s,b):
    global n
    ns = []
    for i in range(n-b):
        ns.append(s[b+i])
    for i in range(b):
        ns.append(s[i])
    return ns

res = 0
for i in range(n):
    if plusa(st,i) == plusb(s,i):
        res += n

print(res)

Solution (Python):
n = int(input())
s = [input() for _ in range(n)]
t = []
for i in range(n):
    t.append(s[i] + s[i])
t = t + t
ans = 0
for i in range(n):
    ok = True
    for j in range(n):
        for k in range(n):
            if t[i+j][k] != t[i+k][j]: 
                ok = False
                break
        if not ok: break
    if ok: ans += 1
print(ans * n)

Solution (Python):
N = int(input())
S = list(input() for i in range(N))
ans = 0
for A in range(N):
    flag = 1
    for i in range(N):
        for j in range(N):
            if S[i][(j+A)%N] != S[j][(i+A)%N]:
                flag = 0
    ans += flag*N
print(ans)