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:
n=int(input())
s=[list(input()) for i in range(n)]


#=2
#s=[['a','b'],['c','a']]
t=[["" for i in range(n)] for i in range(n)]

c=0
for a in range(n):
  for i in range(n):
    for j in range(n):
      ia=i+a
      if ia>=n:
        ia-=n
      t[i][j]=s[ia][j]
  f=0
  for i in range(0,n):
    for j in range(i+1,n):
      if t[i][j]!=t[j][i]:
        f=1
        break
  if f==0:
    c+=1
          
print(c*n)

Submission status: Time Limit Exceeded

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


#=2
#s=[['a','b'],['c','a']]
t=[["" for i in range(n)] for i in range(n)]

c=0
for a in range(n):
  for i in range(n):
    for j in range(n):
      ia=i+a
      if ia>=n:
        ia-=n
      t[i][j]=s[ia][j]
  f=0
  for i in range(n):
    for j in range(n):
      if t[i][j]!=t[j][i]:
        f=1
        break
  if f==0:
    c+=1
          
print(c*n)

Submission status: Time Limit Exceeded

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


#=2
#s=[['a','b'],['c','a']]
t=[["" for i in range(n)] for i in range(n)]

c=0
for a in range(n):
  for b in range(n):
    for i in range(n):
      for j in range(n):
        ia=i+a
        if ia>=n:
          ia-=n
        jb=j+b
        if jb>=n:
          jb-=n
        t[i][j]=s[ia][jb]
    f=0
    for i in range(n):
      for j in range(n):
        if t[i][j]!=t[j][i]:
          f=1
          break
    if f==0:
      c+=1
          
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

def check(a):
    flag = True
    n = len(a)
    for i in range(n):
        for j in range(n):
            if a[i][j] == a[j][i]:
                pass
            else:
                flag = False
    return flag

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

Submission status: Time Limit Exceeded

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(A, B):
    for i in range(N):
        si = S[A+i-N]
        ti = T[B+i-N]
        for j in range(i+1, N):
            if si[B+j-N] != ti[A+j-N]:
                return 0
    return 1

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

Submission status: Time Limit Exceeded

Solution code:
import itertools


def slv(S):
    count = 0
    N = len(S)
    a = 9
    # for a, b in itertools.product(range(len(S)), repeat=2):
    for b in range(N):
        # print(a, b)
        for i, j in itertools.combinations_with_replacement(range(len(S)), 2):
            if S[(i + a) % N][(j + b) % N] != S[(j + a) % N][(i + b) % N]:
                break

        else:
            # print(a, b)
            count += N
    # print(S)
    return count


def main():
    N = int(input())
    S = [input() for _ in range(N)]

    # N = 300
    # S = ['a' * N for _ in range(N)]

    # print(len(A))
    print(slv(S))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    field = []
    for i in range(N):
        input_raw = read_func().strip()
        field.append(input_raw)

##    judge_ij = []
##    judge_arr = [[0 for i in range(N)] for j in range(N)]
##    center = [0 for i in range(N)]
##
##    for i in range(N):
##        if i == 0 and N > 1:
##            center[i] = 0
##        else:
##            center[i] = (center[i - 1] + (N/2 + 1)) % N
##        for j in range(N):
##            judge_arr[i][j] = field[(i - j) % N][j]

    count = 0
    for k in range(N):
        is_good = True
        for i in range(N):
            for j in range(i + 1, N):
                if i != j:
                    if field[i][(j + k) % N] != field[j][(i + k) % N]:
                        is_good = False
                        break;
            if is_good == False:
                break
        if is_good == True:
            count += 1
    print count * N




##    good_list = set([])
##    for i in range(N):
##        is_good = True
##        if N %
##        for k in range(1, N):
##            if judge_arr[i][(center[i] + k) % N] != judge_arr[i][(center[i] - k) % N]:
##                is_good== False;
##                break;
##
##
##        for k in range(N):
##            if judge_arr[k][judge_ij[(i + k) % N][0]] != judge_arr[k][judge_ij[(i + k) % N][1]]:
##                is_good = False
##                break;
##        if is_good == True:
##            good_list.add(i)
##    count = 0
##    for A in range(N):
##        for B in range(N):
##            if (A - B) % N in good_list:
##                count += 1
##    print count



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = [[ord(i) for i in input()] for j in range(n)]
count = 0
for a in range(n):
    arr = np.array(A[a:][:] + A[:a][:])
    arrT = arr.T
    for i in range(n):
        if list(arr[i][i+1:]) != list(arrT[i][i+1:]):
            break
    else:
        count += n
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = [[ord(i) for i in input()] for j in range(n)]
count = 0
for a in range(n):
    arr = np.array(A[a:][:] + A[:a][:])
    if np.allclose(arr,arr.T):
        count += n
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = [[ord(i) for i in input()] for j in range(n)]
count = 0
for a in range(n):
    arr = np.roll(A, a, axis=0)
    arrT = arr.T
    for i in range(n):
        if list(arr[i][i+1:]) != list(arrT[i][i+1:]):
            break
    else:
        count += n
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
A = [[ord(i) for i in input()] for j in range(n)]
count = 0
for a in range(n):
    arr = np.roll(A, a, axis=0)
    if np.allclose(arr,arr.T):
        count += n
