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.



Sample input:
2
ab
ca


Sample output:
2

Submission status: Accepted

Solution code:
N=int(input())
ans,flag=0,0
S=[list(input()) for i in range(N)]
for k in range(N):
  flag=0
  for i in range(N):
    for j in range(N):
      if S[i][j]!=S[(j+k)%N][(i-k)%N]:
        flag=1
        break
    if flag==1:
      break
  if flag==0:
    ans+=N
print(ans)

Submission status: Accepted

Solution code:
# coding: utf8

if __name__ == '__main_sample__':
    """
        (A, B)と(A+1, B+1)の結果は同じになる.
        (0, 0), (0, 1) ... (0, N-1)をチェックすれば良くなる.
    """
    N = int(input())
    rows = [input() for _ in range(N)]
    ans = 0
    for n in range(N):
        flag = False
        for i in range(N):
            for j in range(N):
                num = j - n if j - n >= 0 else N + (j - n)
                a = i - n if i - n >= 0 else N + (i - n)
                if rows[i][num] != rows[j][a]:
                    flag = True
                    break
            if flag == True:
                break
        if flag == False:
            ans += 1
    print(ans * N)

if __name__ == '__main__':
    """
        高速化された開放
    """
    N = int(input())
    Sss = [input() for _ in range(N)]
    Sss_tr = list(map(''.join, zip(*Sss))) # 転置
    ans = 0
    for k in range(N):
        for i in range(N):
            if (Sss[i][-k:] + Sss[i][:-k]) != Sss_tr[i - k]:
                break
        else:
            ans += N
    
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
S = np.array([list(input()) for _ in range(N)]) 
ans = 0
for i in range(N):
    S = np.roll(S, 1, axis = 1)
    if (S != S.T).any():
        continue
    ans += 1
print(ans * N)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N = d_in()
S_LIST = []
for _ in range(N):
    s = list(s_in())
    S_LIST.append(list(map(ord, s)))
S_LIST = np.array(S_LIST, dtype=int)

ans = 0
for i in range(N):
    check_array = np.concatenate((S_LIST[:, i:], S_LIST[:, :i]), axis=1)
    if np.array_equal(check_array, check_array.T):
        ans += 1
print(ans*N)

Submission status: Accepted

Solution code:
N = int(input())
Sss = [input() for _ in range(N)]
Sss_tr = list(map(''.join, zip(*Sss))) # 転置

ans = 0
for k in range(N):
    for i in range(N):
        if (Sss[i][-k:] + Sss[i][:-k]) != Sss_tr[i - k]:
            break
    else:
        ans += N

print(ans)

Submission status: Accepted

Solution code:
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
b = 0 

for a in range(n):
        
    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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=[list(input()) for i in range(n)]
a=np.array(a)
c=0
for i in range(n):
    a=np.roll(a,1,axis=0)
    if len(a[a!=a.T])==0:c+=n
print(c)

Submission status: Accepted

Solution code:
from itertools import combinations
N = int(input())
grid = tuple(tuple(input()) for _ in range(N))

res = 0
for d in range(N):
  for x,y in combinations(range(N), 2):
    if grid[(x+d)%N][y] != grid[(y+d)%N][x]:
      break
  else:
    res += 1
print(res*N)

Submission status: Accepted

Solution code:
n=input()
s=[raw_input() for i in range(n)]
t=[]
for i in range(n):
	t+=[s[j][i] for j in xrange(n)]*2
s+=s
s=''.join(s)
#print s
t=''.join(t)
#print t
result=0
for shift in xrange(n):
	ok=True
	for i in xrange(n):
		if s[(i+shift)*n: (i+shift)*n+n]!=t[i*n*2+shift: i*n*2+shift+n]:
			ok=False
			break
	if ok:
		result+=n
print result

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
from collections import deque


def main():
  def check(mp):
    for i in range(n):
      for j in range(i, n):
        if mp[i][j] != mp[j][i]: return 0
    return 1
  n = int(input())
  mp = deque([deque(list(input())) for _ in range(n)])
  ans = 0
  for a in range(n):
    mp.rotate()
    ans += check(mp)
  print(ans * n)
  
main()

Submission status: Accepted

Solution code:
N = int(input())
S = [input() for i in range(N)]
r = N*(N-1)//2
ans = 0
for i in range(N):
    g = 0
    for j in range(N):
        ji = (j+i) % N
        for k in range(j+1, N):
            if S[j][k+i-N] == S[k][ji]:
                g += 1
    if g == r:
        ans += 1
    for j in range(N-1):
        ji = (j+i) % N
        for k in range(N-1):
            if S[1+k+j-N][ji] == S[j][ji+k+1-N]:
                g += 1
            if S[j+k-N][ji] == S[j][ji+k-N]:
                g -= 1
        if g == r:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S=[]
