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

Submission status: Time Limit Exceeded

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

"""
AGC023 B
"""

from itertools import product

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

ans = 0
for k in range(n):
    if all((sli[i][j] == sli[(j+k) % n][(i-k) % n] for i, j in product(range(n), repeat=2))):
        ans += n

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import sys
N = int(input())

grid = sys.stdin.read().split('\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: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())

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

res = 0
for a in range(N):
    for x, y in combinations(range(N), r=2):
        if S[(x-a)%N][y] != S[(y-a)%N][x]:
            break
    else:
        res += N
            
print(res)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())

S = []

for _ in range(N):
    S.append(input())

res = 0
for a in range(N):
    for x, y in combinations(range(N), r=2):
        if S[(x-a)%N][y] != S[(y-a)%N][x]:
            break
    else:
        res += N
            
print(res)

Submission status: Time Limit Exceeded

Solution code:
n=input()
s=''
for i in range(n):
	s+=raw_input()
s+=s
result=0
for shift in xrange(n):
	ok=True
	for i in xrange(n):
		if not ok:
			break
		j=0
		while j<n:
			if s[(i+shift)*n+j]!=s[(j+shift)*n+i]:
				ok=False
				break
			j+=1
	if ok:
		result+=n
print result

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=input()
s=[]
for i in range(n):
	s+=[raw_input()]
result=0
for shift in range(n):
	ok=True
	for i in range(n):
		for j in range(n):
			if s[i][j]!=s[j][i]:
				ok=False
				break
	if ok:
		result+=n
	s=s[1:]+s[:1]
print result

Submission status: Time Limit Exceeded

Solution code:
n=input()
s=[]
for i in range(n):
	s+=[raw_input()]
result=0
for shift in range(n):
	ok=True
	for i in range(n):
		for j in range(n):
			if s[(i+shift)%n][j]!=s[(j+shift)%n][i]:
				ok=False
				break
	if ok:
		result+=n
print result

Submission status: Time Limit Exceeded

Solution code:
n=input()
s=[]
for i in range(n):
	s+=[raw_input()]
result=0
for shift in range(n):
	ok=True
	for i in range(n):
		for j in range(n):
			if s[i][j]!=s[j][i]:
				ok=False
				break
	if ok:
		result+=n
	for i in range(n):
		s[i]=s[i][1:]+s[i][0:1]
print result

Submission status: Time Limit Exceeded

Solution code:
n=input()
s=[]
for i in range(n):
	s+=[raw_input()]
result=0
for shift in range(n):
	ok=True
	for i in range(n):
		for j in range(n):
			if s[i][j]!=s[j][i]:
				ok=False
	if ok:
		result+=n
	for i in range(n):
		s[i]=s[i][1:]+s[i][0:1]
print result

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
for k in range(N):
    f = True
    for i in range(N):
        for j in range(i + 1, N):
            if S[i][(j + k) % N] != S[j][(i + k) % N]:
                f = False
                break

        if not f:
            break
    if f:
        ans += N
print(ans)

Submission status: Time Limit Exceeded

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

if n == 1:
    print('1')
    exit()

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

print(ans * n)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[list(input()) for i in range(N)]
def issym(IL):
    for i in range(N):
        for j in range(i+1,N):
            if IL[i][j]!=IL[j][i]:
                return False
    return True
def changelist(A,B):
    T=[["" for i in range(N)] for j in range(N)]
    for i in range(N):
        for j in range(N):
            T[i][j]=S[(i+A)%N][(j+B)%N]
    return T
ans=0
for i in range(N):
    L=changelist(i,0)
    if issym(L):
        ans+=N
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = [[c for c in input()] for _ in range(n)]

def chechTable(A, Shift):
    for i in range(n):
        for j in range(i):
            i_shift = i + Shift if i + Shift < n else i + Shift - n
            j_shift = j + Shift if j + Shift < n else j + Shift - n
            
            if A[i_shift][j] != A[j_shift][i]:
                return False
    return True

count = 0
for i in range(n):
    if chechTable(A, i):
        count += 1
print(count * n)

Submission status: Time Limit Exceeded

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()
    for b in range(n):
      for y in range(n):
        mp[y].rotate()
      ans += check(mp)
  print(ans)
  
main()

Submission status: Time Limit Exceeded

Solution code:
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 = [input() for _ in range(n)]
ans = 0
for a in range(n):
  mp.insert(0, mp.pop())
  for b in range(n):
    for y in range(n):
      mp[y] = mp[y][-1] + mp[y][:-1]
    ans += check(mp)
print(ans)

