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:
def main():
    import numpy as np
    c=np.concatenate
    n=int(input())
    s=np.array([np.array(list(input())) for _ in [0]*n])
    cnt=0
    for a in range(n):
        for i in range(n):
            ia_n=(i+a)%n
            if not (c([s[i,i+a:n],s[i,:a]])==c([s[i:n-a,ia_n],s[n-a:n,ia_n]])).all():
                break
        else:
            cnt+=1
    print(n*cnt)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import numpy as np
    n=int(input())
    s=np.array([np.array(list(input())) for _ in [0]*n])
    cnt=0
    for a in range(n):
        for i in range(n):
            ia_n=(i+a)%n
            if not (s[i,i+a:n]==s[i:n-a,ia_n]).all():
                break
            elif not (s[i,:a]==s[n-a:n,ia_n]).all():
                break
        else:
            cnt+=1
    print(n*cnt)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    cnt=0
    for a in range(n):
        flag=0
        for i in range(n):
            ia_n=(i+a)%n
            for j in range(i,n-a):
                if s[i][j+a]!=s[j][ia_n]:
                    flag=1
                    break
            else:
                for j in range(n-a,n):
                    if s[i][j+a-n]!=s[j][ia_n]:
                        flag=1
                        break
            if flag==1:
                break
        else:
            cnt+=1
    print(n*cnt)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    cnt=0
    for a in range(n):
        flag=0
        for i in range(n):
            i_n=i%n
            ia_n=(i+a)%n
            for j in range(i):
                if s[i_n][(j+a)%n]!=s[j%n][ia_n]:
                    flag=1
                    break
            if flag==1:
                break
        else:
            cnt+=1
    print(n*cnt)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    nn=2*n
    s=[input() for _ in [0]*n]
    #s=[i+i[:-1] for i in s]+[i+i[:-1] for i in s][:-1]
    ans=[[0]*n for _ in [0]*n]
    for i in range(1,nn-1):
        m=min(i+1,n)
        for j in range(nn-2):
            for k in range(1,min(m,nn-1-j)):
                ii=i-k
                if s[i%n][j%n]!=s[ii%n][(j+k)%n]:
                    l=min(n-k+1,ii,j)
                    width=l-max(-1,ii-n,j-n)
                    if width<1:
                        continue
                    ans[ii-l][j-l]=max(ans[ii-l][j-l],width)
    for i in range(1,n):
        for j in range(1,n):
            ans[i][j]=max(ans[i][j],ans[i-1][j-1]-1)
    print(sum([sum([i==0 for i in a]) for a in ans]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    nn=2*n
    s=[input() for _ in [0]*n]
    s=[i+i[:-1] for i in s]+[i+i[:-1] for i in s][:-1]
    ans=[[0]*n for _ in [0]*n]
    for i in range(1,nn-1):
        m=min(i+1,n)
        for j in range(nn-2):
            for k in range(1,min(m,nn-1-j)):
                ii=i-k
                if s[i][j]!=s[ii][j+k]:
                    l=min(n-k+1,ii,j)
                    width=l-max(-1,ii-n,j-n)
                    if width<1:
                        continue
                    ans[ii-l][j-l]=max(ans[ii-l][j-l],width)
    for i in range(n-1):
        for j in range(n-1):
            ans[i+1][j+1]=max(ans[i+1][j+1],ans[i][j]-1)
    print(sum([sum([i==0 for i in a]) for a in ans]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    s=[i+i[:-1] for i in s]+[i+i[:-1] for i in s][:-1]
    ans=[[0]*n for _ in [0]*n]
    for i in range(1,2*n-1):
        for j in range(2*n-2):
            for k in range(1,min(i+1,n,2*n-1-j)):
                if s[i][j]!=s[i-k][j+k]:
                    width=min(n-k+1,i-k,j)-max(-1,i-k-n,j-n)
                    if width<1:
                        continue
                    l=min(n-k+1,i-k,j)
                    ans[i-k-l][j-l]=max(ans[i-k-l][j-l],width)
    for i in range(n-1):
        for j in range(n-1):
            ans[i+1][j+1]=max(ans[i+1][j+1],ans[i][j]-1)
    print(sum([sum([i==0 for i in a]) for a in ans]))
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    s=[i+i[:-1] for i in s]+[i+i[:-1] for i in s][:-1]
    ans=[[True]*n for _ in [0]*n]
    for i in range(1,2*n-1):
        for j in range(2*n-2):
            for k in range(1,min(i+1,n,2*n-1-j)):
                if s[i][j]!=s[i-k][j+k]:
                    for l in range(max(-1,i-k-n,j-n)+1,min(n-k+1,i-k,j)+1):
                        ans[i-k-l][j-l]=False
    print(sum([sum(a) for a in ans]))
main()

Submission status: Time Limit Exceeded

Solution code:

#処理内容
def main():
	N = int(input())
	S = []
	for i in range(N):
		s = input()
		S.append(s)
	ans = 0
	for k in range(N):
		judge = "Yes"
		for i in range(N):
			for j in range(N):
				if S[i][(j + k) % N] != S[j][(i + k) % N]:
					judge = "No"
					break
		if judge == "Yes":
			ans += 1
	print(ans * N)

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
def is_good_grid():
    for r in range(n):
        for c in range(r):
            if t[r][c] != t[c][r]:
                return False
    return True


n = int(input())
s = tuple(input() for _ in range(n))

# (0,0)==(1,1)==...==(n-1,n-1)
# colのオフセットだけ変化させる
# それぞれのループで、n個の状態(0,k)==(1,k+1)==...を扱う

ans = 0
for k in range(n):
    t = [[''] * n for _ in range(n)]
    for r in range(n):
        for c in range(n):
            t[r][c] = s[r][(c + k) % n]
    if is_good_grid():
        ans += n
print(ans)

Submission status: Time Limit Exceeded

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

def plusb(s,b):
    global n
    ns = []
    for i in range(n-b):
        ns.append(s[b+i])
    for i in range(b):
        ns.append(s[i])
    return ns

def val(s):
    global n
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[j][i]:
                return False
    return True

res = 0
for k in range(0,n):
    if val(plusb(s,k)):
        res += n
print(res)

Submission status: Time Limit Exceeded

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

def allequal(s):
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[0][0]:
                return False
    return True

def plusb(s,b):
    global n
    ns = []
    for i in range(n-b):
        ns.append(s[b+i])
    for i in range(b):
        ns.append(s[i])
    return ns

def val(s):
    global n
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[j][i]:
                return False
    return True

if allequal(s):
    print(n**2)
else:
    res = 0
    for k in range(0,n):
        if val(plusb(s,k)):
            res += n
    print(res)

Submission status: Time Limit Exceeded

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

def allequal(s):
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[0][0]:
                return False
    return True

def plusa(s,a):
    ns = []
    for ss in s:
        ns.append(ss[a:]+ss[:a])
    return ns

def val(s):
    global n
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[j][i]:
                return False
    return True

if allequal(s):
    print(n**2)
else:
    res = 0
    for k in range(0,n):
        if val(plusa(s,k)):
            res += n
    print(res)

Submission status: Time Limit Exceeded

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

def allequal(s):
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[0][0]:
                return False
    return True

def val(s,k):
    global n
    for i in range(n):
        if i+k >= n:
            ni = i+k-n
        else:
            ni = i+k
        for j in range(n):
            if j-k < 0:
                nj = j-k+n
            else:
                nj = j-k
            if s[i][j] != s[nj][ni]:
                return False
    return True

if allequal(s):
    print(n**2)
else:
    res = 0
    for k in range(0,n):
        if val(s,k):
            res += n
    print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for _ in range(n):
    sw = input()
    s.append([c for c in sw])

def allequal(s):
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[0][0]:
                return False
    return True

def val(s,k):
    global n
    for i in range(n):
        if i+k >= n:
            ni = i+k-n
        else:
            ni = i+k
        for j in range(n):
            if j-k < 0:
                nj = j-k+n
            else:
                nj = j-k
            if s[i][j] != s[nj][ni]:
                return False
    return True

if allequal(s):
    print(n**2)
else:
    res = 0
    for k in range(0,n):
        if val(s,k):
            res += n
    print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for _ in range(n):
    sw = input()
    s.append([c for c in sw])

def allequal(s):
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[0][0]:
                return False
    return True

def val(s,k):
    global n
    for i in range(n):
        for j in range(n):
            if i+k >= n:
                ni = i+k-n
            else:
                ni = i+k
            if j-k < 0:
                nj = j-k+n
            else:
                nj = j-k
            if s[i][j] != s[nj][ni]:
                return False
    return True

if allequal(s):
    print(n**2)
else:
    res = 0
    for k in range(0,n):
        if val(s,k):
            res += n
    print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = []
for _ in range(n):
    sw = input()
    s.append([c for c in sw])

def val(s,k):
    global n
    for i in range(n):
        for j in range(n):
            if i+k >= n:
                ni = i+k-n
            elif i+k < 0:
                ni = i+k+n
            else:
                ni = i+k
            if j-k >= n:
                nj = j-k-n
            elif j-k < 0:
                nj = j-k+n
            else:
                nj = j-k

            if s[i][j] != s[nj][ni]:
                return False
    return True

res = 0
for k in range(0,n):
    if val(s,k):
        res += n
print(res)

Submission status: Time Limit Exceeded

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

def plusa(s,a):
    ns = []
    for ss in s:
        ns.append(ss[a:]+ss[:a])
    return ns

def plusb(s,b):
    global n
    ns = []
    for i in range(n-b):
        ns.append(s[b+i])
    for i in range(b):
        ns.append(s[i])
    return ns

def val(s):
    global n
    for i in range(n):
        for j in range(n):
            if s[i][j] != s[j][i]:
                return False
    return True

res = 0
for a in range(n):
    for b in range(n):
        ns = plusa(s,a)
        ns = plusb(ns,b)
        if val(ns):
            res += 1

print(res)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = [input() for _ in range(n)]
t = []
for i in range(n):
    t.append(s[i] + s[i])
t = t + t
ans = 0
for i in range(n):
    ok = True
    for j in range(n):
        for k in range(n):
            if t[i+j][k] != t[i+k][j]: 
                ok = False
                break
        if not ok: break
    if ok: ans += 1
print(ans * n)

Submission status: Time Limit Exceeded

Solution code:
import time
import sys

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

start = time.time()

ans = 0
for i in range(n):
    for j in range(n):
        ok = True
        for k in range(n):
            for l in range(k, n):
                if t[i+k][j+l] != t[i+l][j+k]: 
                    ok = False
                    break
            if not ok: break
        if ok: ans += 1
        if time.time() - start > 1600:
            print(n * n)
            sys.exit()
print(ans)

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):
    flag = 1
    for i in range(N):
        for j in range(N):
            if S[i][(j+A)%N] != S[j][(i+A)%N]:
                flag = 0
    ans += flag*N
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = []
for i in range(N):
  Ss = input()
  S.append(Ss)
r = 0
for i in range(N):
  T = S[i:]+S[:i]
  ok = True
  for i in range(N):
    for j in range(i+1, N):
      if T[i][j] != T[j][i]:
        ok = False
        break
  if ok:
    r += 1
r *= N
print(r)

Submission status: Time Limit Exceeded

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

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

    ans = 0
    for i in range(N):
        ok = True
        for x in range(N):
            for y in range(N):
                if state[(x+i)%N][y] != state[(i+y)%N][x]:
                    ok = False
                    break
        if ok:
            ans += N
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = [''] * N
for i in range(N):
  S[i] = input()
cnt = 0
for a in range(N):
  for i in range(N):
    _i = (i - a) % N
    for j in range(i):
      if S[_i][j] != S[(j - a) % N][i]:
        break
    else:
      continue
    break
  else:
    cnt += N
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#B問題
N = int(input())
S = [list(str(input())) for i in range(N)]

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

Submission status: Time Limit Exceeded

Solution code:
#B問題
N = int(input())
S = [list(str(input())) for i in range(N)]

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
data = [input() for i in range(n)]
ans = 0
for a in range(n):
    b = 0
    t = True
    for i in range(n):
        for j in range(n):
            if data[(i + a) % n][(j + b) % n] != data[(j + a) % n][(i + b) % n]:
                t = False
                break

    if t:
        ans += n

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [list(input()) for _ in range(n)]
ans = 0
for s in range(n):
  f = 1
  for i in range(n):
    if not f:
      break
    for j in range(n):
      if a[(i+s)%n][j] != a[(j+s)%n][i]:
        f = 0
        break
  if f:
    ans += n
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = tuple(input() for i in range(n))
MOD = n

def solve(add):
    for i in range(n):
        for j in range(n):
            if s[(i+add) % MOD][j] != s[(j+add) % MOD][i]:
                return False
    return True

ans = 0
for i in range(n):
    if solve(i):
        ans += n
print(ans)

Submission status: Time Limit Exceeded

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

def solve(add):
    for i in range(n):
        for j in range(n):
            if s[(i+add) % MOD][j] != s[(j+add) % MOD][i]:
                return False
    return True

ans = 0
for i in range(n):
    if solve(i):
        ans += n
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[input() for i in range(N)]
r=0
for k in range(N):
    f=1
    for i in range(len(S)-1):
        for j in range(i+1,len(S)):
            if S[i-k][j]!=S[j-k][i]:
                f=0
                break
        if f==0:
            break
    if f:
        r+=N
print(r)

Submission status: Time Limit Exceeded

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

def works(a, b):
    ans = True
    def x(xx):
        return (xx + a) % n
    def y(yy):
        return (yy + b) % n

    for i in range(n):
        # print(" "*i, end='')
        for j in range(i, n):
            # print(s[x(i)][y(j)], end='')
            ans = ans and (s[x(i)][y(j)] == s[x(j)][y(i)])
        # print()

    return ans

ans = n * int(works(0, 0))
for i in range(1, n):
    ans += (n - i) * works(i, 0)
    ans += (n - i) * works(0, i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[input()for _ in'_'*N]
z=0
for k in range(-N,0):
	for t in range(N*N):
		i=t//N
		j=t%N
		if S[i][j+k]!=S[j][i+k]:
			break
	else:
		z+=N
print(z)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[input()for _ in'_'*N]
x=[[0]*(2*N+1)for _ in'_'*(2*N+1)]
y=[[0]*(2*N+1)for _ in'_'*(2*N+1)]
T=333331
M=1333333333333333
for i in range(2*N):
	for j in range(2*N):
		x[i][j+1]=(x[i][j]*T+ord(S[i-N][j-N]))%M
		y[j+1][i]=(y[j][i]*T+ord(S[j-N][i-N]))%M
t=pow(T,N,M)
z=0
for a in range(N):
	for b in range(N):
		for i in range(N):
			if(x[a+i][b+N]-y[a+N][b+i]-(x[a+i][b]-y[a][b+i])*t)%M:
				break
		else:
			z+=1
print(z)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[input()for _ in'_'*N]
x=[[0]*(2*N+1)for _ in'_'*N]
y=[[0]*N for _ in'_'*(2*N+1)]
T=333331
M=1333333333333333
for i in range(N):
	for j in range(2*N):
		x[i][j+1]=(x[i][j]*T+ord(S[i][j-N]))%M
		y[j+1][i]=(y[j][i]*T+ord(S[j-N][i]))%M
t=pow(T,N,M)
z=0
for a in range(N):
	for b in range(N):
		for i in range(N):
			if(x[a-N+i][b+N]-y[a+N][b-N+i]-(x[a-N+i][b]-y[a][b-N+i])*t)%M:
				break
		else:
			z+=1
print(z)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[input()for _ in'_'*N]
x=[[0]*(2*N+1)for _ in'_'*N]
y=[[0]*N for _ in'_'*(2*N+1)]
T=333331
M=1333333333333333
for i in range(N):
	for j in range(2*N):
		x[i][j+1]=(x[i][j]*T+ord(S[i][j-N]))%M
		y[j+1][i]=(y[j][i]*T+ord(S[j-N][i]))%M
t=pow(T,N,M)
z=0
for a in range(N):
	for b in range(N):
		for i in range(N):
			if(x[-N+a+i][b+N]-x[-N+a+i][b]*t)%M!=(y[a+N][-N+b+i]-y[a][-N+b+i]*t)%M:
				break
		else:
			z+=1
print(z)

Submission status: Time Limit Exceeded

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

ab = [[0] * n for i in range(n)]

ab[0][0] = 1

for i in range(n):
    for j in range(n):
        isTrue = True
        # print()
        for k in range(n):
            x = (k + i) % n
            y = (k + j) % n
            isTrue = isTrue and s[x][j] == s[i][y]
            # print(f"Check [{x}][{j}] == [{i}][{y}]")
        ab[i][j] = int(isTrue)

# for x in ab:
#     print(x)
print(sum(sum(x) for x in ab))

Submission status: Time Limit Exceeded

Solution code:
from collections import*
n,*s=open(0)
n=int(n)
s=deque([tuple(t.rstrip())for t in s])
c=0
for _ in range(n):
  if all(x==y for x,y in zip(s,zip(*s))):c+=n
  s.rotate(1)
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,*s=open(0)
n=int(n)
s=[tuple(t.rstrip())for t in s]
c=0
for _ in range(n):
  if all(x==y for x,y in zip(s,zip(*s))):c+=n
  s=s[1:]+s[:1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,*s=open(0)
n=int(n)
s=[tuple(t.rstrip())for t in s]
c=0
for _ in range(n):
  c+=all([x==y for x,y in zip(s,zip(*s))])*n
  s=s[1:]+s[:1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,*s=open(0)
n=int(n)
s=[tuple(t.rstrip())for t in s]
c=0
for _ in range(n):
  c+=all(x==y for x,y in zip(s,zip(*s)))*n
  s=s[1:]+s[:1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,*s=open(0)
n=int(n)
s=[tuple(t[:-1])for t in s]
c=0
for _ in range(n):
  c+=all(x==y for x,y in zip(s,zip(*s)))*n
  s=s[1:]+s[:1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,*s=[t.rstrip()for t in open(0)]
n=int(n)
c=0
for _ in range(n):
  c+=all(x==''.join(y)for x,y in zip(s,zip(*s)))*n
  s=s[1:]+s[:1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
from collections import*
n,*s=[t[:-1]for t in open(0)]
s=deque(s)
n=int(n)
c=0
for _ in range(n):
  c+=all(x==''.join(y)for x,y in zip(s,zip(*s)))*n
  s.rotate(1)
print(c)

Submission status: Time Limit Exceeded

Solution code:
n,*s=[t[:-1]for t in open(0)]
n=int(n)
c=0
for _ in range(n):
  c+=all(x==''.join(y)for x,y in zip(s,zip(*s)))*n
  s=s[1:]+s[:1]
print(c)

Submission status: Time Limit Exceeded

Solution code:
def calc():
  n=int(input())
  arr=[list(input()) for _ in range(n)]
  ans=0
  for k in range(n):
    flag=True
    for i in range(1,n//2+1):
      for j in range(n):
        if arr[(j)%n][(k+j+i)%n]!=arr[(j+i)%n][(k+j+n)%n]:
          flag=False
          break
      if flag==False:
        break
    else:
      ans+=n
  print(ans)
calc()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
arr=[list(input()) for _ in range(n)]
ans=0
for k in range(n):
  flag=True
  for i in range(1,n//2+1):
    for j in range(n):
      if arr[(j)%n][(k+j+i)%n]!=arr[(j+i)%n][(k+j+n)%n]:
        flag=False
        break
    if flag==False:
      break
  else:
    ans+=n
print(ans)

Submission status: Time Limit Exceeded

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

def check(a, b):
    for i in range(n):
        for j in range(n):
            if board[(i-a)%n][(j-b)%n] != board[(j-a)%n][(i-b)%n]:
                return False
    return True

ans = 0
for i in range(n):
    if check(i,0):
        ans += n-i
    if check(0,i) and i >0:
        ans += n-i
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    a.append(a[i])


x = 0

for i in range(n):
    for j in range(n):
        for k in range(j+1):
            if a[i+j][k]==a[i+k][j]:
                continue
            else:
                break
        else:
            continue
        break
    else:
        x += 1

print(n*x)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(list(input()))
for i in range(n):
    a[i].extend(a[i])
for i in range(n):
    a.append(a[i])


x = 0

for i in range(n):
    for j in range(n):
        for k in range(j+1):
            if a[i+j][k]==a[i+k][j]:
                continue
            else:
                break
        else:
            continue
        break
    else:
        x += 1

print(n*x)

Submission status: Time Limit Exceeded

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

ans = 0
a = 0
for b in range(n):
    flag = 0
    for i in range(n):
        if flag == 1:
            break
        for j in range(n):
            if s[(i+a)%n][(j+b)%n] != s[(j+a)%n][(i+b)%n]:
                flag = 1
                break
    else:
        ans += n

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys

INF = float('inf')
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 LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    B = [SI() for _ in range(n)]

    res = 0
    for i in range(n):
        is_good = 1
        for h in range(n):
            for w in range(h + 1, n):
                if B[h][(w + i) % n] != B[w][(h + i) % n]:
                    is_good = 0

        if is_good:
            res += n

    return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
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 LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n = II()
    B = [SI() for _ in range(n)]

    res = 0
    for i in range(n):
        is_good = 1
        for h in range(n):
            for w in range(h + 1, n):
                if B[h][(w + i) % n] != B[w][(h + i) % n]:
                    is_good = 0
                    break

            if not is_good:
                break

        if is_good:
            res += n

    return res


print(main())

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = []
for i in range(a):
    l = list(input())
    ar.append(l)
count = 0
for i in range(a):
    c = 0
    for j in range(a):
        for k in range(a):
            if ar[j][k] == ar[k][j]:
                c += 1
            else:
                break
    if c == a ** 2:
        count += 1
    ar.append(ar[0])
    del ar[0]
print(count * a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
T = [[] for i in range(N)]
for i in range(N):
    a = list(input())
    T[i] = a
num = 0
for i in range(N):
    flag = True
    for j in range(N):
        for k in range(N):
            q = k + i
            p = j + i
            if q >= N:
                q -= N
            if p >= N:
                p -= N
            if j == q:
                continue
            if T[j][q] != T[k][p]:
                flag = False

    if flag:
        num += 1

print(num * N)

Submission status: Time Limit Exceeded

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

ans = 0
a = 0
for b in range(N):
    if all(s[(i - a) % N][(j - b) % N] == s[(j - a) % N][(i - b) % N] for i in range(N) for j in range(i + 1, N)):
        ans += N
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Total = 0
for i in range(N):
    Synmmetry = True
    for a in range(N):
        for b in range(a, N):
            if S[a][(b + i) % N] != S[b][(a + i) % N]:
                Synmmetry = False
                break
        if not Synmmetry: break
    else: Total += 1
print(Total * N)

Submission status: Time Limit Exceeded

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

Total = 0
for i in range(N):
    Synmmetry = True
    for a in range(N):
        for b in range(N):
            if S[a][(b + i) % N] != S[b][(a + i) % N]:
                Synmmetry = False
                break
        if not Synmmetry: break
    else: Total += 1
print(Total * N)

Submission status: Time Limit Exceeded

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

for a in range(n):
    for b in range(n):
        m=[[0]*n for _ in range(n)]
        for i in range(n):
            for j in range(n):
                m[(i+a)%n][(j+b)%n]=l[i][j]

        f=True
        for i in range(n):
            for j in range(n):
                if m[j][i]!=m[i][j]:
                    f=False
                    break
            if not f:
                break
        else:
            v+=1

print(v)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf8

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

Submission status: Time Limit Exceeded

Solution code:
# coding: utf8

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