Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from collections import deque

INF = float("inf")

H, W, K = map(int, input().split())
A = [list(input()) for _ in range(H)]

Si, Sj = (0, 0)
for i, a in enumerate(A):
    if "S" in a:
        Si, Sj = (i, a.index("S"))
        break

dist = [[INF] * W for _ in range(H)]
dist[Si][Sj] = 0


ans = INF

Q = deque([(Si, Sj)])
while Q:
    i, j = Q.popleft()

    ans = min(
        ans,
        1 + (i + K - 1) // K,
        1 + (j + K - 1) // K,
        1 + (H - 1 - i + K - 1) // K,
        1 + (W - 1 - j + K - 1) // K
    )

    if dist[i][j] == K:
        continue

    distance = dist[i][j] + 1

    for di, dj in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        ni, nj = i + di, j + dj

        if not (0 <= ni < H and 0 <= nj < W and A[ni][nj] == "."):
            continue

        A[ni][nj] = "#"

        if dist[ni][nj] > distance:
            dist[ni][nj] = distance
            Q.append((ni, nj))

print(ans)


### FIXED CODE ###

from collections import deque

INF = float("inf")

H, W, K = map(int, input().split())
A = [list(input()) for _ in range(H)]

Si, Sj = (0, 0)
for i, a in enumerate(A):
    if "S" in a:
        Si, Sj = (i, a.index("S"))
        break

dist = [[INF] * W for _ in range(H)]
dist[Si][Sj] = 0


ans = INF

Q = deque([(Si, Sj)])
while Q:
    i, j = Q.popleft()

    ans = min(
        ans,
        1 + (i + K - 1) // K,
        1 + (j + K - 1) // K,
        1 + (H - 1 - i + K - 1) // K,
        1 + (W - 1 - j + K - 1) // K
    )

    if dist[i][j] == K:
        continue

    distance = dist[i][j] + 1

    for di, dj in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        ni, nj = i + di, j + dj

        if not (0 <= ni < H and 0 <= nj < W and A[ni][nj] == "."):
            continue

        A[ni][nj] = "#"

        if dist[ni][nj] > distance:
            dist[ni][nj] = distance
            Q.append((ni, nj))

print(ans)
### BUGGY CODE ###

from math import factorial
#a = factorial(n) / factorial(r) / factorial(n - r)
n,m = map(int,input().split())
l = list(map(int,input().split()))
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
l.sort()
ans = 0
mod = 10**9+7
mi = 0
for i in range(n-m+1):
    com = combination(n-i-1,m-1)
    ans += com*(l[-i-1] -l[i])
    
    ans %= mod
    
    
print(ans%mod)


### FIXED CODE ###

n,k = map(int,input().split())
l = list(map(int,input().split()))

mod = 10**9+7

fact = [1]
for i in range(n):
    fact.append(fact[-1]*(i+1)%mod)

finv = [1]*(n+1)
finv[n] = pow(fact[-1],mod-2,mod)
for i in range(n-1,0,-1):
    finv[i] = ((i+1)*finv[i+1])%mod

def combi(n,k,mod):
    return (fact[n]*finv[k]*finv[n-k])%mod

l.sort()

ans = 0
for i in range(n-k+1):
    tmp = combi(n-i-1,k-1,mod)
    ans += tmp*(l[-i-1]-l[i])
    ans %= mod

print(ans%mod)
### BUGGY CODE ###

N = int(input())
S = input()
ans = 0

for i in range(N):
    for j in range(i, N):
        cand = S[i:j]
        if S.find(cand, j) != -1:
            ans = max(ans, j-i)

print(ans)

### FIXED CODE ###

def z_algo(N, S):
    i = 0 # start 
    j = 1 # end
    ml = 0 # max length

    while i < N:
        if j-i > N-j:
            break
        
        sw = S[i:j]

        if sw in S[j:]:
            ml = max(ml, len(sw))
            j += 1
        else:
            i += 1

    return ml


N = int(input())
S = input()

print(z_algo(N, S))
### BUGGY CODE ###

X = map(int,input().split())
#X=7
P = {0}
i=1
while True:
    Q = {0}
    for p in P:
        Q.add(p+i)
        Q.add(p-i)
    P = P.union(Q)
    if X in P:
        print(i)
        break
    i = i+1


