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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [list(input()) for _ in range(n)]
ans = 0
alphcnt = [0] * 26
for i in range(n):
    for j in range(n):
        alphcnt[ord(s[i][j]) - 97] += 1
if max(alphcnt) == pow(n, 2):
    print(pow(n, 2))
    exit()
for k in range(n):
    c = 1
    for i in range(n - 1):
        for j in range(i + 1, n):
            if not s[(i + k) % n][j] == s[(j + k) % n][i]:
                c = 0
                break
        if c == 0:
            break
    if c == 1:
        ans += n
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [list(ord(e) - ord("a") for e in input()) for _ in range(n)]

ans = 0
for b in range(n):
    t = [[-1] * n for _ in range(n)]
    for hi in range(n):
        for wi in range(n):
            nh = hi
            nw = (wi + b) % n
            t[nh][nw] = s[hi][wi]

    bl = True
    for i in range(n):
        for j in range(n):
            if t[i][j] != t[j][i]:
                bl = False

    ans += bl

ans *= n
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# a b c
# b e d
# c d f
# について、
#  a  b  c [a  b]
#  b  e  d [b  e]
#  c  d  f [c  d]
# [a  b  c  a  b]
# [b  e  d  b  e]
# 左上の1,1から始まるものが条件を満たすなら、
# 2,2から始まるものも3,3から始まるものも条件を満たす
# 一つ条件を満たすのであれば、N個条件を満たすことになる。
# 従い、以下を順にチェックして答えをN倍すれば良い
#  a  b  c [a  b]
#  b  e  d [b  e]
#  c  d  f [c  d]

import sys
readline = sys.stdin.readline

N = int(readline())
grid = [None] * N
for i in range(N):
  S = readline().rstrip()
  grid[i] = S + S[:-1]
  
# 左上の頂点を(0,0)~(N-1,0)の全てをチェックする
ans = 0
for p in range(N):
  # 左上の頂点はp
  ok = True
  for i in range(N):
    for j in range(i+1):
      if grid[i][j + p] != grid[j][i + p]:
        ok = False
        break
    if not ok:
      break
  if ok:
    ans += 1

print(ans * N)

Submission status: Time Limit Exceeded

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

def main():
  n = int(input())
  board = []
  for _ in range(n):
    board.append(list(input().strip()))
  
  def judge(ia):
    for i in range(n):
      for j in range(i+1, n):
        if board[(i+ia)%n][j] != board[(j+ia)%n][i]:
          return False
    return True
  
  cnt = 0 
  for ia in range(n):
    if judge(ia):
      cnt += 1
  print(n * cnt)
  
if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
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):
    flag=True
    for j in range(0,N):
        for k in range(0,N):
            flag=flag&(S[(k+i)%N][j]==S[(j+i)%N][k])
    ans+=N*int(flag)

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
#023_B
n = int(input())
s = [input() * 2 for _ in range(n)]
ans = 0
for b in range(0, n):
    flg = True
    for i in range(n):
        for j in range(n):
            if s[i][b+j] != s[j][b+i]:
                flg = False
                break
    if flg:
        ans += 1

print(ans * n)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
    a = 1
    for x in range(n):
        if any(s[(x+i)%n][y] != s[(y+i)%n][x] for y in range(x, n)):
            a = 0
            break
    result += a
print(result*n)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
    h = True
    for x in range(n):
        if all(s[(x+i)%n][y]==s[(y+i)%n][x] for y in range(x, n)):
            continue
        else:
            h = False
            break
    if h:
        result += 1
print(result*n)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for _ in range(n)]
result = 0
for i in range(n):
    h = True
    for x in range(n):
        for y in range(x, n):
            if s[(x+i)%n][y]==s[(y+i)%n][x]:
                continue
            else:
                h = False
                break
        if h==False:
            break
    if h:
        result += 1
print(result*n)

Submission status: Time Limit Exceeded

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def is_sym(S):
    N = len(S)
    for i in range(N):
        for j in range(N):
            if S[i][j] != S[j][i]:
                return False
    return True
def main():
    N=I()
    S=[ST() for _ in range(N)]
    ans = 0
    for A in range(N):
        S=S[1:] + S[:1]
        if(is_sym(S)):
            ans += N
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(N):
        for k in range(N):
            if S[(i+k)%N][j]!=S[i][(j+k)%N]:
                is_same[i][j] = False

ans = 0

for i in range(N):
    for j in range(N):
        flag = True
        
        for k in range(N):
            flag &= is_same[(i+k)%N][(j+k)%N]
        
        if flag:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [input() for i in range(N)]
F = 0
for i in range(N):
  p = 1
  for j in range(N):
    for k in range(N):
      if S[(j + i) % N][k] != S[(k + i) % N][j]:
        p = 0
        break
    if p == 0:
      break
  F += N * p