for i in range(N):
    s =list(input())
    S.append(s)
num=0
for a in range(N):
    t =1
    flag = True
    for i in range(N):
        if not flag:
            break
        for j in range(N):
            if i==j:
                continue
            #x = (i+a)%N
            #y=j
            if S[(j+a)%N][i]!=S[(i+a)%N][j]:
                t=0
                flag=False
                break
    if t:
        num+=1
print(N*num)

Submission status: Accepted

Solution code:
N = int(input())
S=[]
for i in range(N):
    s =list(input())
    S.append(s)
num=0
for a in range(N):
    t =1
    #flag = True
    for i in range(N):
        #if not flag:
           # break
        for j in range(N):
            if i==j:
                continue
            #x = (i+a)%N
            #y=j
            if S[(j+a)%N][i]!=S[(i+a)%N][j]:
                t=0
                #flag=False
                #break
    if t:
        num+=1
print(N*num)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
S=[]
for i in range(n):
    S.append(input())

a=0

for i in range(n):
    S=[S[-1]]+S[:-1]
#    print(S)
    flag=1
    for j in range(n):
        for k in range(j):
            if S[j][k] != S[k][j]:
                flag=0
                break
    if flag == 1:
        a = a+1

print(a*n)

Submission status: Accepted

Solution code:
N = int(input())
S = []
for i in range(N):
    S.append(input())
T = []
for i in range(N):
    tmp = ''
    for j in range(N):
        tmp += S[j][i]
    T.append(tmp[:])
#print(S)
#print(T)

ans = 0
for i in range(N):
    #print(i)
    flag = True
    for y in range(N):
        #print(S[y][i:]+S[y][:i])
        #for x in range(N):
            #if x==y:
            #    continue
            #print(x,y,(x+i)%N, S[y][(x+i)%N],S[(x+i)%N][y])
            #if S[y][(x+i)%N]!=S[(y+i)%N][x]:
        #print(y, T[(y+i)%N], S[y][i:]+S[y][:i])
        if T[(y+i)%N]!=S[y][i:]+S[y][:i]:
            flag = False
            break
    if flag:
        ans += 1

print(ans*N)

Submission status: Accepted

Solution code:
import numpy as np
from scipy.ndimage.interpolation import shift as scipy_shift

class shift:
    def __init__(self,a):
        self.arr = a
        self.height = a.shape[0]
        self.width = a.shape[1]
    def shifted(self,down,right):
        temp1 = self.arr[list(np.linspace(-down, -down + self.height - 1, self.height).astype(np.int32) % self.height), :]
        temp2 = temp1[:, list(np.linspace(-right, -right + self.width - 1, self.width).astype(np.int32) % self.width)]
        return temp2
    def shifted_zero(self,down,right):
        index_vertical = np.arange(self.height)
        index_horizontal = np.arange(self.width)
        temp1 = self.arr[list(scipy_shift(index_vertical, down, output = int, mode = 'constant')),:]
        temp2 = temp1[:,list(scipy_shift(index_horizontal, right, output = int, mode = 'constant'))]
        return temp2
    def is_sym(self,down,right):
        temp = self.shifted(down,right)
        delta = temp - temp.T
        return(not (delta*delta).sum())



N = int(input())
array = [list(input()) for i in range(N)]
array_int = np.array([[ord(i) for i in j] for j in array])

if N >= 2:
    count = np.array([shift(array_int).is_sym(0,i)*(N-i) for i in range(N)]).sum() + np.array([shift(array_int).is_sym(i,0)*(N-i) for i in range(1,N)]).sum()
else:
    count =  int(shift(array_int).is_sym(0,0))
print(count)

Submission status: Accepted

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

sol = 0

for i in range(n):
    for k in range(n):
        if s[k] != st[k]:
            break
    else:
        sol += 1
    
    s = [s_l[1:]+s_l[0] for s_l in s]
    st = st[1:] + [st[0]]
    
print(sol * n)

Submission status: Accepted

Solution code:
N=input()
S=[ list(raw_input()) for i in range(N)]

A=0
ans=0
for B in range(N):
	flag=0
	for i in range(N):
		if flag==-1: break
		for j in range(N):
			x1=(N+i-A)%N
			y1=(N+j-B)%N

			x2=(N+j-A)%N
			y2=(N+i-B)%N

			if S[x1][y1]==S[x2][y2]:
				pass
			else:
				flag=-1
				break
	else:
		ans+=1*N


