Problem:


Score : 500 points

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns.
For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

First, choose two integers A and B ( 0 \leq A, B < N ).

Write one letter in each square on the second board.
Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board.
Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Constraints

1 \leq N \leq 300

S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.

Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.

Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.



Sample input:
2
ab
ca


Sample output:
2

Submission status: Accepted

Solution code:
import sys
import numpy as np

read = sys.stdin.read
readline = sys.stdin.readline

N, *S = map(str, read().split())
S = np.array(list(map(list, S)), np.str)
S = np.hstack((S, S))
S = np.vstack((S, S))
N = int(N)
answer = 0
for y in range(N):
    for i in range(N):
        a = S[i, y:y + N]
        b = S[:N, i + y]
        if np.any(a != b):
            break
    else:
        answer += N

print(answer)

Submission status: Accepted

Solution code:
import sys
import numpy as np

read = sys.stdin.read
readline = sys.stdin.readline

N, *S = map(str, read().split())
S = np.array(list(map(list, S)), np.str)
N = int(N)
answer = 0
for y in range(N):
    for i in range(N):
        a = S[i, y:].tolist() + S[i, :y].tolist()
        b = S[:, (i + y) % N].tolist()
        if a != b:
            break
    else:
        answer += N

print(answer)

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    S=[input()for _ in range(N)]
    T=["".join(s) for s in zip(*S)]
    ans=0
    for i in range(N):
        if all(s==t for s,t in zip(S,T)):
            ans+=1
        S=[s[-1]+s[:-1] for s in S]
        T=[T[-1]]+T[:-1]
    print(ans*N)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
#input = sys.stdin.readline
import numpy as np

def main():
    N = int(input())
    s = [list(map(str,input())) for _ in range(N)]
    s = np.array(s)
    ans = 0
    for i in range(N):
        T = np.vstack((s[i:],s[:i]))
        if np.all(T==T.T):
            ans += N
            
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, *S = open(0).read().split()
N = int(N)
ans = 0
for p in range(N):
  flag = False
  for i in range(N):
    for j in range(N):
     x = i
     y = (j-p)%N
     a,b = y, (x+p)%N
     if S[i][j] != S[a][b]:
       flag = True
       break
    if flag:
      break
  else:
    ans+= N
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
S = np.array([ list(input()) for _ in range(n) ])
ans = 0
for j in range(n):
  M = np.roll(S, j, axis=0)
  if (M==M.T).all():
    ans += 1
print(ans*n)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
S = np.array([ list(input()) for _ in range(n) ])
ans = 0
for j in range(n):
  M = np.roll(S, j, axis=0)
  if (M==M.T).all():
    ans += n
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
S = np.array([ list(input()) for _ in range(n) ])
ans = 0
for j in range(n):
  M = S[:, [(l+j)%n for l in range(n)]]
  if (M==M.T).all():
    ans += n
print(ans)

Submission status: Accepted

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

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

for A in range(1, N):
    flag = True
    for i in range(N):
        for j in range(i+1, N):
            if S[(i-A)%N][j] != S[(j-A)%N][i]:
                flag = False
                break
    if flag:
        ans += N-A
print(ans)

Submission status: Accepted

Solution code:



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

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

    """
    あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
    """
    S2 = [["" for _ in range(N)]  for _ in range(N)]
    cnt = 0
    for a in range(N):
        
        for i in range(N):
            for j in range(N):
                if 0 <= i + a <= N-1:
                    y = i + a
                else:
                    y = (i + a) - N
                S2[y][j] = S[i][j]
        
        flag = True
        for i in range(N):
            if not flag:
                break
            for j in range(N):
                if i == j:
                    continue
                if S2[i][j] != S2[j][i]:
                    flag = False
                    break
        if flag:
            cnt += 1

    print(cnt * N)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math

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

N=int(input())
S=[list(input()) for i in range(N)]
count=0


for i in range(N):
    if main():
        count+=N
    for m in range(N):
        a=S[m].pop()
        S[m].insert(0,a)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