Submission status: Time Limit Exceeded

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

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):
        for k in range(j+1, N):
            if S[j][(k+i)%N] == S[k][(j+i)%N]:
                g += 1
    if g == r:
        ans += 1
    for j in range(N-1):
        for k in range(N-1):
            if S[(1+k+j)%N][(j+i)%N] == S[j][(j+i+k+1)%N]:
                g += 1
            if S[(j+k)%N][(j+i)%N] == S[j][(j+i+k)%N]:
                g -= 1
        if g == r:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 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 S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = [[c for c in S()] for _ in range(n)]
    r = 0
    for i in range(n):
        b = [[a[ii][(j+i)%n] for j in range(n)] for ii in range(n)]
        f = True
        for ii in range(n):
            for jj in range(ii+1,n):
                if b[ii][jj] != b[jj][ii]:
                    f = False
                    break
            if not f:
                break
        if f:
            r += 1

    return r * n




print(main())

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

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

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

k = S[0]
if set(k) == 1: 
    for i in S:
        if i != k:
            break
    print(N**2)

else:
    count = 0
    for _ in range(N):
        s = S.pop(0)
        S.append(s)
        a = 0
        if check():
            count += N
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())

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

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

count = 0
for _ in range(N):
    s = S.popleft()
    S.append(s)
    a = 0
    if check():
        count += N
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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

count = 0
for _ in range(N):
    s = S.pop(0)
    S.append(s)
    a = 0
    if check():
        count += N
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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

count = 0
for _ in range(N):
    s = S.pop(0)
    S.append(s)
    a = 0
    if check():
        count += N
print(count)

Submission status: Time Limit Exceeded

Solution code:

N = int(input())

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

count = 0
for _ in range(N):
    s = S.pop(0)
    S.append(s)
    a = 0
    for i in range(N):
        for j in range(N):
            if S[i][j] == S[j][i]:
                a += 1
    if a == N**2:
        count += N
print(count)

Submission status: Time Limit Exceeded

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

count = np.array([[shift(array_int).is_sym(i,j) for i in range(N)] for j in range(N)])
print(count.sum())

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s1 = [[i for i in input()] for j in range(n)]
count = int(0)
for a in range(n):
  flag = 0
  for i in range(n):
    for j in range(n):
      if s1[i][(j+a)%n] != s1[j][(i+a)%n]:
        flag += 1
        break
  if flag == 0:
    count += 1
print(count* n)

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):
        shifted = shiftTable(table, a, 0)
        if isGoodTable(shifted, a, 0):
            count += 1
    return count * N


print(countGoodShift(S))

Submission status: Time Limit Exceeded

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

cnt = 0
for k in range(N):
    for i in range(0,N-1):
        for j in range(i+1,N):
            if l[i][j] != l[j][i]:
                break
        else :
            continue
        break
    else :
        cnt += N
    tmp = l[0]
    for i in range(N-1):
        l[i] = l[i+1]
    l[N-1] = tmp
    
    
print(cnt)

Submission status: Time Limit Exceeded

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

def yoi(s, b):
  for i in range(n):
    for j in range(i):
      if s[i][(j + b) % n] != s[j][(i + b) % n]:
        return 0
  return 1

def not_eq(s, b, n, i, j):
  return s[i][(j + b) % n] != s[j][(i + b) % n]

def yoi2(s, b):
  if sum(not_eq(s, b, n, i, j) for i in range(n) for j in range(i)) > 0:
        return 0
  return 1

sol = n * sum(yoi2(s, b) for b in range(n))
print(sol)

Submission status: Time Limit Exceeded

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


def eq(s, b, n, i, j):
  return s[i][(j + b) % n] != s[j][(i + b) % n]

def yoi2(s, b):
  for i in range(n):
    for j in range(i):
      if eq(s, b, n, i, j):
        return 0
  return 1

sol = n * sum(yoi2(s, b) for b in range(n))
print(sol)

Submission status: Time Limit Exceeded

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

def yoi(s, b):
  for i in range(n):
    for j in range(i):
      if s[i][(j + b) % n] != s[j][(i + b) % n]:
        return 0
  return 1

sol = n * sum(yoi(s, b) for b in range(n))
print(sol)

Submission status: Time Limit Exceeded

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

def yoi(s, b):
  for i in range(n):
    for j in range(i):
      if s[i % n][(j + b) % n] != s[j % n][(i + b) % n]:
        return 0
  return 1


sol = n * sum(yoi(s, b) for b in range(n))
print(sol)

Submission status: Time Limit Exceeded

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