### FIXED CODE ###

X = int(input())
a = 0
while X >0:
    a += 1
    X -= a
print (a)
### BUGGY CODE ###

N=int(input())
a=list(map(int,input().split()))
d=min(abs(sum(a)-2*sum(a[0:i])) for i in range(N))
print(d)

### FIXED CODE ###

N=int(input())
a=list(map(int,input().split()))
s=sum(a)
i=0
while s>0:
  s-=2*a[i]
  i+=1
d=min(abs(s),s+2*a[i-1])
print(d)
### BUGGY CODE ###

h,w,m=map(int, input().split())
from collections import defaultdict
hd=defaultdict(int)
wd=defaultdict(int)
hd2=defaultdict(list)
wd2=defaultdict(list)
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hd2[h0].append(w0)
    wd[w0]+=1
    wd2[w0].append(h0)

max_h_list = [kv[0] for kv in hd.items() if kv[1] == max(hd.values())]
max_w_list = [kv[0] for kv in wd.items() if kv[1] == max(wd.values())]

ans=float('inf')

if len(max_h_list)<=len(max_w_list):
    for x in max_h_list:
        hlist=hd2[x]
        for y in max_w_list:
            if y in hlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
else:
    for x in max_w_list:
        wlist=wd2[x]
        for y in max_h_list:
            if y in wlist:
                ans=min(ans,1)
            else:
                ans=min(ans,0)
print(hd[max_h_list[0]]+wd[max_w_list[0]]-ans)


### FIXED CODE ###

import sys
from collections import defaultdict
input = sys.stdin.readline
h,w,m=map(int, input().split())
hd=[0 for i in range(h+1)]
wd=[0 for i in range(w+1)]
hw=defaultdict(int)
hmax=0
wmax=0
for i in range(m):
    h0,w0=map(int, input().split())
    hd[h0]+=1
    hmax=max(hmax,hd[h0])
    wd[w0]+=1
    wmax=max(wmax,wd[w0])
    hw[(h0,w0)]+=1

max_h_list=[]
max_w_list=[]
for k in range(h+1):
    if hd[k]==hmax:
        max_h_list.append(k)

for k in range(w+1):
    if wd[k]==wmax:
        max_w_list.append(k)

ans=float('inf')

for x in max_h_list:
    for y in max_w_list:
        if hw[(x,y)]==1:
            ans=min(ans,1)
        else:
            ans=min(ans,0)
            break
print(hmax+wmax-ans)
### BUGGY CODE ###

s = input()
n = int(input())

for i in range(n):
    que = input()
    if(len(que) == 1):
        s = s[::-1]
    else:
        que = que.split()
        if(que[1]=="1"):
            s = que[2] + s
        else:
            s = s + que[2]

print(s)

### FIXED CODE ###

s = input()

flag = 0
cnt = 0
n = int(input())

que_ar = [input() for _ in range(n)]

f_x = ["" for _ in range(n)]
b_x = ["" for _ in range(n)]


f = n-1
b = 0

for i in range(n):
    que = que_ar[i]
    if(len(que) == 1):
        cnt += 1
    else:
        que = que.split()
        if(cnt%2 == 0):
            if(que[1]=="1"):
                f_x[f] = que[2]
                f -= 1
            else:
                b_x[b] = que[2]
                b += 1
        else:
            if(que[1]=="1"):
                b_x[b] = que[2]
                b += 1
            else:
                f_x[f] = que[2]
                f -= 1

s = "".join(f_x)+s+"".join(b_x)             
                
if(cnt%2 == 0):
    print(s)
else:
    print(s[::-1])
### BUGGY CODE ###

import copy
import collections

n = int(input())
a = list(map(int, input().split()))
ans = 0

for i in range(n):
    b = copy.copy(a)
    del b[i]
    c = collections.Counter(b)
    d = c.most_common()
    values, counts = zip(*c.most_common())
    e = (counts)
    for j in range(len(e)):
        f = e[j]
        ans = ans + (f*(f - 1)/2)
    print(int(ans))
    ans = 0

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
b = [0]*n
ans = 0

