Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
X,Y,Z,K = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
B = sorted([int(x) for x in input().split()])[::-1]
C = sorted([int(x) for x in input().split()])[::-1]

AB = []
for i in range(X):
    for j in range(Y):
        AB.append(A[i]+B[j])
AB = sorted(AB)[::-1]

ans = []
for i in range(min(K,len(AB))):
    for j in range(min(K,Z)):
        ans.append(AB[i]+C[j])
ans.sort()

for i in range(K):
    print(ans[-i-1])


Fixed submission:
X,Y,Z,K = (int(x) for x in input().split())
A = sorted([int(x) for x in input().split()])[::-1]
B = sorted([int(x) for x in input().split()])[::-1]
C = sorted([int(x) for x in input().split()])[::-1]

AB = []
for i in range(X):
    for j in range(Y):
        if i*j > K:
            break
        AB.append(A[i]+B[j])
AB = sorted(AB)[::-1]

ans = []
for i in range(min(K,len(AB))):
    for j in range(min(K,Z)):
        if i*j > K:
            break
        ans.append(AB[i]+C[j])
ans.sort()

for i in range(K):
    print(ans[-i-1])
Buggy python submission (verdict: Time Limit Exceeded):
N,Q = (int(x) for x in input().split())
S = input()

for i in range(Q):
    l,r = (int(x) for x in input().split())
    print(S[l-1:r].count('AC'))


Fixed submission:
N,Q = (int(x) for x in input().split())
S = list(input())
count = [0 for i in range(N)]
c = 0
for i in range(N-1):
    if S[i] == 'A':
        if S[i+1] == 'C':
            count[i] = c
            c += 1
            i += 1
        elif S[i+1] != 'A':
            count[i] = c
            i += 1 
    count[i] = c
if count[-2] > count[-1]:
    count[-1] = count[-2]