print(count)

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(n):
    i_ = (n+i)%n
    s_ = s[i_:] + s[:i_]
    for k in range(n):
        for l in range(k+1, n):
            if s_[k][l] != s_[l][k]:
                break
        else:
            continue
        break
    else:
        cnt += 1

print(cnt*n)

Submission status: Time Limit Exceeded

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

ans = 0
for A in range(N):
    for n in range(N*N):
        i, j = n // N, n % N
        if i >= A: 
            if j >= A:
                if S[i-A][j] != S[j-A][i]:
                    break
            else:
                if S[i-A][j] != S[j-A+N][i]:
                    break
        else:
            if j >= A:
                if S[i-A+N][j] != S[j-A][i]:
                    break
            else:
                if S[i-A+N][j] != S[j-A+N][i]:
                    break
    else:
        ans += N - A

for B in range(1, N):
    for n in range(N*N):
        i, j = n // N, n % N
        if j >= B: 
            if i >= B:
                if S[i][j-B] != S[j][i-B]:
                    break
            else:
                if S[i][j-B] != S[j][i-B+N]:
                    break
        else:
            if i >= B:
                if S[i][j-B+N] != S[j][i-B]:
                    break
            else:
                if S[i][j-B+N] != S[j][i-B+N]:
                    break
    else:
        ans += N - B

print(ans)

Submission status: Time Limit Exceeded

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

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

cnt = 0

for i in range(n):
    i_ = (n+i)%n
    s_ = s[i_:] + s[:i_]
    for k in range(n):
        for l in range(n):
            if s_[k][l] != s_[l][k]:
                break
        else:
            continue
        break
    else:
        cnt += 1

print(cnt*n)

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(n):
    i_ = (n+i)%n
    for k in range(n):
        for l in range(n):
            if s[(i_+k)%n][l] != s[(i_+l)%n][k]:
                break
        else:
            continue
        break
    else:
        cnt += 1

print(cnt*n)

Submission status: Time Limit Exceeded

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

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

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

print(res*n)

Submission status: Time Limit Exceeded

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

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

print(ans * N)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = np.array([list(input()) for i in range(N)])

ans = 0
flg = False
for a in range(N):
    S = np.roll(S, 1, axis=0)
    for i in range(N):
        for j in range(N):
            if S[i][j] != S[j][i]:
                flg = True
                break
        if flg:
            flg = False
            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 i in range(N)])

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

print(ans * N)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations, product

def main():
    grid = [[0] * N for _ in [0] * N]
    cnt = 0
    for i in range(N):
        cnt += judge(0, i, grid)
    print(cnt * N)


def judge(a, b, grid):
    for i, j in product(range(N), repeat=2):
        k = (i + a) % N
        l = (j + b) % N
        grid[k][l] = T[i][j]

    for i, j in combinations(range(N), 2):
        if grid[i][j] != grid[j][i]:
            return 0
    return 1

N = int(input())
T = [list(input()) for _ in [0] * N]
main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for _ in range(n)]
r = 0
for a in range(n):
    flag = True
    sa = [t[a:]+t[:a] for t in s]
    for i in range(n):
        if not(flag):
            break
        for j in range(i+1,n):
            sij = sa[i][j]
            sji = sa[j][i]
            if sij == sji:
                pass
            else:
                flag = False
                break
    if flag:
        r += 1
print(r*n)

Submission status: Time Limit Exceeded

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):
    for B in range(W):
        if check(A, B):
            count += 1
            if 300<=count and ng==0:
                print N*N
                exit()
        else:
            ng += 1
print count * rate

Submission status: Time Limit Exceeded