for i in range(n):
    b[a[i] - 1] += 1

for i in range(n):
    ans += b[i]*(b[i] - 1)//2

for i in range(n):
    print(ans - b[a[i] -1] + 1)
### BUGGY CODE ###

N, M = map(int,input().split())
data = []
for i in range(M):
    data.append(int(input()))
dp = [0] * (N + 1)
dp[0] = 1
dp[1] = 1
if 1 in data:
    dp[1] = 0
mod = 10**9 + 7
for i in range(2,N + 1):
    if not i in data:
        dp[i] = dp[i - 1] + dp[i - 2]
print(dp[N] % mod)

### FIXED CODE ###

N, M = map(int,input().split())
data = []
dp = ['.' for i in range(N + 1)]
dp[0] = 1
dp[1] = 1
for i in range(M):
    k = int(input())
    data.append(k)
    dp[k] = 0
mod = 10**9 + 7
for i in range(2,N + 1):
    if dp[i] != 0:
        dp[i] = (dp[i - 1] + dp[i - 2]) % mod
print(dp[N] % mod)
### BUGGY CODE ###

#!/usr/bin/env python3

N, W = list(map(int, input().split()))
item_list = []

for _ in range(N):
    w, v = list(map(int, input().split()))
    item_list.append((w, v))

dp_table = [[0] * (W + 1) for _ in range(N + 1)]

for i in range(1, N + 1):
    w, v = item_list[i - 1]
    for j in range(W + 1):
        # 品物iを入れない場合
        before_best_value = dp_table[i - 1][j]
        dp_table[i][j] = before_best_value
        # 品物iを入れる場合
        if j - w < 0:
            continue
        value = dp_table[i - 1][j - w] + v
        best_value = max(before_best_value, value)
        dp_table[i][j] = best_value

ans = max(dp_table[-1])
print(ans)


### FIXED CODE ###

#!/usr/bin/env python3

N, W = list(map(int, input().split()))
item_list = []

for _ in range(N):
    w, v = list(map(int, input().split()))
    item_list.append((w, v))

dp_table = [[0] * (W + 1) for _ in range(N + 1)]

for i in range(1, N + 1):
    w, v = item_list[i - 1]
    for j in range(W + 1):
        # 品物iを入れない場合
        before_best_value = dp_table[i - 1][j]
        dp_table[i][j] = before_best_value
        # 品物iを入れる場合
        if j - w < 0:
            continue
        value = dp_table[i - 1][j - w] + v
        best_value = max(before_best_value, value)
        dp_table[i][j] = best_value

ans = max(dp_table[-1])
print(ans)
### BUGGY CODE ###

from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

S = sum(A)
ans = []
s = 0
for k in range(int((N - 1) / 2)):
    s += A[2 * k + 1] * 2
ans.append(S - s)
for k in range(N - 1):
    ans.insert(0, A[k] * 2 - ans[0])
ans.reverse()
print(*ans)


### FIXED CODE ###

from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]

S = sum(A)
ans = [0 for _ in range(N)]
s = 0
for k in range(int((N - 1) / 2)):
    s += A[2 * k + 1] * 2
ans[0] = S - s
for k in range(1, N):
    ans[k] = A[k - 1] * 2 - ans[k - 1]
print(*ans)
### BUGGY CODE ###

import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque

k = ni()
que = deque([(1,1)])

kmod = [0]*k

while kmod[0] == 0:
    cost, cur = que.popleft()
    if kmod[cur%k] == 0:
        kmod[cur%k] = cost
        if kmod[(10*cur)%k] == 0:
            que.appendleft((cost, 10*cur))
        if kmod[(cur+1)%k] == 0:
            que.append((cost+1, cur+1))            
        
print(kmod[0])

### FIXED CODE ###

from collections import deque

def bfs(K:int) -> list:
    hasChecked = [False]*(K+1)
    cost_list = [0]*(K+1)
    cost_list[1] = 1
    
    que = deque([(cost_list[1], 1)])
    
    while que:
        cost, res = que.popleft()
        if hasChecked[res]:
            continue
        
        hasChecked[res] = True
        cost_list[res] = cost
        
        if not hasChecked[10*res%K]:
            que.appendleft((cost, 10*res%K))
            
        if not hasChecked[(res+1)%K]:
            que.append((cost+1, (res+1)%K))
            
            
    return cost_list