s=[list(input()) for i in range(n)]
ans=0
for i in range(n):
  ct=0
  for j in range(n):
    for k in range(n):
      if s[(j+i)%n][k]==s[(k+i)%n][j]:
        ct+=1
  if ct==n**2:
    ans+=1
print(ans*n)

Submission status: Accepted

Solution code:
n=int(input())
s=[list(input()) for i in range(n)]
ans=0
for i in range(n):
  ct=0
  for j in range(n):
    for k in range(n):
      if s[(j+i)%n][k]!=s[(k+i)%n][j]:
        ct+=1
  if ct==0:
    ans+=1
print(ans*n)

Submission status: Accepted

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

ans = 0
for i in range(N):
    tmp = S[:, i: i + N]
    if (tmp == tmp.T).all():
        ans += 1

ans = ans * N
print(ans)

Submission status: Accepted

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

ans = 0

for k in range(N):
    symmetry = True
    
    for i in range(N-1):
        for j in range(i+1,N):
            
            if S[(k+i)%N][j]!=S[(k+j)%N][i]:
                symmetry = False
                
    if symmetry:
        ans += N
        
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, S: "List[str]"):

    A = 0
    count = 0
    for B in range(N):
        # print("***", B)
        flag = True
        # for i in range(N):
        #     print(S[i][B:]+S[i][:B])
        for i in range(N):
            for j in range(i, N):
                # print(i, j, j-B, S[i][j-B], S[j][i-B])
                if S[i][j-B] != S[j][i-B]:
                    flag = False
                    break
            if flag is False:
                break
        if flag:
            count += 1
    print(count*N)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, S)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

yoko, tate = [], []
for i in range(N):
  tmp = 0
  for j in range(N):
    tmp += S[i][j] * 2 ** ((N - j - 1) * 5)
  yoko.append(tmp)
for j in range(N):
  tmp = 0
  for i in range(N):
    tmp += S[i][j] * 2 ** ((N - i - 1) * 5)
  tate.append(tmp)
#print("yoko:", yoko)
#print("tate:", tate)

answer = 0
yi, ti = 0, 0
for t in range(N):
  flag = 0
  for i in range(N):
    if yoko[(i + yi) % N] != tate[(i + ti) % N]:
      flag = 1
      break
  if flag == 0:
    answer += N
    
  # 左にずらす
  for i in range(N):
    tate[i] = (tate[i] << 5 | tate[i] >> (N - 1) * 5) & (2 ** (5 * N) - 1)
  yi += 1
  #print("tate:", tate)

print(answer)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
s=np.array([list(input()) for i in range(n)])
s=np.concatenate([s,s])
ans=0
for i in range(n):
    l=s[i:i+n]
    if (l==l.T).all():
        ans+=n
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    s = [input() for i in range(n)]
    ans = 0
    for i in range(n):
        s = [[s[i][(j+1)%n] for j in range(n)] for i in range(n)]
        for j in range(n):
            for k in range(j):
                if s[k][j] != s[j][k]:
                    break
            else:
                continue
            break
        else:
            ans += n
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

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


def check(a, b):
    checkgrid = [ [ grid[(i+a)%n][(j+b)%n] for j in range(n)] for i in range(n) ]
    
    ret = True
    for i in range(n):
        for j in range(n):
            if checkgrid[i][j] != checkgrid[j][i]:
                ret = False
                break
    return ret

ans = 0

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

Submission status: Accepted

Solution code:
def main():
    import numpy as np
    c = np.concatenate
    n = int(input())
    ss = [list(input()) for _ in [0]*n]
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    d = {j: i for i, j in enumerate(alphabet)}
    s = np.array([[d[i] for i in j] for j in ss], dtype=np.int8)
    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: Accepted

Solution code:
import collections
import sys
import numpy as np
input = sys.stdin.readline
table = collections.defaultdict(set)
N = int(input())
S = [list(input().rstrip()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*N)]
table2 = [[set() for j in range(N)] for i in range(N)]

for i in range(2*N):
    if i < N:
        T[i] = S[i] + S[i]
    else:
        T[i] = S[i - N] + S[i-N]
