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):
import numpy as np
n = int(input())
S = [list(input()) for _ in range(n)]

ss = np.array(S + S)

ans = 0
for i in range(n):
    a = ss[i:i+n]
    if np.array_equal(a, a.T):
      ans += 1
print(ans * n)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LS2(): return list(sys.stdin.readline().rstrip())  


N = I()
S = [LS2() for _ in range(N)]




ans = 0
for a in range(N):
    X = [S[i] for i in range(a,N)] + [S[i] for i in range(a)]
    for i in range(N-1):
        for j in range(i+1,N):
            if X[i][j] != X[j][i]:
                break
        else:
            continue
        break
    else:
        ans += 1

print(ans*N)

Solution (Python):
N = int(input())
S = []
for i in range(N):
  temp = str(input()); temp = list(temp)
  S.append(temp)
ans = 0
for alpha in range(-N+1,N): 
  
  Flag = True
  for i in range(N):
    if not Flag:
      break
    for j in range(N):
      p = (j-alpha+N)%N
      q = (i+alpha)%N
      
      if S[i][j] != S[p][q]:
        Flag = False
        break
  if Flag:
    ans += N - abs(alpha)
print(ans)

Solution (Python):
n=int(input())
def rotate(l, n):
    return l[-n:] + l[:-n]
s=[list(input())for i in range(n)]
ans=0;
for _ in range(n):
  for i in range(n):
    s[i]=rotate(s[i],1)
  f=1
  for i in range(n):
    for j in range(i+1,n):
      f &= s[i][j]==s[j][i]
  ans+=f
print(ans*n)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


N = I()
S = [LS2() for _ in range(N)]
ans = 0
for a in range(N):
    X = [S[i] for i in range(a,N)] + [S[i] for i in range(a)]
    for i in range(N-1):
        for j in range(i+1,N):
            if X[i][j] != X[j][i]:
                break
        else:
            continue
        break
    else:
        ans += 1

print(ans*N)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7







N = getN()

B = []
for i in range(N):
    q = list(input())
    B.append(q * 2)
B += B






def judge(a, b):
    """
    res = [[''] * N for i in range(N)]
    
    for i in range(N):
        for j in range(N):
            res[(i + a) % N][(j + b) % N] = B[i][j]
    """

    for i in range(N):
        for j in range(i, N):
            if B[i + a][j + b] != B[j + a][i + b]:
                return 0
        else:
            continue
        break
    else:
        return 1

















ans = 0
for i in range(N):
    
    
    ans += (N - i) * judge(i, 0)


for i in range(1, N):
    ans += (N - i) * judge(0, i)
print(ans)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7







N = getN()

B = []
for i in range(N):
    q = list(input())
    B.append(q * 2)
B += B






def judge(a, b):
    """
    res = [[''] * N for i in range(N)]
    
    for i in range(N):
        for j in range(N):
            res[(i + a) % N][(j + b) % N] = B[i][j]
    """

    for i in range(N):
        for j in range(N):
            if B[i + a][j + b] != B[j + a][i + b]:
                return 0
        else:
            continue
        break
    else:
        return 1

















ans = 0
for i in range(N):
    
    
    ans += (N - i) * judge(i, 0)


for i in range(1, N):
    ans += (N - i) * judge(0, i)
print(ans)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7







N = getN()

B = [list(input()) for i in range(N)]






def judge(a, b):
    res = [[''] * N for i in range(N)]
    
    for i in range(N):
        for j in range(N):
            res[(i + a) % N][(j + b) % N] = B[i][j]

    for i in range(N):
        for j in range(N):
            if res[i][j] != res[j][i]:
                return 0
        else:
            continue
        break
    else:
        return 1

















ans = 0
for i in range(N):
    
    
    ans += (N - i) * judge(i, 0)


for i in range(1, N):
    ans += (N - i) * judge(0, i)
print(ans)

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

ans = 0
for k in range(N):
  D = [[0] * N for i in range(N)]
  for i in range(N):
    for j in range(N):
      D[i][j] = S[i][(j + k) % N]
  for i in range(N):
    for j in range(N):
      if D[i][j] != D[j][i]:
        break
    else:
      continue
    break
  else:
    ans += 1

print(ans * N)

Solution (Python):
N = int(input())
S = [list(input()) for i in range(N)]
ans = 0
for n in range(N):
    flag = True
    for i in range(N):
        for j in range(i, N):
            if S[i][j] == S[j][i]:
                continue
            flag = False
            break
        if not flag:
            break
    if flag:
        ans += N
    for i in range(N):
        S[i] = [S[i][-1-n]] + S[i]
print(ans)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main2():
	n = int(input())
	
	
	mt=[input() for _ in range(n)]
	
	tt=[]
	for i in range(n):
		l=[]
		for s in mt:
			l.append(s[i])
		tt.append(''.join(l+l))
	pa(tt)
	ret = 0
	for ai in range(n):
		for s,t in zip(mt, tt):
			pa((s, t[ai:ai+n]))
			if t[ai:ai+n] != s:
				break
		else:
			ret += n
	return ret
	