K = int(input())
cost_list = bfs(K)
print(cost_list[0])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000000)

N, Q = list(map(int, input().split()))
graph = [[] for i in range(N)]
# 最初にroot pointを定めて:
# graph構造に大して点を与える...
for i in range(N-1):
    a, b = list(map(int, input().split()))
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

points = [0] * N
for _ in range(Q):
    p, x = list(map(int, input().split()))
    points[p-1] += x

visited = [False] * N

nodes = graph[0]
visited[0] = True


def dfs(old_node, node):
    if visited[node]:
        return
    points[node] += points[old_node]
    visited[node] = True
    nodes = graph[node]
    for _node in nodes:
        dfs(node, _node)
for _node in nodes:
    dfs(0, _node)

print(*points)
    

### FIXED CODE ###

import sys
from queue import deque
input = sys.stdin.readline

def main():
    N, Q = list(map(int, input().split()))
    graph = [[] for i in range(N)]
    # 最初にroot pointを定めて:
    # graph構造に大して点を与える...
    for i in range(N-1):
        a, b = list(map(int, input().split()))
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    
    points = [0] * N
    for _ in range(Q):
        p, x = list(map(int, input().split()))
        points[p-1] += x
    
    visited = [False] * N
    
    nodes = deque([0])

    while nodes:
        node = nodes.pop()
        visited[node] = True
        for _node in graph[node]:
            if visited[_node]:
                continue
            points[_node] += points[node]
            nodes.append(_node)
    
    print(*points)
    
main()
### BUGGY CODE ###

a, b = list(map(int, input().split()))

def gcd(num):
    k = []
    for i in range(1, num+1):
        if num%i == 0:
            if is_prime(i):
                k.append(i) 
    return k

def is_prime(num):
    for i in range(2, num):
        if num % i == 0:
            return False
    return True

ka = gcd(a)
kb = []
for i in ka:
  if b%i == 0:
    kb.append(i)

print(len(kb))

### FIXED CODE ###

def f(n):
    res = set()
    for i in range(2, int(n ** 0.5)):
        if n % i: continue
        while not n % i: n //= i
        res.add(i)
    return res | {n}
 
a, b = map(int,input().split())
print(len(f(a) & f(b) | {1}))
### BUGGY CODE ###

n = int(input())
p = list(map(int, input().split()))
d = [min(i, n - i - 1, j, n - j - 1) for i in range(n) for j in range(n)]
ans = 0
l = [1] * (n ** 2)
for i in p:
    ans += d[i - 1]
    l[i - 1] = 0
    que = [i - 1]
    while que:
        s = que.pop()
        if not s % n == n - 1:
            if d[s + 1] > d[s] + l[s]:
                que.append(s + 1)
                d[s + 1] = d[s] + l[s]
        if s % n:
            if d[s - 1] > d[s] + l[s]:
                que.append(s - 1)
                d[s - 1] = d[s] + l[s]
        if s >= n:
            if d[s - n] > d[s] + l[s]:
                que.append(s - n)
                d[s - n] = d[s] + l[s]
        if n ** 2 > s + n:
            if d[s + n] > d[s] + l[s]:
                que.append(s + n)
                d[s + n] = d[s] + l[s]
print(ans)

### FIXED CODE ###

n = int(input())
p = list(map(int, input().split()))
d = [min(i, n - i - 1, j, n - j - 1) for i in range(n) for j in range(n)]
ans = 0
l = [1] * (n ** 2)
for i in p:
    ans += d[i - 1]
    l[i - 1] = 0
    que = [i - 1]
    while que:
        s = que.pop()
        if not s % n == n - 1:
            if d[s + 1] > d[s] + l[s]:
                que.append(s + 1)
                d[s + 1] = d[s] + l[s]
        if s % n:
            if d[s - 1] > d[s] + l[s]:
                que.append(s - 1)
                d[s - 1] = d[s] + l[s]
        if s >= n:
            if d[s - n] > d[s] + l[s]:
                que.append(s - n)
                d[s - n] = d[s] + l[s]
        if n ** 2 > s + n:
            if d[s + n] > d[s] + l[s]:
                que.append(s + n)
                d[s + n] = d[s] + l[s]