Solution code:
'''input
5
abcde
fghij
klmno
pqrst
uvwxy
'''


def is_symmetric(board):
    for i in range(N):
        for j in range(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: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

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

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

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

        if flg: ans += 1

    return ans * n


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

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

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    field = []
    for i in range(N):
        input_raw = read_func().strip()
        field.append(input_raw)

##    judge_ij = []
##    judge_arr = [[0 for i in range(N)] for j in range(N)]
##    center = [0 for i in range(N)]
##
##    for i in range(N):
##        if i == 0 and N > 1:
##            center[i] = 0
##        else:
##            center[i] = (center[i - 1] + (N/2 + 1)) % N
##        for j in range(N):
##            judge_arr[i][j] = field[(i - j) % N][j]

    count = 0
    for k in range(N):
        is_good = True
        for i in range(N):
            for j in range(N):
                if i != j:
                    if field[i][(j + k) % N] != field[j][(i + k) % N]:
                        is_good = False
                        break;
            if is_good == False:
                break
        if is_good == True:
            count += 1
    print count * N




##    good_list = set([])
##    for i in range(N):
##        is_good = True
##        if N %
##        for k in range(1, N):
##            if judge_arr[i][(center[i] + k) % N] != judge_arr[i][(center[i] - k) % N]:
##                is_good== False;
##                break;
##
##
##        for k in range(N):
##            if judge_arr[k][judge_ij[(i + k) % N][0]] != judge_arr[k][judge_ij[(i + k) % N][1]]:
##                is_good = False
##                break;
##        if is_good == True:
##            good_list.add(i)
##    count = 0
##    for A in range(N):
##        for B in range(N):
##            if (A - B) % N in good_list:
##                count += 1
##    print count



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def is_symmetric(N, board):
    for i in range(N):
        for j in range(N):
            if board[i][j] != board[j][i]:
                return False
    return True


def generate_board(N, board, a, b):
    new = []
    for i in range(N):
        new.append([None for i in range(N)])

    for i in range(N):
        for j in range(N):
            new[(i + a) % N][(j + b) % N] = board[i][j]

    return new


def solve(N, board):
    ans = 0
    for i in range(2*N - 1):
        if i < N:
            if is_symmetric(N, generate_board(N, board, 0, i)):
                ans += i + 1
        else:
            if is_symmetric(N, generate_board(N, board, 0, i)):
                ans += 2*N - 1 - i
    return ans


N = int(input())
board = []
for i in range(N):
    board.append([j for j in input()])


print(int(solve(N, board)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for i in range(n)]
if all(s[i][j]==s[0][0] for i in range(n) for j in range(n)): print(n**2)
else:
	for i in range(n): s[i] += s[i]
	s,ans = s+s,0
	for k in range(n):
		flag = True
		for k2 in range(k,n+k):
			for j2 in range(n):
				if s[k2][j2]!=s[j2+k][k2-k]:
					flag = False
					break
			if not flag: break
		if flag: ans += n
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = [list(raw_input()) for t in range(N)]
ans = 0
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: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = [list(raw_input()) for t in range(N)]
ans = 0
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
  ans += 1 if isSym else 0
  s = S.pop()
  S.insert(0, s)
print ans*N

Submission status: Time Limit Exceeded

Solution code:
N = int(input())						# 5

#A = list( list(input()) for _ in range(N) )

A=[]
for _ in range(N):
	a=list(input())
	b=list(a[:-1])
	a=a+b
	A.append(a)

#print(A)

for i in range(N-1):
	a=A[i]
	A.append(a)

#for i in range(N*2-1):
#	print(A[i])


count=0
bBreak=False
for i in range(N):
	for j in range(N):
		bBreak=False
		for x in range(N):
			for y in range(N):
				a=A[i+x][j+y]
				b=A[i+y][j+x]
				bBreak = (a!=b)
#				print(i, j, x, y, a, b, bBreak)
				if bBreak: break
			
			if bBreak: break
		else:
			count += 1
#		print("count=", count)

print(count)

Submission status: Time Limit Exceeded

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)
  for j in range(n):
    aa_cij=np.concatenate( [ aa_cij[:,1:], aa_cij[:,[0]] ], axis=1)
    if np.allclose(aa_cij, aa_cij.T ):
      ans+=1 
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
s=[]
g=[0 for i in range(n)]
for i in range(n):
 s.append(input())
k=0
for i in range(n):
 for ii in range(n):
  w=1
  for jj in range(1,n):
   for j in range(jj+1):
    if s[(jj-j+i)%n][(j+ii)%n]!=s[(j+i)%n][(jj-j+ii)%n]:
        w=0
  k+=w      


print(k)

Submission status: Time Limit Exceeded

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)
  for j in range(n):
    aa_cij=np.concatenate( [ aa_cij[:,1:], aa_cij[:,[0]] ], axis=1)
    if np.allclose(aa_cij - aa_cij.T, z ):
      ans+=1 
