Problem: 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.

Submission attempts (in order):
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] != S[j][i]:
                return 0
    return 1

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

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

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

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
            
            
            # else:
             - 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)

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[a][b] != S[c][d]:
                return 0
    return 1

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

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

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

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, i)
print(cnt * N)

Verdicts: Wrong Answer

Runtime Error

Time Limit Exceeded

Runtime Error

Runtime Error

Accepted