print(ans)
### BUGGY CODE ###

from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)+1
    total = sum(H)
    bottom = 1
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid+1
        else:
            top = mid+1
        mid = (top+bottom)//2
    ans = mid
    return ans
print(solve())

### FIXED CODE ###

from math import ceil
def solve():
    N, A, B = map(int, input().split())
    H = [int(input()) for _ in range(N)]
    top = max(H)
    total = sum(H)
    bottom = 0
    mid = (top+bottom)//2
    while bottom+1<top:
        cnt = 0
        for i in range(N):
            cnt += max(ceil((H[i]-B*mid)/(A-B)),0)
        if cnt>mid:
            bottom = mid
        else:
            top = mid
        mid = (top+bottom)//2
    ans = top
    return ans
print(solve())
### BUGGY CODE ###

import bisect
inf = 10**15
mod = 10**9+7
n, m = map(int, input().split())
ken = [[] for i in range(n)]
py = [list(map(int, input().split())) for i in range(m)]
for i in range(m):
    p, y = py[i]
    bisect.insort_left(ken[p-1], y)
for i in range(m):
    p, y = py[i]
    ind = bisect.bisect_left(ken[p-1], y)
    print('{ans:06}{anse:06}'.format(ans=p, anse=ind+1))

### FIXED CODE ###

import bisect
inf = 10**15
mod = 10**9+7
n, m = map(int, input().split())
ken = [[] for i in range(n)]
py = [list(map(int, input().split())) for i in range(m)]
for i in range(m):
    p, y = py[i]
    ken[p-1].append(y)
for tmp in ken:
    tmp.sort()
for i in range(m):
    p, y = py[i]
    ind = bisect.bisect_left(ken[p-1], y)
    print('{ans:06}{anse:06}'.format(ans=p, anse=ind+1))
### BUGGY CODE ###

n = int(input())
a = input().split()
k = len(set(a))
ans = 0
if k == 1:
    ans += n//2
else:
    # 偶奇でリストを分割
    # それぞれで最も多い要素を選択
    # 選択した要素が異なるならOK
    # 少ない方を二番目に変更
    od = a[::2]
    ev = a[1::2]
    k_od = list(set(od))
    k_ev = list(set(ev))
    c_od = [(od.count(i),i) for i in k_od]
    c_ev = [(ev.count(i),i) for i in k_ev]
    c_od.sort()
    c_ev.sort()
    if c_od[-1][1] != c_ev[-1][1]:
        ans += n-c_od[-1][0]-c_ev[-1][0]
    else:
        ans += n-max(c_od[-1][0],c_ev[-1][0])
        ans -= c_od[-2][0] if c_od[-1][0]>=c_ev[-1][0] else c_ev[-2][0]
print(ans)

### FIXED CODE ###

import itertools
from collections import Counter
N = int(input())
V = list(map(int,input().split()))
even_counter = Counter(V[::2])
odd_counter = Counter(V[1::2])
even_counter[-1] = 0
odd_counter[-1] = 0
even = even_counter.most_common(2)
odd = odd_counter.most_common(2)
ans = N
for (k1,v1),(k2,v2) in itertools.product(even,odd):
  if k1 == k2:
    continue
  ans = min(ans,N - v1 - v2)
print(ans)
### BUGGY CODE ###

n,k,q = map(int,input().split(" "))
point = [k for i in range(n)]

for i in range(q):
    ans = int(input())
    point=list(map(lambda x:x-1,point))
    point[ans-1] += 1

for i in range(n):
    if(point[i] <= 0):
        print("No")
    else:
        print("Yes")

### FIXED CODE ###

n,k,q = map(int,input().split(" "))
point = [k-q for i in range(n)]

for i in range(q):
    ans = int(input())
    point[ans-1] += 1

for i in range(n):
    if(point[i] <= 0):
        print("No")
    else:
        print("Yes")
### BUGGY CODE ###

import math