ans = 0
T = np.array(T)
for i in range(N):
    tmp = T[0:N, i:i+N]
    if (tmp == tmp.T).all():
        ans += N

    tmp = T[i+1:i+N+1, N-1-i:2*N-1-i]
print(ans)

Submission status: Accepted

Solution code:
import collections
import sys
import numpy as np
input = sys.stdin.readline
table = collections.defaultdict(set)
N = int(input())
S = [list(input().rstrip()) for i in range(N)]
T = [[""] * (N*2) for i in range(2*N)]
table2 = [[set() for j in range(N)] for i in range(N)]

for i in range(2*N):
    if i < N:
        T[i] = S[i] + S[i]
    else:
        T[i] = S[i - N] + S[i-N]
ans = 0
T = np.array(T)
for i in range(N):
    tmp = T[i:i+N, N-1-i:2*N-1-i]

    if (tmp == tmp.T).all():
        ans += N - (N // 2)

    tmp = T[i+1:i+N+1, N-1-i:2*N-1-i]

    if (tmp == tmp.T).all():
        ans += N // 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [input().strip() for _ in range(N)]
cnt = 0
for k in range(N):
    flag = 0
    T = [S[i][k:]+S[i][:k] for i in range(N)]
    for i in range(N-1):
        for j in range(i+1,N):
            if T[i][j]!=T[j][i]:
                flag = 1
                break
        if flag==1:break
    if flag==0:
        cnt += 1
print(cnt*N)

Submission status: Accepted

Solution code:
def examA():
    N = I()
    A = LI()
    d = defaultdict(int)
    d[0] = 1; cur = 0
    for i in A:
        cur +=i
        d[cur] +=1
#    print(d)
    ans = 0
    for i in d.values():
        ans +=i*(i-1)//2
    print(ans)
    return

def examB():
    N = I()
    S = [SI() for _ in range(N)]
    cur = 0
    for k in range(N):
        flag = True
        for i in range(N):
            for j in range(i+1,N):
                if S[i][j-k]!=S[j][i-k]:
                    flag = False
                    break
            if not flag:
                break
        if flag:
            cur +=1
    ans = cur*N
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Submission status: Accepted

Solution code:
N = int(input())

S = []

ans = 0

for i in range(N):

    s = list(input())
    S.append(s)


for A in range(N):

    flag = True

    for i in range(N):

        for j in range(i):

            if S[(i+A) % N][j % N] != S[(j+A) % N][i % N]:
                flag = False
                break

        if not flag:
            break

    if flag:
        ans += N

print (ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from itertools import product

def resolve():
    n=int(input())
    G=[input() for _ in range(n)]
    ans=0
    for i in range(n):
        now=G[i:]+G[:i]
        ans+=all(now[i][j]==now[j][i] for i,j in product(range(n),repeat=2))

    print(n*ans)
resolve()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
S = [list(input()) for _ in range(N)]
S = np.array(S, dtype=object)

S = np.concatenate([S, S])
ans = 0
for B in range(N):
    T = S[B:B+N]
    if (T == T.T).all():
        ans += N
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

N = ri()
S = np.array([list(rs()) for _ in range(N)])

S = np.concatenate([S, S], axis=0)
answer = 0
for b in range(N):
    S2 = S[b:b+N]
    if (S2 == S2.T).all():
        answer += N

print(answer)

Submission status: Accepted

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


ans = 0

for i in range(n):
    ok = True
    for j in range(n):
        for k in range(n):
            if j == k:
                continue
            l = j + i
            if l > n-1:
                l = l-n

            m = k + i
            if m > n-1:
                m = m-n
            # print(l,k,i)
            # print(arr[l][k],arr[k][l])
            if arr[l][k] != arr[m][j]:
                ok = False
                break

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



print(ans)

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
S = [list(sys.stdin.buffer.readline().decode().rstrip() * 2) for _ in range(N)] * 2
S = np.array(S, dtype='U1')

ans = 0
for h in range(N):
        s = S[h: h + N, 0: 0 + N]
        if np.all(s == s.T):
            ans += N
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [input() for _ in range(n)]
ans = 0
for j in range(n):
    for k in range(j + 1, n):
        if s[j][k] != s[k][j]:
            break
    else:
        continue
    break
else:
    ans += n
for i in range(1, n):
    for j in range(n):
        for k in range(j + 1, n):
            if s[j + i - n][k] != s[k + i - n][j]:
                break
        else:
            continue
        break
    else:
        ans += n
print(ans)

Submission status: Accepted

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

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

ret = 0
for a in range(N) :
    if check(a) :
        ret += N
            
print(ret)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    s=[input() for _ in [0]*n]
    def check(a):
        for i in range(n):
            for j in range(i):
                if s[i%n][(j+a)%n]!=s[j%n][(i+a)%n]:
                    return 0
        return 1
    print(n*sum([check(i) for i in range(n)]))
main()

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    nn=2*n-1
    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):
        m=min(i+1,n)
        for j in range(nn-1):
            for k in range(max(i-j-n,0)+1,min(m,nn-j,i-j+n)):
                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)
                    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: Accepted

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