for i in range(Q):
    l,r = (int(x) for x in input().split())
    print(count[r-1]-count[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = (int(x) for x in input().split())
S = list(input())
count = [0 for i in range(N-1)]
for i in range(N-1):
    if S[i] == 'A':
        if S[i+1] == 'C':
            count[i] = 1
            i += 1

for i in range(Q):
    l,r = (int(x) for x in input().split())
    print(sum(count[l-1:r-1]))


Fixed submission:
N,Q = (int(x) for x in input().split())
S = list(input())
count = [0 for i in range(N)]
c = 0
for i in range(N-1):
    if S[i] == 'A':
        if S[i+1] == 'C':
            count[i] = c
            c += 1
            i += 1
        elif S[i+1] != 'A':
            count[i] = c
            i += 1 
    count[i] = c
if count[-2] > count[-1]:
    count[-1] = count[-2]

for i in range(Q):
    l,r = (int(x) for x in input().split())
    print(count[r-1]-count[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = (int(x) for x in input().split())
S = list(input())
for i in range(Q):
    ans = 0
    flag = 0
    l,r = (int(x) for x in input().split())
    for j in range(l-1,r):
        if S[j] == 'A':
            flag = 1
        elif S[j] == 'C':
            if flag == 1:
                ans += 1
        else:
            flag = 0
    print(ans)


Fixed submission:
N,Q = (int(x) for x in input().split())
S = list(input())
count = [0 for i in range(N)]
c = 0
for i in range(N-1):
    if S[i] == 'A':
        if S[i+1] == 'C':
            count[i] = c
            c += 1
            i += 1
        elif S[i+1] != 'A':
            count[i] = c
            i += 1 
    count[i] = c
if count[-2] > count[-1]:
    count[-1] = count[-2]

for i in range(Q):
    l,r = (int(x) for x in input().split())
    print(count[r-1]-count[l-1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
X = list(range(1,N+1))
Nsum = sum(X)

n = 2 
while Nsum % n != 0:
    n += 1

group = [[] for i in range(n)]
value = int(Nsum/n)

for i in range(n-1):
    for j in range(N):
        if sum(X[-j-1:]) > value:
            break
    group[i] = X[-j:]
    X = X[:-j]
    ex = value - sum(group[i])
    if ex != 0:
        group[i].append(X.pop(ex))

group[-1] = X

LEN = []
LENpr = 0
for i in range(n):
    LEN.append(len(group[i]))
for i in range(n-1):
    LENpr += LEN[i] * sum(LEN[i+1:])

print(LENpr)

for i in range(n-1):
    for j in range(i+1,n):
        for k in range(LEN[i]):
            for l in range(LEN[j]):
                print(group[i][k],group[j][l])

Fixed submission:
N = int(input())
X = list(range(1,N+1))
Nsum = sum(X)

n = 2 
while Nsum % n != 0:
    n += 1

group = [[] for i in range(n)]
value = int(Nsum/n)

for i in range(n-1):
    for j in range(N):
        if sum(X[-j-1:]) > value:
            break
    group[i] = X[-j:]
    X = X[:-j]
    ex = value - sum(group[i])
    if ex != 0:
        group[i].append(X.pop(X.index(ex)))

group[-1] = X

LEN = []
LENpr = 0
for i in range(n):
    LEN.append(len(group[i]))
for i in range(n-1):
    LENpr += LEN[i] * sum(LEN[i+1:])

print(LENpr)

for i in range(n-1):
    for j in range(i+1,n):
        for k in range(LEN[i]):
            for l in range(LEN[j]):
                print(group[i][k],group[j][l])
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
drink = []
for _ in range(N):
    drink.append(list(map(int, input().split())))
drink.sort()
ans = 0
while M > 0:
    AB = drink.pop(0)
    b = min(M, AB[1])
    ans += b * AB[0]
    M -= b
print(ans)

Fixed submission:
N, M = map(int, input().split())
drink, sumA, sumB = [], 0, 0
for _ in range(N):
    A,B = map(int, input().split())
    drink.append([A, B])
    sumA += A*B
    sumB += B 

if M == sumB:
    print(sumA)
elif M > sumB // 2:
    drink.sort(reverse=True)
    ans, exM = sumA, sumB - M
    while exM > 0:
        AB = drink.pop(0)
        b = min(exM, AB[1])
        ans -= b * AB[0]
        exM -= b
    print(ans)
else:
    drink.sort()
    ans = 0
    while M > 0:
        AB = drink.pop(0)
        b = min(M, AB[1])
        ans += b * AB[0]
        M -= b
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = (int(x) for x in input().split())
A=[]
B=[]
for i in range(N):
    a,b = (int(x) for x in input().split())
    A.append(a)
    B.append(b)
ans = 0

while M > 0:
    ind = A.index(min(A))
    b = B.pop(ind)

    if M < b:
        b = M
        M = 0
    else:
        M -= b

    ans += A.pop(ind) * b
    
print(ans)


Fixed submission:
N, M = map(int, input().split())
drink, sumA, sumB = [], 0, 0
for _ in range(N):
    A,B = map(int, input().split())
    drink.append([A, B])
    sumA += A*B
    sumB += B 

if M == sumB:
    print(sumA)
elif M > sumB // 2:
    drink.sort(reverse=True)
    ans, exM = sumA, sumB - M
    while exM > 0:
        AB = drink.pop(0)
        b = min(exM, AB[1])
        ans -= b * AB[0]
        exM -= b
    print(ans)
else:
    drink.sort()
    ans = 0
    while M > 0:
        AB = drink.pop(0)
        b = min(M, AB[1])
        ans += b * AB[0]
        M -= b
    print(ans)
Buggy python submission (verdict: Runtime Error):
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
query, X = [], {}

for i in range(Q):
    q = int(input())
    query.append(q)
    X[q] = i
query.sort()

near_s = [0 for _ in range(Q)]
near_t = [0 for _ in range(Q)]
j = 0
while j < Q and query[j] < S[0]:
    near_s[j] = [0, S[0]]
    j += 1    
for i in range(A-1):
    while j < Q and S[i] < query[j] < S[i+1]:
        near_s[j] = [S[i], S[i+1]]
        j += 1
near_s = near_s[:j] + [[S[A-1],0] for _ in range(Q-j)]

j = 0
while j < Q and query[j] < T[0]:
    near_t[j] = [0, T[0]]
    j += 1
for i in range(B-1):
    while j < Q and T[i] < query[j] < T[i+1]:
        near_t[j] = [T[i], T[i+1]]
        j += 1
near_t = near_t[:j] + [[T[B-1],0] for _ in range(Q-j)]

ans = [0]*Q
for i in range(Q):
    q, s0, s1, t0, t1 = query[i], *near_s[i], *near_t[i]
    l1 = q - min(s0, t0)
    l2 = max(s1, t1) - q
    l3 = s1 - t0 + min(s1 - q, q - t0)
    l4 = t1 - s0 + min(t1 - q, q - s0)
    if s0 == 0:
        l1, l4 = float('inf'), float('inf')
    if s1 == 0:
        l2, l3 = float('inf'), float('inf')
    if t0 == 0:
        l1, l3 = float('inf'), float('inf')
    if t1 == 0:
        l2, l4 = float('inf'), float('inf')
    ans[X[q]] = min(l1, l2, l3, l4)

for a in ans:
    print(a)

Fixed submission:
A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
query, X = [], {}

for i in range(Q):
    q = int(input())
    query.append(q)
    X[q] = i
query.sort()

near_s = [0 for _ in range(Q)]
near_t = [0 for _ in range(Q)]
j = 0
while j < Q and query[j] < S[0]:
    near_s[j] = [0, S[0]]
    j += 1    
for i in range(A-1):
    while j < Q and S[i] < query[j] < S[i+1]:
        near_s[j] = [S[i], S[i+1]]
        j += 1
near_s = near_s[:j] + [[S[A-1],0] for _ in range(Q-j)]

j = 0
while j < Q and query[j] < T[0]:
    near_t[j] = [0, T[0]]
    j += 1
for i in range(B-1):
    while j < Q and T[i] < query[j] < T[i+1]:
        near_t[j] = [T[i], T[i+1]]
        j += 1
near_t = near_t[:j] + [[T[B-1],0] for _ in range(Q-j)]

ans = [0]*Q
for i in range(Q):
    q, s0, s1, t0, t1 = query[i], near_s[i][0], near_s[i][1], near_t[i][0], near_t[i][1]
    l1 = q - min(s0, t0)
    l2 = max(s1, t1) - q
    l3 = s1 - t0 + min(s1 - q, q - t0)
    l4 = t1 - s0 + min(t1 - q, q - s0)
    if s0 == 0:
        l1, l4 = float('inf'), float('inf')
    if s1 == 0:
        l2, l3 = float('inf'), float('inf')
    if t0 == 0:
        l1, l3 = float('inf'), float('inf')
    if t1 == 0:
        l2, l4 = float('inf'), float('inf')
    ans[X[q]] = min(l1, l2, l3, l4)

for a in ans:
    print(a)
Buggy python submission (verdict: Runtime Error):
def f_max(X, digit):
    if X == 2**digit - 1:
        return sum([max(Ad[i], N - Ad[i]) * (2**i) for i in range(digit)])
    elif X >= 2**(digit-1):
        return max((N - Ad[digit-1]) * (2**(digit-1)) + f_max(X-2**(digit-1), digit-1), f_max(2**(digit-1)-1, digit-1))
    else:
        return Ad[digit-1] * (2**(digit-1)) + f_max(X, digit-1)

N, K = map(int, input().split())
A = list(map(int, input().split()))
K_max, digits = 2*10**12, 0
while K_max > 0
    K_max >>= 1
    digits += 1

Ad = {i:0 for i in range(digits)}
for a in A:
    for i in range(digits):
        Ad[i] += a % 2
        a >>= 1

print(f_max(K, digits))

Fixed submission:
def f_max(X, digit):
    if X == 0:
        return sum([Ad[i] * (2**i) for i in range(digit)])
    elif X == 2**digit - 1:
        return sum([max(Ad[i], N - Ad[i]) * (2**i) for i in range(digit)])
    elif X >= 2**(digit-1):
        return max((N - Ad[digit-1]) * (2**(digit-1)) + f_max(X-2**(digit-1), digit-1), Ad[digit-1] * (2**(digit-1)) + f_max(2**(digit-1)-1, digit-1))
    else:
        return Ad[digit-1] * (2**(digit-1)) + f_max(X, digit-1)

N, K = map(int, input().split())
K_max, digits = 2*10**12, 0
while K_max > 0:
    K_max >>= 1
    digits += 1

Ad = [0]*digits
for a in list(map(int, input().split())):
    for i in range(digits):
        Ad[i] += a % 2
        a >>= 1

print(f_max(K, digits))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A, B = [], []
for _ in range(N):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)

AB = sorted([[A[i] + B[i], i] for i in range(N)])[::-1]
indexes = sorted([AB[i][1] for i in range(0, N, 2)])[::-1]
ans = 0

for i in range(N):
    if i == indexes[-1]:
        indexes = indexes[:-1]
        ans += A[i]
    else:
        ans -= B[i]
print(ans)

Fixed submission:
N = int(input())
AB, ans = [], 0
for _ in range(N):
    a, b = map(int, input().split())
    AB.append([a+b, a, b])    
AB = sorted(AB, reverse=True)

for i in range(N):
    ans += (-1)**(i%2) * AB[i][i%2 + 1]
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
ans = 0
for i in range(4)
 a = N%10
 if a==2:
  ans += 1
 N /= 10
print(ans)


Fixed submission:
N = int(input())
ans = 0
for i in range(4):
 a = N%10
 if a==2:
  ans = ans+ 1
 N /= 10
 N = int(N)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = list(input())
ans = 0

while 'W' in S:
    ind = S.index('W')
    S.pop(ind)
    ans += ind

print(ans)

Fixed submission:
S = list(input())
B = 0
ans = 0

for i in range(len(S)):
    if S[i] == 'B':
        B += 1
    else:
        ans += B

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
ans = 0

while 'BW' in S:
    ans += S.count('BW')
    S = S.replace('BW','WB')

print(ans)

Fixed submission:
S = list(input())
B = 0
ans = 0

for i in range(len(S)):
    if S[i] == 'B':
        B += 1
    else:
        ans += B

print(ans)
Buggy python submission (verdict: Runtime Error):
import copy
import bisect
import math

N, K = map(int, input().split())
a = list(map(int, input().split()))

s = [a[0]]
for i in range(1, N):
    s.append(s[i-1] + a[i])
ps = copy.copy(s)
for i in range(N):
    ps = [ps[j] - ps[0] for j in range(1, N-i)]
    s += ps

ans = 0
s.sort()

while s[-1] != 0:
    n = int(math.log2(s[-1]))
    i = bisect.bisect_left(s, 2**n)
    update = False
    while len(s) - i < K - 1:
        n -= 1
        i = bisect.bisect_left(s, 2**n)
        update = True
    if not update:
        if len(s) - i == K - 1:
            temp = s[-1]
            for i in range(1, K):
                temp &= s[-i-1]
            print(ans + temp)
            exit()
        else:
            ans += 2**n
            s = [x & (2**n-1) for x in s[i:]]
    else:
        s = [x & (2**n-1) for x in s]
    s.sort()
print(ans)

Fixed submission:
import copy
import bisect
import math

N, K = map(int, input().split())
a = list(map(int, input().split()))

s = [a[0]]
for i in range(1, N):
    s.append(s[i-1] + a[i])
ps = copy.copy(s)
for i in range(N):
    ps = [ps[j] - ps[0] for j in range(1, N-i)]
    s += ps

ans = 0
s.sort()

while s[-1] != 0:
    n = int(math.log2(s[-1]))
    ind = bisect.bisect_left(s, 2**n)
    if len(s) - ind == K:
        temp = s[-1]
        for i in range(1, K):
            temp &= s[-i-1]
        print(ans + temp)
        exit()
    elif len(s) - ind > K:
        ans += 2**n
        s = [x & (2**n-1) for x in s[ind:]]
    else:
        s = [x & (2**n-1) for x in s]
    s.sort()
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N,M = (int(x) for x in input().split())
P,Y=[],[]
ID = [0 for i in range(M)]
for i in range(M):
    p,y = (int(x) for x in input().split())
    P.append(p)
    Y.append(y)

Ys = sorted(Y)
num = [1 for i in range(N)]

for i in range(M):
    ind = Y.index(Ys[i])
    Pi = P[ind]
    ID[ind] = str(Pi).zfill(6) + str(num[Pi-1]).zfill(6)
    num[Pi-1] += 1

for i in range(M):
    print(ID[i])

Fixed submission:
N,M = map(int, input().split())
city, ID = [[] for _ in range(N+1)], [0 for _ in range(M)]
for i in range(M):
    P, Y = map(int, input().split())
    city[P].append([Y,i])
for i in range(1, N+1):
    city[i].sort()
    for j in range(len(city[i])):
        ID[city[i][j][1]] = str(i).zfill(6) + str(j+1).zfill(6)
for x in ID:    print(x)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
p = []
ph = []
for i in range(N):
    x,y,h = map(int,input().split())
    p += [[x,y]]
    ph += [h]

hmaxi = ph.index(max(ph))
pmax = p.pop(hmaxi)
hmax = ph.pop(hmaxi)
H,d = hmax,0
ans = False

while not ans:
    C = [[pmax[0]+d, pmax[1]], [pmax[0]-d, pmax[1]], [pmax[0], pmax[1]+d], [pmax[0], pmax[1]-d]]
    for i in range(d-1):
        C += [[pmax[0]+i, pmax[1]+i]]
        C += [[pmax[0]+i, pmax[1]-i]]
        C += [[pmax[0]-i, pmax[1]+i]]
        C += [[pmax[0]-i, pmax[1]-i]]
    for xy in C:
        ans = True
        for i in range(N-1):
            if ph[i] != H - abs(p[i][0]-xy[0]) - abs(p[i][1]-xy[1]):
                ans = False
                break
        if ans:
            print(xy[0],xy[1],H)
            break
    d += 1
    H += 1


Fixed submission:
N = int(input())
x,y,h = [],[],[]
for i in range(N):
    X,Y,H = map(int,input().split())
    x += [X]
    y += [Y]
    h += [H]
a = 0
while h[a] == 0:
    a += 1 
    if a == N:
        for i in range(101):
            if i not in x:  X = i
        for i in range(101):
            if i not in y:  Y = i
        print(X,Y,1)
        exit()

for cx in range(101):
    for cy in range(101):
        H = h[a] + abs(x[a]-cx) + abs(y[a]-cy)
        ans = True
        R = list(range(N))[0:a] + list(range(N))[a+1:N]
        for i in R:
            if h[i] != max(H - abs(x[i]-cx) - abs(y[i]-cy),0):
                ans = False
                break
        if ans:
            print(cx,cy,H)
            break
    else:
        continue
    break
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())

if M % N == 0:    print(M//N)
else:
    for i in range(N,M//2+1):
        if M % i == 0:
            print(M//i)
            break
    else:
        print(1)


Fixed submission:
N,M = map(int,input().split())

if M % N == 0:    print(M//N)
elif M//N > 100000:
    for i in range(N,M//2+1):
        if M % i == 0:
            print(M//i)
            break
    else:
        print(1)
else:
    m = M//N
    for i in range(2,m)[::-1]:
        if M % i == 0:
            print(i)
            break
    else:
        print(1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())

if M % N == 0:    print(M//N)
else:
    m = M//N
    for i in range(2,m)[::-1]:
        if M % i == 0:
            print(i)
            break
    else:
        print(1)


Fixed submission:
N,M = map(int,input().split())

if M % N == 0:    print(M//N)
elif M//N > 100000:
    for i in range(N,M//2+1):
        if M % i == 0:
            print(M//i)
            break
    else:
        print(1)
else:
    m = M//N
    for i in range(2,m)[::-1]:
        if M % i == 0:
            print(i)
            break
    else:
        print(1)
Buggy python submission (verdict: Runtime Error):
import math

N,X = (int(x) for x in input().split())
x = [int(p) for p in input().split()]
x.append(X)
x.sort()
D = x[1] - x[0]

for i in range(1,N-1):
    D = math.gcd(D,x[i+1]-x[i])

print(D)

Fixed submission:
import fractions

N,X = (int(x) for x in input().split())
x = [int(p) for p in input().split()]
x.append(X)
x.sort()
D = x[1] - x[0]

for i in range(1,N):
    D = fractions.gcd(D,x[i+1]-x[i])

print(D)
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
p = [0] + [int(x) for x in input().split()]

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    def find(self, x):
        if self.par[x] == x:    return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:    self.rank[x] += 1

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

UF = UnionFind(N)
for _ in range(M):
    x,y = map(int,input().split())
    UF.union(x, y)

unionUF, unionp, par = [], [], []
for i in range(1, N+1):
    temp = UF.find(i)
    if temp in par:
        unionUF[par.index(temp)].add(i)
        unionp[par.index(temp)].add(p[i])
    else:
        par.append(temp)
        unionUF.append(set([i]))
        unionp.append(set([p[i]]))

ans = 0
for i in range(len(unionUF)):
    ans += len(unionUF[i] & unionp[i])
print(ans)

Fixed submission:
N,M = map(int,input().split())
p = [0] + [int(x) for x in input().split()]

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    def find(self, x):
        if self.par[x] == x:    return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:    self.rank[x] += 1

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

UF = UnionFind(N)
for _ in range(M):
    x,y = map(int,input().split())
    UF.union(x, y)

unionUF, unionp = [set() for _ in range(N+1)], [set() for _ in range(N+1)]
for i in range(1, N+1):
    temp = UF.find(i)
    unionUF[temp].add(i)
    unionp[temp].add(p[i])

ans = 0
for i in range(len(unionUF)):
    ans += len(unionUF[i] & unionp[i])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = [list(input()) for _ in range(N)]
ans = 0

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

print(ans)

Fixed submission:
N = int(input())
S = [list(input()) for _ in range(N)]
#N = 300
#S = [["a"]*N for _ in range(N)]
ans = 0

for A in range(N):
    if all([S[(i+A)%N][j] == S[(j+A)%N][i] for i in range(N) for j in range(i+1, N)]):
        ans += N
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

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

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

print(ans)

Fixed submission:
N = int(input())
S = [list(input()) for _ in range(N)]
#N = 300
#S = [["a"]*N for _ in range(N)]
ans = 0

for A in range(N):
    if all([S[(i+A)%N][j] == S[(j+A)%N][i] for i in range(N) for j in range(i+1, N)]):
        ans += N
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

N = int(input())
S = [list(input()) for _ in range(N)]
S = copy.copy(S) + copy.copy(S)
ans = 0

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

print(ans)

Fixed submission:
N = int(input())
S = [list(input()) for _ in range(N)]
#N = 300
#S = [["a"]*N for _ in range(N)]
ans = 0

for A in range(N):
    if all([S[(i+A)%N][j] == S[(j+A)%N][i] for i in range(N) for j in range(i+1, N)]):
        ans += N
print(ans)