print ans

Submission status: Accepted

Solution code:
n = int(input())

s = []
for _ in range(n):
    s.append(input())
s = s + s

def check(a):
    flag = True
    n = len(a)
    i, j_start = 0, 1
    while flag:
        if i == n - 1:
            break
        for j in range(j_start, n):
            if a[i][j] != a[j][i]:
                flag = False
        i += 1
        j_start += 1
    return flag

ans = 0
for i in range(n):
    if check(s[i:i+n]):
        ans += n
    else:
        pass
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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

count=0

for l in range(N):
    JUDGE=1
    for j in range(N):
        for k in range(j+1,N):
            
            if S[j][k-l]!=S[k][j-l]:
                JUDGE=0
                break
        if JUDGE==0:
            break

    if JUDGE==1:
        count+=1

print(count*N)

Submission status: Accepted

Solution code:
import numpy

N = int(input())

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

S_mat = numpy.mat(S)

ret = 0
for A in range(N):
    A_change_list = list(range(A, N))
    A_change_list.extend(list(range(A)))
    moved = S_mat[A_change_list, :]
    if numpy.all(moved == moved.T):
        ret +=1
print(N*ret)

Submission status: Accepted

Solution code:
n = int(input())

s = []
for i in range(n):
    s.append(input())

result = 0

def judge(board):
    for i in range(n):
        for j in range(i+1, n):
            if board[i][j] != board[j][i]:
                return 0
    return 1

result = 0
for i in range(n):
    board = s[i:] + s[:i]
    result += n * judge(board)

print(result)

Submission status: Accepted

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

def solve(N,S):
  st=[tuple(x) for x in S]
  ch=list(zip(*st))
  ans=0
  for i in range(N):
    if st==ch:
      ans+=N
    st=st[-1:]+st[:-1]
    ch=[row[-1:] + row[:-1] for row in ch]
  return ans

print(solve(N,S))

Submission status: Accepted

Solution code:
N = int(input())
S_list = [input() for _ in range(N)]
cnt = 0


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


for k in range(N):
    S_symmetry = S_list[k:] + S_list[:k]
    if count_symmetry(S_symmetry):
        cnt += 1

print(cnt * N)

Submission status: Accepted

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

def check(B):
    b = B-N
    for i in range(N):
        si = S[i]
        ti = T[b+i]
        if any(si[b+j] != ti[j] for j in range(i)):
            return 0
    return 1

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int(input())
A = [input() for _ in range(n)]
# AT = list(map(''.join,zip(*A)))
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)

Submission status: Accepted

Solution code:
# coding: utf-8
import numpy as np

# AとB(行と列の移動量)を入れ替えた場合も対称になりたつ。
N = int(input())
# numpyを使うため、文字を文字コードの数字に置き換えている(ord関数)
S = [list(map( ord, list(input()) )) for _ in range(N)]
Sarr = np.array(S)
# 配列をひとまとめにしてタイル状に複製して配置
# 行, 列のアクセスインデックスが行、列の大きさを飛び越えると
# それぞれ先頭にインデックスを戻す必要があるところを行列を拡張して対応する
Sext = np.tile(Sarr, [2,1])[:-1,:]
# 拡張は行方向だけで良さそう
#Sext = np.tile(Sarr, [2,2])[:-1,:-1]

cnt = 0
for i in range(N):
    # 行について1つずつずらしたスタート位置で配列を取得し、
    # 転置したものと等しくなっていれば、良い配列
    # 行と列の移動量の差(A-B)が同一のときは対角線成分に対して
    # 線対称を判定する要素の組み合わせが変わらない。
    # そのため、列は探索せずに、N倍すればよい
    ar = Sext[i:i+N, 0:N]
    cnt += N * (ar == ar.T).all()

print(cnt)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

IN = 'sample-1.in'  # sample case for local test
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()

Submission status: Accepted

Solution code:
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
#for n in range(N):
#  isSym = True
#  for i in range(N):
#    for j in range(i,N):
#      if S[i][j] != S[j][i]:
#        isSym = False
#        break
#    if not isSym:
#      break
#  ans += 1 if isSym else 0
#  s = S.pop()
#  S.insert(0, s)
print ans*N

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:


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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# B
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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


"""

Submission status: Accepted

Solution code:
# B

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


def is_good(S):
    # T = []
    # for j in range(N):
    #     ls = []
    #     for i in range(N):
    #         ls.append(S[i][j])
    #     T.append(ls)
    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)