ans = 0
for n in range(N):
    m = S[n:]+S[:n]
    flg = True
    for i in range(N):
        for j in range(N):
            if m[i][j] != m[j][i]:
                flg = False

    if flg:
        ans += N

print(ans)

Submission status: Accepted

Solution code:
#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
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: Accepted

Solution code:
def is_good_grid(sft):
    for r in range(n):
        r_ = (r - sft) % n  # 減算に直した
        for c in range(r):
            c_ = (c - sft) % n  # 減算に直した
            if s[r][c_] != s[c][r_]:
                # kマス列シフトした後の(r,c)と(c,r)の一致を見るには、
                # (r,c)と(c,r)それぞれの列シフト前、つまり列-sftの座標を見る必要がある
                # r_を用意せず、c_だけで済むと勘違いした
                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):
    if is_good_grid(k):
        ans += n
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
s = [list(input())[: -1] for _ in range(N)]
res = 0
for a in range(N):
  f = 1
  for i in range(N):
    for j in range(N):
      if s[(i + a) % N][j] != s[(j + a) % N][i]:
        f = 0
        break
  if f: res += N
print(res)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N = I()
S = []
for _ in range(N):
    S.append(str(input()))

ans = 0
for b in range(N):
    flag = True
    for i in range(N):
        for j in range(i+1,N):
            if S[i][(j+b)%N] != S[j][(i+b)%N]:
                flag = False
                break
        else:
            continue
        break
    if flag:
        ans += N

print(ans)

Submission status: Accepted

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

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

ans = 0
SS = []
for i in range(N):
  SS = []
  for j in range(1,N):
    SS.append(S[j])
  SS.append(S[0])
  #print(SS)
  ans += check(SS)
  S = SS
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=[]
for i in range(N):
    l=list(input())
    L.append(l)
    
#print(L)
F=0
ans=0
for i in range(N):
    F=0
    for a in range(N):
        if F==1:
            break
        for b in range(N):
            if L[a][(b+i)%N]!=L[b][(a+i)%N]:
                F=1
                break
    if F==0:
        ans+=N
        #print(i)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
st=list(map(''.join,zip(*s)))

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

res = 0
for i in range(n):
    if plusa(st,i) == plusb(s,i):
        res += n

print(res)

Submission status: Accepted

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