print(ans)

Submission status: Time Limit Exceeded

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):
    for j in range(n):
        p = s[i:i + n, j:j + n]
        if (p == p.T).all():
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
s=[chr(i) for i in range(97, 97+26)]

a=[]    
for i in range(n):
  a.append([s.index(i)+1  for i in list(input())])

aa_cij=np.array(a)
z=np.zeros((n,n))
ans=0
for i in range(n):
  aa_cij=np.concatenate( [ aa_cij[1:,:], aa_cij[[0],:] ], axis=0)
  for j in range(n):
    aa_cij=np.concatenate( [ aa_cij[:,1:], aa_cij[:,[0]] ], axis=1)

    #print(aa_cij)
    #print(aa_cij.T)
    #print(np.diag(np.diag(at)))
    #print(at-np.diag(np.diag(at)))
    if np.allclose(aa_cij-aa_cij.T,z):
      ans+=1 
print(ans)

Submission status: Time Limit Exceeded

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

Solution code:
from itertools import product

n = int(input())
s = []

for i in range(n):
    s.append(list(input()))
s = [l*2 for l in s] * 2

count = 0
for a in range(n):
    for b in range(n):
        t = [l[b:b+n] for l in s[a:a+n]]
        for i, j in product(range(n), range(n)):
            if t[i][j] != t[j][i]:
                break
        else:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [ list(input()) for i in range(N) ]
ans = 0
for A in range(N):
    for B in range(N):
        flag = False
        for i in range(A,N+A):
            for j in range(B,B+ i + 1 - A):
                if S[i % N][j % N] != S[(j - B + A  + 2 * N  ) % N][( i - A +  B + 2 * N ) % N]:
                    flag = True

                    break
            if flag:
                break
        if not flag:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[]
for i in range(n):
	s.append(input().strip())

ans=0
for k in range((-1)*n+1,n):
	count=0
	for i in range(n):
		for j in range(n):
			if s[i][j]!=s[(j-k+n)%n][(i+k+n)%n]:
				break
			count+=1
	if count==n*n:
		if k<0:
			ans+=n+k
		else:
			ans+=n-k

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=[]
for i in range(n):
	s.append(input().strip())

ans=0
for k in range((-1)*n+1,n):
	count=0
	for i in range(n):
		for j in range(n):
			if s[i][j]==s[(j-k+n)%n][(i+k+n)%n]:
				count+=1
	if count==n*n:
		if k<0:
			ans+=n+k
		else:
			ans+=n-k

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import itertools
n = int(input())
ban = [input() for i in range(n)]
ans = 0
for a in range(n):
    for b in range(n):
        flag = a
        for (i,j) in list(itertools.product(range(n),repeat=2)):
            if i+a >= n:
                x = i+a-n
            else:
                x = i+a
            if j+b >= n:
                y = j+b-n
            else:
                y = j+b
            if j+a >= n:
                X = j+a-n
            else:
                X = j+a
            if i+b >= n:
                Y = i+b-n
            else:
                Y = i+b
            #print(a,b,x,y,ban[x][y],ban[X][Y])
            if ban[x][y] != ban[X][Y]:
                flag = -1
                break
        #print(a,b)
        if flag == a:
            ans += 1
            #print(a,b)
            break
if [ban[0][0]*n for x in range(n)] == ban:
    print(ans*n)
else:
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
S = [str(input()) for _ in range(N)]


def isGoodTable(table, A, B):
    for i in range(N):
        for j in range(i, N):
            if table[i][j] != table[j][i]:
                return False
    return True


def shiftTable(table, A, B):
    shifted = []
    for i in range(N):
        shifted.append([])
        for j in range(N):
            shifted[i].append(table[(i + A) % N][(j + B) % N])
    return shifted


def countGoodShift(table):
    count = 0
    for a in range(N):
        for b in range(N):
            shifted = shiftTable(table, a, b)
            if isGoodTable(shifted, a, b):
                count += 1
    return count


print(countGoodShift(S))

Submission status: Time Limit Exceeded

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

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

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

print(res)