print(F)

Submission status: Time Limit Exceeded

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

def find_symmetries(Map, m, A, B):
  p = 0
  for i in range(m):
    for j in range(m):
      if Map[(i + A) % m][(j + B) % m] != Map[(j + A) % m][(i + B) % m]:
        p = 1
        break
    if p == 1:
      break
  return 1 - p
F = 0
for i in range(N):
  F += N * find_symmetries(S, N, i, 0)
print(F)

Submission status: Time Limit Exceeded

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

def find_symmetries(Map, m, A, B):
  p = 0
  for i in range(m):
    for j in range(m):
      if Map[(i + A) % m][(j + B) % m] != Map[(j + A) % m][(i + B) % m]:
        p = 1
        break
    if p == 1:
      break
  return 1 - p
F = 0
for i in range(N):
  for j in range(N):
    F += find_symmetries(S, N, i, j)
print(F)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def is_symmetric(a):
    return np.array_equal(a, a.T)

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

    S = np.array(S)

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

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
I=input
N=int(I())
X=range(N)
S=[I()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)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
fie = [list(input())for i in range(n)]

ans = 0
for i in range(n):
        for j in range(n):
                for k in range(j, n):
                        if fie[j][(i+k)%n]!=fie[k][(i+j)%n]: break
                else: continue
                break
        else: ans += 1
print(ans*n)

Submission status: Time Limit Exceeded

Solution code:
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)
S = S.tolist()


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


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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
#input = sys.stdin.readline

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

Submission status: Time Limit Exceeded

Solution code:
import sys
#input = sys.stdin.readline

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(N):
                if s[x+i][y] != s[y+i][x]:
                    b = False
        if b:
            ans += N
    
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:



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

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

    """
    あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
    """

    cnt = 0
    for a in range(N):
        S2 = [["" for _ in range(N)]  for _ 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()

Submission status: Time Limit Exceeded

Solution code:


N = int(input())

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

"""
あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
"""

cnt = 0
for a in range(N):
    S2 = [["" for _ in range(N)]  for _ 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)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
s = []
for i in range(n):
    s.append(ins())
d = [ [0]*n for i in range(n) ]
e = [ [0]*n for i in range(n) ]
for i in range(n):
    for j in range(n):
        m = (n-1)//2
        dend = m
        eend = m-1 if n%2==1 else m
        dd = 0
        for k in range(1,dend+1):
            if s[(i+k)%n][(j-k)%n]==s[(i-k)%n][(j+k)%n]:
                dd = k
            else:
                break
        d[i][j] = dd
        ee = -1
        for k in range(eend+1):
            if s[(i-k)%n][(j+k)%n]==s[(i+1+k)%n][(j-1-k)%n]:
                ee = k
            else:
                break
        e[i][j] = ee
if DBG:
    print('d')
    for i in range(n):
        print(d[i])
    print('e')
    for i in range(n):
        print(e[i])

cnt = 0
for i in range(n):
    for j in range(n):
        ok = True
        for k in range(n):
            if d[(i+k)%n][(j+k)%n] < min(k,n-1-k):
                #ddprint(f'setfd k {k}')
                ok = False
                break
        for k in range(n-1):
            if e[(i+k)%n][(j+1+k)%n] < min(k,n-2-k):
                #ddprint(f'setfe k {k}')
                ok = False
                break
        #ddprint(f'i {i} j {j} ok {ok}')
        if ok:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N):
    x = i
    y = 0
    
    for j in range(N**2):
        dx = j//N
        dy = j%N
        
        if S[(x+dx)%N][(y+dy)%N]!=S[(x+dy)%N][(y+dx)%N]:
            break
    else:
        ans += N
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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)

Submission status: Time Limit Exceeded

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

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)
#print("yoko:", yoko)
#print("tate:", tate)

answer = 0
yi, ti = 0, 0
for y in range(N):
  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 += 1
    
    # 左にずらす
    for i in range(N):
      tate[i] = (tate[i] << 5 | tate[i] >> (N - 1) * 5) & (2 ** (5 * N) - 1)
    yi += 1
    #print("tate:", tate)

  # 上にずらす
  for i in range(N):
    yoko[i] = (yoko[i] << 5 | yoko[i] >> (N - 1) * 5) & (2 ** (5 * N) - 1)
  ti += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=np.array([list(input()) for i in range(n)])
l=np.zeros((n,n),dtype=np.str)
ans=0
for i in range(n):
    l[:i]=s[n-i:n]
    l[i:]=s[:n-i]
    t=False
    for x in range(n):
        for y in range(x+1,n):
            if l[x][y]!=l[y][x]:
                t=True
                break
        if t:
            break
    if not t:
        ans+=n

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    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)

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

Submission status: Time Limit Exceeded