def is_s(a,mt):
	pass
	
	n=len(mt)
	for i in range(n):
		for j in range(n):
			
			if mt[(i+a)%n][j] != mt[(j+a)%n][i]:
				return False
	return True
	
def main():
	n = int(input())
	
	
	mt=[list(input()) for _ in range(n)]
	ret = 0
	for i in range(n):
		if is_s(i, mt):
			ret += n
	return ret
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):







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

count = 0
for diff in range(n):
    flag = True
    for i in range(n):
        for j in range(i+1, n):
            if masume[(i+diff)%n][j] != masume[(j+diff)%n][i]:
                flag = False
                break
        if not flag:
            break
    count += flag

print(count * n)

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

Solution (Python):
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)]
    res = 0
    for a in range(N):
        f = True
        for i in range(N):
            for j in range(N):
                if S[(i - a) % N][j] != S[(j - a) % N][i]:
                    f = False
        if f:
            res += N
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
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)]
    res = 0
    for a in range(N):
        f = True
        for i in range(N):
            for j in range(N):
                if S[(i - a) % N][j] != S[(j - a) % N][i]:
                    f = False
                    break
            if not f:
                break
        if f:
            res += N
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = [list(input()) for i in range(n)]
ans = 0
for a in range(n):
    check = True
    for x in range(n):
        for y in range(x):
            if s[(x+a)%n][y] != s[(y+a)%n][x]:
                check = False
                break
        if check == False:
            break
    if check:
        ans += n
    
print(ans)

Solution (Python):
n = int(input())
s = [list(input()) for i in range(n)]
ans = 0
for a in range(n):
    check = True
    for x in range(n):
        for y in range(x):
            if s[(x+a)%n][y] != s[(y+a)%n][x]:
                check = False
                break
        if check == False:
            break
    if check:
        ans += n-a
    if a == 0:
        continue
    check = True
    for x in range(n):
        for y in range(x):
            if s[x][(y+a)%n] != s[y][(x+a)%n]:
                check = False
                break
        if check == False:
            break
    if check:
        ans += n-a
print(ans)

Solution (Python):
N=int(input())
L=list()
for i in range(N):
  L.append(list(input())*2)
L=L*2
ans=0
for o in range((2*N)-1):
  a=0
  if o<N:
    i=0
    j=o
    omomi=N-j
  else:
    j=0
    i=o-N+1
    omomi=N-i
  for t in range(N):
    for q in range(t):
      if L[i+t][j+q]!=L[i+q][j+t]:
        a=1
        break
    if a==1:
      break
  if a==0:
    ans+=omomi
print(ans)

Solution (Python):



def chr2ord(c):
  return ord(c) - ord('a')

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

ANS = 0


for b in range(N):
  cnt = 0
  B = [['X' for _ in range(N)] for _ in range(N)]
  for i in range(N):
    for j in range(N):
      B[i][j] = S[i][(j+b)%N]
  
  for i in range(N):
    for j in range(N):
      if B[i][j] == B[j][i]:
        cnt += 1
  
  if cnt == N*N:
    ANS += N
print(ANS)