res = 0
for b in range(n):
    judge = True
    d = [['']*n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            d[i][(j+b)%n] = s[i][j]
    
    for i in range(n):
        for j in range(n):
            if d[i][j] != d[j][i]:
                judge = False
    
    if judge:
        res += n

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC23 B

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

def check(x):
    for i in range(n):
        for j in range(n):
            if x[i][j] == x[j][i]:
                continue
            return False
    return True

n = I()
s = [list(input()) for _ in range(n)]
ans = 0
for b in range(n):
    flg = True
    for i in range(n):
        for j in range(n):
            if s[(i+b)%n][j] == s[(j+b)%n][i]:
                continue
            else:
                flg = False
                break
    if flg:
        ans += n
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = [list(input()) for _ in range(N)]
T = [list(_) for _ in zip(*S)]
cnt = 0
for i in range(N):
    f = 1
    for j in range(N):
        if S[i + j - N] != T[j][i:] + T[j][:i]:
            f = 0
            break
    cnt += f
print(cnt * N)

Submission status: Accepted

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

A = 0
B = 0

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

Submission status: Accepted

Solution code:
N=int(input())
S=[input() for i in range(N)]
St=list(map(''.join,zip(*S)))
r=0
for k in range(N):
    f=1
    for i in range(N):
        if S[i][k:]+S[i][:k]!=St[i+k-N]:
            f=0
            break
    if f:
        r+=N
print(r)

Submission status: Accepted

Solution code:
import sys

import numpy as np

N = int(sys.stdin.buffer.readline())
S = np.frombuffer(sys.stdin.buffer.read(),dtype='S1').reshape(N,-1)[:,:N]

# 2倍しておく
S = np.concatenate([S,S])

answer = 0
for x in range(N):
    T = S[x:x+N]
    if (T == T.T).all():
        answer += N

print(answer)

Submission status: Accepted

Solution code:
import sys
import heapq
from operator import itemgetter
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7

def sol():
    N = int(input())
    S = [0] * N

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

    def isSame(a):
        for i in range(N):
            for j in range(N):
                if S[(i + a) % N][j] != S[(j + a) % N][i]:
                    return False
        return True

    ans = 0
    for a in range(N):
        if isSame(a):
            ans += N

    print(ans)

sol()

Submission status: Accepted

Solution code:
n,*s=open(0)
n=int(n)
c=0
for _ in range(n):
  for i in range(n*n):
    i,j=i//n,i%n
    if s[i][j]!=s[j][i]:
      break
  else:
    c+=n    
  s=s[1:]+s[:1]
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

s = []

for i in range(n):
    s.append(input()*2)

ans = 0

for i in range(n):
    flag = True
    for j in range(n):
        for k in range(n):
            if s[j][k+i]!=s[k][j+i]:
                flag = False
                break
        if not flag:
            break
    if flag:
        ans += n

print(ans)

Submission status: Accepted

Solution code:
def check(b):
  for i in range(n):
    for j in range(i+1,n):
      if b[i][j]!=b[j][i]:
        return False
  return True

n=int(input())
board=[list(input()) for _ in range(n)]
cnt=0
for i in range(n):
  if check(board[i:]+board[:i]):
    cnt+=1
print(cnt*n)

Submission status: Accepted

Solution code:
def check(N, S, bi, bj):
    for i in range(N):
        for j in range(i+1, N):
            if S[(bi+i) % N][(bj+j) % N] != S[(bi+j) % N][(bj+i) % N]:
                return False
    return True


def main():
    N = int(input())
    S = [input() for _ in range(N)]
    ans = 0
    for i in range(N):
        if check(N, S, i, 0):
            ans += N
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# 解説AC
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: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

    BT = list(zip(*B))
    res = 0
    for a in range(n):
        is_good = True
        for i in range(n):
            if B[i][n - a:] + B[i][:n - a] != ''.join(BT[(i - a) % n]):
                is_good = False

        if is_good:
            res += n

    return res


print(main())

Submission status: Accepted

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 = [tuple(SI()) for _ in range(n)]

    BT = list(zip(*B))
    res = 0
    for a in range(n):
        is_good = True
        for i in range(n):
            if B[i][n - a:] + B[i][:n - a] != BT[(i - a) % n]:
                is_good = False

        if is_good:
            res += n

    return res


print(main())

Submission status: Accepted

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

# 解は、B = 0として、A = 0, 1, ..., N - 1 のうち、よい盤面になるものの個数のN倍
ans = N * sum(
    all(
        S[i][j] == S[(j - A) % N][(i + A) % N]
        for i in range(N)
        for j in range(N)
    )
    for A in range(N)
)

# 出力
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
l=np.array([list(input()) for _ in range(n)])
ans=0

for _ in range(n):
    l=np.roll(l,1,axis=1)
    if (l==l.T).all():ans+=n

print(ans)