Buggy python submission (verdict: Time Limit Exceeded):



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

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

    """
    あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
    """

    cnt = 0
    for a in range(N):
        S2 = [["" for _ in range(N)]  for _ 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()

Fixed submission:



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

Buggy python submission (verdict: Time Limit Exceeded):


N = int(input())

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

"""
あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
"""

cnt = 0
for a in range(N):
    S2 = [["" for _ in range(N)]  for _ 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)


Fixed submission:



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

Buggy python submission (verdict: Time Limit Exceeded):


N = int(input())

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

"""
あるA,Bを決めたときに、A+1,B+1と比べると、全ての文字が右下にずれるので、(A,B)と(A+1,B+1)の結果は同じになる
"""

cnt = 0
for a in range(N):
    S2 = [["" for _ in range(N)]  for _ 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)


Fixed submission:



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