Solution (Python):
"""
不変なもの
- 移動した行/列内の順番
○ 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)

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n = ni()
S =[]
for i in range(n):
    S.append(input())
def isok(a,b):
    for j in range(n):
        for i in range(j+1,n):
            if not S[(j+b)%n][(i+a)%n] == S[(i+b)%n][(j+a)%n]:
                return False
    return True

ans = 0
if isok(0,0):
    ans +=n
for a in range(1,n):
    if isok(a,0):
        ans+=n-a
    if isok(0,a):
        ans+=n-a
    
print(ans)

def solve2():
    ret=0
    for a in range(n):
        for b in range(n):
            if isok(a,b):
                ret+=1
                print(a,b)
    print(ret)

Solution (Python):
n=int(input())
s=[input() for _ in range(n)]
ans=0
for k in range(n):
  cnt=0
  for i in range(n): 
    l=(i+k)%n
    for j in range(n):
      cnt+=s[i][j]==s[(i+j-l)%n][l]
  if cnt==n**2:ans+=n
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7

    n=I()
    S=[SI() for i in range(n)]
    ans=0
    if n==1:
        print(1)
        sys.exit()
    st=n*(n-1)//2
    for b in range(n):
        count=0
        for i in range(n-1):
            for j in range(i+1,n):
                
                
                if S[i][(j+b)%n]==S[j][(i+b)%n]:
                    count+=1
            if count==st:
                ans+=n
    print(ans)
        
        
                





if __name__=="__main__":
    main()

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


sys.setrecursionlimit(10**7)









INF = 1 << 50
EPS = 1e-8


def run():
    N = int(input())
    S_ = []

    for i in range(N):
        s = input()
        s = [ord(ss)-96 for ss in s] * 2
        S_.append(s)

    
    
    S = []
    for s in S_:
        S.append(s)
    for s in S_:
        S.append(s)

    
    


    axises = [(i, 0) for i in range(N)]
    for i in range(1, N):
        axises.append((0, i))

    ans = 0

    
    for axis in axises:
        ci, cj = axis
        done = False
        for i in range(N):
            if done:break
            for j in range(i, N):
                if S[ci+i][cj+j] == S[ci+j][cj+i]:continue
                else:
                    
                    done=True
                    break
        if not done:
            ans += min(N-ci, N-cj)


    print(ans)

if __name__ == "__main__":
    run()

Solution (Python):
import sys
import math
import copy
import random
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter



input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        
        
        
        
        

def solve():
    n = getN()
    g = []
    for i in range(n):
        g.append(getS())


    ans = 0
    for zu in range(n):
        fl = True
        for i in range(n):
            for j in range(n):
                if g[i][(j+zu) % n] != g[j][(i+zu) % n]:
                    fl = False
                    break
            if not fl:
                break
        if fl:
            ans += n

    print(ans)



def main():
    n = getN()
    for _ in range(n):
        s = "".join([random.choice(["F", "T"]) for i in range(20)])
        print(s)
        solve(s, 1, 0)

    return
if __name__ == "__main__":
    
    solve()

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

ans=0
for i in range(0,N):
    ans+=N*int(all(S[(k+i)%N][j]==S[(j+i)%N][k] for j in range(N) for k in range(N)))

print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    def calc(x):
        for row in range(n):
            for col in range(n):
                if s[(row + x) % n][col] != s[(col + x) % n][row]:
                    return 0
        return n

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

    ans = 0

    for i in range(n):
        ans += calc(i)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

N = int(sys.stdin.readline())
grid = []
for _ in range(N):
    grid.append(list(sys.stdin.readline().strip()*2))
for i in range(N):
    grid.append(grid[i])

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

print(ans * N)

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

Solution (Python):
import numpy as np
n = int(input())
S = [list(input()) for _ in range(n)]

ss = [s + s for s in S]
ss = np.array(ss + ss)

ans = 0
for i in range(n):
    a = ss[i:i + n, :n]
    if np.array_equal(a, a.T):
      ans += 1
print(ans * n)

Solution (Python):
import sys
import math
import collections
import bisect
import itertools



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    n = ni()
    mat = [list(sys.stdin.readline()) for _ in range(n)]

    ans = 0
    for i in range(n):
        tmp = [[mati[(i + j) % n] for j in range(n)] for mati in mat]
        flg = True
        for y in range(n):
            for x in range(n):
                if tmp[y][x] != tmp[x][y]:
                    flg = False
        ans += flg * n
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
s = [input() for i in range(n)]
ans = 0
for i in range(n):
    flag = True
    for x in range(n):
        for y in range(n):
            if s[(x-i)%n][y] != s[(y-i)%n][x]:
                flag = False
    if flag:
        ans += 1
print(ans*n)

Solution (Python):



import numpy as np

n = int(input())
s = [list(input())*2 for _ in range(n)]
s = s * 2
s = np.array(s)
ret = 0
for a in range(n):
    si = s[a:a+n, :n]
    if (si == si.T).all():
        ret += 1
print(ret*n)

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


ans = 0

for A in range(N):
    if all([S[(i+A)%N][j] == S[(j+A)%N][i] for i in range(N) for j in range(i+1, N)]):
        ans += N
print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s - ord('a') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N = in_n()
    grid = in_map2(N)

    cnt = 0
    for i in range(N):
        tmp_grid = [[0] * N for _ in range(N)]
        for y in range(N):
            for x in range(N):
                tmp_grid[(y + i) % N][x] = grid[y][x]

        

        flag = True
        for y in range(N):
            for x in range(N):
                if tmp_grid[y][x] != tmp_grid[x][y]:
                    flag = False

        if flag:
            cnt += N

    print(cnt)


if __name__ == '__main__':
    main()

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




def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))


def main():
	n=II()
	G=[]
	for _ in range(n):
		s=input().rstrip().decode()
		G.append(list(s*2))

	ans=0
	for x in range(n):
		f=1
		for i in range(n):
			if f==0:
				break
			for j in range(n):
				
				if G[i][j]!=G[j-x][i+x]:
					f=0
					break
		else:
			
			ans+=n
	print(ans)










if __name__ == "__main__":
	main()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    grid = [list(input().rstrip()) for _ in range(n)]

    for i in range(1, n):
        target = [grid[0][(i + j) % n] for j in range(n)]
        if grid[i] != target:
            break
    else:
        print(n ** 2)
        exit()

    res = 0
    for a in range(n):
        grid2 = [[""] * n for _ in range(n)]
        for i in range(n):
            for j in range(n):
                grid2[i][j] = grid[(i + a) % n][j]
        for i in range(n):
            for j in range(n):
                if grid2[i][j] != grid2[j][i]:
                    break
            else:
                continue
            break
        else:
            res += 1
    print(res * n)


if __name__ == '__main__':
    resolve()