def yoi(s, b):
  for i in range(n):
    for j in range(i+1, n):
      if s[i % n][(j + b) % n] != s[j % n][(i + b) % n]:
        return 0
  return 1

sol = sum(yoi(s, b) for b in range(n))
print(sol * n)

Submission status: Time Limit Exceeded

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

def yoi(s, a, b):
  for i in range(n):
    for j in range(i+1, n):
      if s[(i + a) % n][(j + b) % n] != s[(j + a) % n][(i + b) % n]:
        return 0
  return 1

sol = sum(yoi(s, 0, b) for b in range(n))
print(sol * n)

Submission status: Time Limit Exceeded

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

def yoi(s, a, b):
  for i in range(n):
    for j in range(i+1, n):
      if s[(i + a) % n][(j + b) % n] != s[(j + a) % n][(i + b) % n]:
        return 0
  return 1

sol = sum(yoi(s, a, b) for a in range(n) for b in range(n))
print(sol)

Submission status: Time Limit Exceeded

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

def yoi(s, a, b):
  for i in range(n):
    for j in range(n):
      if s[(i + a) % n][(j + b) % n] != s[(j + a) % n][(i + b) % n]:
        return 0
  return 1

sol = sum(yoi(s, a, b) for a in range(n) for b in range(n))
print(sol)

Submission status: Time Limit Exceeded

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

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


print ans

Submission status: Time Limit Exceeded

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

A=0
ans=0
for B in range(N):
	T={}

	for i in range(N):
		for j in range(N):
			T[(i,j)]=S[(i+A)%N][(j+B)%N]

	flag=0
	for i in range(N):
		if flag==-1: break
		for j in range(N):
			if T[ (i,j) ]==T[ (j,i) ]:
				pass
			else:
				flag=-1
				break
	else:
		ans+=1*N


print ans

Submission status: Time Limit Exceeded

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

A=0
ans=0
for B in range(N):
	T={}

	for i in range(N):
		for j in range(N):
			T[(i,j)]=S[(i+A)%N][(j+B)%N]

	flag=0
	for i in range(N):
		if flag==-1: break
		for j in range(N):
			if T[ (i,j) ]==T[ (j,i) ]:
				pass
			else:
				flag=-1
				break
	else:
		ans+=1*N


print ans

Submission status: Time Limit Exceeded

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

A=0
ans=0
for B in range(N):
	T=[ ["" for j in range(N)] for i in range(N)]

	for i in range(N):
		for j in range(N):
			T[i][j]=S[(i+A)%N][(j+B)%N]

	flag=0
	for i in range(N):
		if flag==-1: break
		for j in range(N):
			if T[i][j]==T[j][i]:
				pass
			else:
				flag=-1
				break
	else:
		ans+=1*N


print ans

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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 j + B < N:
            #     b = j + B
            # else:
            #     b = j + B - N
            # if i + B < N:
            #     d = i + B
            # else:
            #     d = i + B - N
            if S[i][j+B-N] != S[j][i+B-N]:
                return 0
    return 1

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

Submission status: Time Limit Exceeded

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 j + B < N:
                b = j + B
            else:
                b = j + B - N
            if i + B < N:
                d = i + B
            else:
                d = i + B - N
            if S[i][b] != S[j][d]:
                return 0
    return 1

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

Submission status: Time Limit Exceeded

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 i + A < N:
                a = i + A
            else:
                a = i + A - N
            if j + B < N:
                b = j + B
            else:
                b = j + B - N
            if j + A < N:
                c = j + A
            else:
                c = j + A - N
            if i + B < N:
                d = i + B
            else:
                d = i + B - N
            if S[a][b] != S[c][d]:
                return False
    return True

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

Submission status: Time Limit Exceeded

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(N):
            if i + A < N:
                a = i + A
            else:
                a = i + A - N
            if j + B < N:
                b = j + B
            else:
                b = j + B - N
            if j + A < N:
                c = j + A
            else:
                c = j + A - N
            if i + B < N:
                d = i + B
            else:
                d = i + B - N
            if S[a][b] != S[c][d]:
                return False
    return True

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

Submission status: Time Limit Exceeded

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

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):
            
            kk=(k-l)%N
            jj=(j-l)%N
            if S[j][kk]!=S[k][jj]:
                JUDGE=0
                break
        if JUDGE==0:
            break

    if JUDGE==1:
        count+=1

print(count*N)

Submission status: Time Limit Exceeded

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)))
    for B in range(N):
        B_change_list = list(range(B, N))
        B_change_list.extend(list(range(B)))

        moved = S_mat[A_change_list, :][:,B_change_list]
        if numpy.all(moved == moved.T):
            ret +=1