Solution code:
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()] * 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
for i in range(2*N):
    for j in range(2*N):
        table[T[i][j]].add((i, j))

for i in range(N):
    for j in range(N):
        for x, y in table[T[i][j]]:
            for k in range(2):
                for l in range(2):
                    idx = i + N*k
                    idy = j + N*l
                    if abs(x-idx) < N and abs(x-idx) == abs(y-idy):
                        A = min(x, idx)
                        B = min(y, idy)
                        if A >= N:
                            A -= N
                        if B >= N:
                            B -= N
                        table2[i][j].add((A, B))


ans = set()
for i in range(N):
    for j in range(N):
        if i == 0 and j == 0:
            ans = table2[i][j]
        else:
            ans = ans & table2[i][j]

s = np.array(T)
count = 0
for A, B in ans:
    tmp = s[A:A+N, B:B+N]
    if (tmp != tmp.T).any() == False:
        count += 1


print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
N = int(input())
S = [list(input().rstrip()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*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
s = np.array(T)
for A in range(N):
    for B in range(N):
        tmp = s[A:A+N, B:B+N]
        if (tmp != tmp.T).any() == False:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = [list(input()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*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
s = np.array(T)
for A in range(N):
    for B in range(N):
        tmp = s[A:A+N, B:B+N]
        if (tmp != tmp.T).any() == False:
            ans += 1


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = [list(input()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*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
s = np.array(T)
for A in range(N):
    for B in range(N):
        tmp = s[A:A+N, B:B+N]

        if (tmp == tmp.T).all():
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

T = [[""] * N for _ in range(N)]
ans = 0
for A in range(N):
    flag = True
    for i in range(N):
        for j in range(N):
            T[(i+A)%N][j] = S[i][j]
    for i in range(N):
        for j in range(N):
            flag &= T[i][j] == T[j][i]
    if flag:
        ans += N

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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):
  for j in range(n):
    M = S[:, [(l+j)%n for l in range(n)]]
    L = M[[(k+i)%n for k in range(n)], :]
    if (L==L.T).all():
      ans += 1 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

#実際に書き込む
N = ri()
S = [rs() for _ in range(N)]
answer = 0
a = 0
for b in range(N):
    bool = True
    for i in range(N):
        for j in range(i+1, N):
            y1 = i
            x1 = (j+b)%(N)
            y2 = j
            x2 = (i+b)%(N)
            if S[y1][x1] != S[y2][x2]:
                bool = False
                break
        else:
            continue
        break
    else:
        if bool:
            #print(a, b, i, j, y1, x1, y2, x2)
            answer += 1
answer *= N
print(answer)

#16

Submission status: Time Limit Exceeded

Solution code:
import sys

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

#実際に書き込む
N = ri()
S = [rs() for _ in range(N)]
answer = 0
a = 0
for b in range(N):
    bool = True
    for i in range(N):
        for j in range(i+1, N):
            y1 = (i+a)%(N)
            x1 = (j+b)%(N)
            y2 = (j+a)%(N)
            x2 = (i+b)%(N)
            
            if S[y1][x1] != S[y2][x2]:
                
                bool = False
                break
        else:
            continue
        break
    else:
        if bool:
            #print(a, b, i, j, y1, x1, y2, x2)
            answer += 1
answer *= N
print(answer)

#16

Submission status: Time Limit Exceeded

Solution code:
import sys

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

#実際に書き込む
N = ri()
S = [rs() for _ in range(N)]
answer = 0
for a in range(N):
    for b in range(N):
        bool = True
        for i in range(N):
            for j in range(i+1, N):
                y1 = (i+a)%(N)
                x1 = (j+b)%(N)
                y2 = (j+a)%(N)
                x2 = (i+b)%(N)
                
                if S[y1][x1] != S[y2][x2]:
                    
                    bool = False
                    break
            else:
                continue
            break
        else:
            if bool:
                #print(a, b, i, j, y1, x1, y2, x2)
                answer += 1
print(answer)

#16

Submission status: Time Limit Exceeded

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

import numpy as np
# print(np.array(arr))

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
            # print(l,k,i)
            # print(arr[l][k],arr[k][l])
            if arr[l][k] != arr[m][j]:
                ok = False
                break

    # print("####")
    if ok:
        ans += n



print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def s():
	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)for a,b in zip(r+[0]*n,[0]*~-n+r)))
s()

Submission status: Time Limit Exceeded

Solution code:
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)for a,b in zip(r+[0]*n,[0]*~-n+r)))

Submission status: Time Limit Exceeded

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

T = [S[i] * 2 for i in range(N)]
T = T + T

def check(i, j) :
    for x in range(N) :
        for y in range(x, N) :
            if T[i+y][j+x] != T[i+x][j+y] :
                return False
    return True

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

Submission status: Time Limit Exceeded

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