N = int(input())
tmp = 0
sum = 0
for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N+1):
            tmp = math.gcd(i,j)
            sum = sum + math.gcd(tmp,k)
            
print(sum)

### FIXED CODE ###

import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N = int(input())
a = 0
for i in range(1,N+1):
    for j in range(1,N+1):
        for k in range(1,N+1):
            a = a + int(gcd(i,j,k))
print(a)
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
ans=[0 for i in range(n)]

for i in range(n):
  ans[a[i]-1]=i+1
  
l=""  
for i in ans:
  l=l+str(i)+" "
  
  
print(l)

### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
dic={}
for i in range(n):
  dic[i+1]=a[i]
 
dic2 = sorted(dic.items(), key=lambda x:x[1])
 
ans=""
for i in dic2:
  print(i[0],end=" ")
  
  
  
print(ans)
### BUGGY CODE ###

s=input()
q=int(input())
for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        s=s[::-1]
    else:
        if a[1]=='1':
            s=a[2]+s
        else:
            s=s+a[2]
print(s)

### FIXED CODE ###

from collections import deque
s=deque(list(input()))
q=int(input())
rev=1

for i in range(q):
    a=list(input().split())
    if a[0]=='1':
        rev*=-1
    else:
        if a[1]=='1':
            if rev>0:
                s.appendleft(a[2])
            else:
                s.append(a[2])
        else:
            if rev>0:
                s.append(a[2])
            else:
                s.appendleft(a[2])

a=[]
for i in range(len(s)):
    a+=s.popleft()    
if rev<0:
    a=a[::-1]
print(''.join(a))
### BUGGY CODE ###

n, m = input().split()
n = int(n)
m = int(m)
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]

s = 0

for i in range(n-1):
    for j in range(i+1, n):
        for k in range(m-1):
            for l in range(k+1,m):
                tate = x[j] - x[i]
                yoko = y[l] - y[k]
                s += tate * yoko

s %= (10**9 + 7)

print(s)


### FIXED CODE ###

n, m = input().split()
n = int(n)
m = int(m)
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]

def sum(a, n):
    sum_a = 0
    for i in range(0, n):
        sum_a += i * a[i] - (n-1-i) * a[i]
    return sum_a

sum_x = sum(x,n)
sum_y = sum(y,m)

s = sum_x * sum_y % (10**9 + 7)

print(s)
### BUGGY CODE ###

n=input()
s=[]
for i in range(n):
	s+=[raw_input()]
result=0
for shift in range(n):
	ok=True
	for i in range(n):
		for j in range(n):
			if s[(i+shift)%n][j]!=s[(j+shift)%n][i]:
				ok=False
				break
	if ok:
		result+=n
print result

### FIXED CODE ###

n=input()
s=[raw_input() for i in range(n)]
t=[]
for i in range(n):
	t+=[s[j][i] for j in xrange(n)]*2
s+=s
s=''.join(s)
#print s
t=''.join(t)
#print t
result=0
for shift in xrange(n):
	ok=True
	for i in xrange(n):
		if s[(i+shift)*n: (i+shift)*n+n]!=t[i*n*2+shift: i*n*2+shift+n]:
			ok=False
			break
	if ok:
		result+=n
print result
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))

ans = 0
for i in range(N):
    for j in range(i+1, N):
        ans += A[i] * A[j]
        ans = ans % (10 ** 9 + 7)

print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))

ans = 0
tmp = sum(A)
for i in range(N):
    tmp -= A[i]
    ans += A[i] * tmp
    ans = ans % (10 ** 9 + 7)

print(ans)
### BUGGY CODE ###

import itertools
n=int(input())
num=[[i,j,k] for i,j,k in itertools.combinations(range(n),3) if (j-i)!=(k-j)]

s=input()
ans=0
for i,j,k in num:
    if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
        ans+=1
print(ans)

### FIXED CODE ###

n=int(input())
s=input()

nr=s.count('R')
ng=s.count('G')
nb=s.count('B')
ans=nr*ng*nb 
cnt=0

for i in range(n):
    for j in range(i+1,n):
        k=2*j-i
        if n<=k or k<=j:
            continue
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
            cnt+=1
print(ans-cnt)