print(ret)

Submission status: Time Limit Exceeded

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):
            
            kk=(k-l)%N
            jj=(j-l)%N
            if S[j][kk]!=S[k][jj]:
                JUDGE=0
                #print([l,j,k,jj,kk])


    if JUDGE==1:
        count+=1

    

print(count*N)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

result = 0
for offset in range(n):
    is_symmetry = True
    for i in range(n):
        for j in range(i+1, n):
            if s[i][(j+offset)%n] != s[j][(i+offset)%n]:
                is_symmetry = False
                break
        if not is_symmetry:
            break
    if is_symmetry:
        result += n

print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

result = 0
for offset in range(n):
    # print('offset: %i' % offset)
    is_symmetry = True
    for i in range(n):
        for j in range(i+1, n):
            # print('s[%i][%i] = %c' % (i, (j+offset)%n, s[i][(j+offset)%n]))
            # print('s[%i][%i] = %c' % (j, (i+offset)%n, s[j][(i+offset)%n]))
            # print('--')
            if s[i][(j+offset)%n] != s[j][(i+offset)%n]:
                is_symmetry = False
                break
        if not is_symmetry:
            break
    if is_symmetry:
        result += n
        # print('offset: %i' % offset)

print(result)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
B=[]
for i in range(N):
  s=input()
  s*=2
  B.append(s)

def solve(a,b):
  for i in range(N):
    for j in range(i+1,N):
      if B[a+i][b+j]!=B[a+j][b+i]:
        return False
  return True

ans=0
for i in range(N):
  if solve(0,i):
    ans+=N

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
B=[]
for i in range(N):
  s=input()
  s*=2
  B.append(s)
for i in range(N):
  B.append(B[i])

def solve(a,b):
  for i in range(N):
    for j in range(i+1,N):
      if B[a+i][b+j]!=B[a+j][b+i]:
        return False
  return True

ans=0
for i in range(N):
  if solve(i,0):
    ans+=N

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

cnt = 0
S_list = [input() for _ in range(N)]


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

Solution code:
N = int(input())

cnt = 0
S_list = [list(input()) for _ in range(N)]
S_symmetry = [['' for _ in range(N)] for _ in range(N)]


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


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

print(cnt * N)

Submission status: Time Limit Exceeded

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


def inp():
    N = int(input())
    S = []
    for i in range(N):
        s = str(input())
        s = [str(ss) for ss in s]
        S.append(s)
    return N, S

def change(S, N, b):
    T = []
    for i in range(N):
        t = []
        s = S[i]
        for j in range(N):
            if j + b >= N:
                t.append(s[j+b-N])
            else:
                t.append(s[j+b])
        T.append(t)
    return T


def check(S, N):
    for i in range(N):
        for j in range(i, N):
            if S[i][j] == S[j][i]:
                continue
            else:
                return False
    return True

def main():
    N, S = inp()
    count = 0
    for i in range(N):
        T = change(S, N, i)
        if check(T, N):
            count += 1
    print(count*N, end='')


if __name__ == '__main__':
    main()

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 i in range(N):
        flag = False
        for j in range(i):  # i > j
            if i >= A: 
                if j >= A:
                    if S[i-A][j] != S[j-A][i]:
                        flag = True
                        break
                else:   # j < A
                    if S[i-A][j] != S[j-A+N][i]:
                        flag = True
                        break
            else:   # i < A
                if j >= A:
                    if S[i-A+N][j] != S[j-A][i]:
                        flag = True
                        break
                else:
                    if S[i-A+N][j] != S[j-A+N][i]:
                        flag = True
                        break
        if flag:
            break
    else:
        ans += N - A

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

print(ans)

Submission status: Time Limit Exceeded

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


#n=300
#s=[['a' for i in range(n)] for j in range(n)]
t=[["" for i in range(n)] for i in range(n)]

c=0
for a in range(n):
  f=0
  for i in range(n):
    ia=i+a
    if ia>=n:
      ia-=n
    for j in range(i+1,n):
      ja=j+a
      if ja>=n:
        ja-=n
      if s[ia][j]!=s[ja][i]:
        f=1
        break
    if 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)]


#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):
  f=0
  for i in range(n):
    ia=i+a
    if ia>=n:
      ia-=n
    for j in range(n):
      ja=j+a
      if ja>=n:
        ja-=n
      if s[ia][j]!=s[ja][i]:
        f=1
        break
    if f==1:
      break
  if f==0:
    c+=1
print(c*n)