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

n = int(input())
a = list(input().split())

if n == 1:
    print(1)
    print(1)
    exit()

MOD = 10 ** 9 + 7

b = sorted(a)

for i in range(n):
    if b[i] == b[i + 1]:
        d = b[i]
        break

first = a.index(d)
for i in range(first + 1, n + 1):
    if a[i] == d:
        second = i

w = n + first - second

print(n)
x = n + 1
y = 1
z = 1
for _ in range(n):
    x *= n + 1 - z
    x //= z + 1
    if z <= w:
        y *= w + 1 - z
        y //= z
    else:
        y = 0
    print((x - y) % MOD)
    z += 1


### FIXED CODE ###

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

class ModInv:
    def __init__(self, size, MOD):
        self.inv = [0] * (size + 1)
        self.inv[1] = 1
        for i in range(2, size + 1):
            self.inv[i] = (-(MOD // i) * self.inv[MOD%i]) % MOD

if n == 1:
    print(1)
    print(1)
    exit()

MOD = 10 ** 9 + 7

b = sorted(a)

for i in range(n):
    if b[i] == b[i + 1]:
        break

first = a.index(b[i])
for i in range(first + 1, n + 1):
    if a[i] == a[first]:
        second = i

w = n + first - second

inv = ModInv(n + 1, MOD)
c = [1] * (n + 2)
d = [1] * (n + 2)

for i in range(n + 1):
    d[i + 1] = (d[i] * inv.inv[i + 1]) % MOD
for i in range(n + 1):
    c[i + 1] = (c[i] * (i + 1)) % MOD

print(n)
for k in range(2, n + 2):
    ans = (((c[n+1] * d[n+1-k]) % MOD) * d[k]) % MOD
    if w >= k - 1:
        ans -= (((c[w] * d[w-k+1]) % MOD) * d[k-1]) % MOD
    print( ans % MOD )
### BUGGY CODE ###

import sys,math,copy
input = sys.stdin.readline

N=int(input().strip())
A=[int(x) for x in input().strip().split()]
def gcd(a,b):
    if b==0:
        return a
    
    return gcd(b,a%b)

ANS=[]
for i in range(len(A)):
    B=copy.deepcopy(A)
    if len(B)>1:
        B.pop(i)
    t=0
    for c in range(len(B)):
        t=gcd(B[c],t)
    ANS.append(t)

print(max(ANS))


### FIXED CODE ###

import sys,math,copy
from fractions import gcd
input = sys.stdin.readline

N=int(input().strip())
A=[int(x) for x in input().strip().split()]
# def gcd(a,b):
#     if b==0:
#         return a
    
#     return gcd(b,a%b)

ANS=[]
L=[0]
R=[0]
for i in range(len(A)):
    L.append(gcd(A[i],L[-1]))
    R.append(gcd(A[-1-i],R[-1]))

R.reverse()
for i in range(len(L)-1):
    ANS.append(gcd(L[i],R[i+1]))

print(max(ANS))
### BUGGY CODE ###

from collections import deque   ##deque 라는 데이터형 편리한듯. print도그렇고 append,pop속도도 빠르고
n = int(input())
xdeque = deque()
for i in range(n):
    command = list(map(str,input().split()))
    if (command[0] == 'insert'):
        x = command[1]
        xdeque.appendleft(int(x))
    elif (command[0] == 'delete'):
        x = command[1]
        try:
            xdeque.remove(int(x))
        except ValueError:
            pass
    elif (command[0] == 'deleteFirst'):
        xdeque.popleft()
    else:
        xdeque.pop()
print(*xdeque)




### FIXED CODE ###

from collections import deque
n = int(input())
a = deque()
for i in range(n):
    c = input()
    if c == "deleteFirst":
        a.popleft()
    elif c == "deleteLast":
        a.pop()
    else:
        c, v = c.split()
        if c == "insert":
            a.appendleft(v)
        else:
            try:
                a.remove(v)
            except:
                pass
print(" ".join(a))
### BUGGY CODE ###

N=int(input())
bar=10**12

def f(x):
  d=[]
  for i in range(1,int(x**0.5)+1):
    if x%i==0:
      d.append(i)
      if i!=x//i:
        d.append(x//i)
  d.sort()
  return d

for i in f(N):
  for j in f(N):
    if i*j==N and i+j<bar:
      bar=i+j
      x=i
      y=j

print(x-1+y-1)
 

### FIXED CODE ###

N=int(input())
bar=10**12
d=[]
for i in range(1,int(N**0.5)+1):
  if N%i==0 and i+N//i<bar:
    d.append(i+N//i)
    bar=i+N//i
#print(d)
print(min(d)-2)
### BUGGY CODE ###

from collections import deque
H, W = map(int, input().split())
cy, cx = map(int, input().split())
dy, dx = map(int, input().split())
cy -= 1
cx -= 1
dy -= 1
dx -= 1
d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
S = [input().strip() for _ in range(H)]

worps = [[float('inf')]*W for _ in range(H)]
q = deque([[cy, cx]])

worps[cy][cx] = 0
while q:
    y, x = q.popleft()
    worp = worps[y][x]

    for h, w in d:
        ny, nx = y+h, x+w
        if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == "." and worp < worps[ny][nx]:
            worps[ny][nx] = worp
            q.appendleft([ny, nx])

    worp += 1
    for h in range(-2+y, 3+y):
        for w in range(-2+x, 3+x):
            if 0 <= h < H and 0 <= w < W and S[h][w] == "." and worp < worps[h][w]:
                worps[h][w] = worp
                q.appendleft([h, w])

ans = worps[dy][dx]
if ans != float('inf'):
    print(ans)
else:
    print(-1)


### FIXED CODE ###

from collections import deque
H, W = map(int, input().split())
cy, cx = map(int, input().split())
dy, dx = map(int, input().split())
cy -= 1
cx -= 1
dy -= 1
dx -= 1
d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
S = [input() for _ in range(H)]

worps = [[float('inf')]*W for _ in range(H)]
q = deque([[cy, cx]])

worps[cy][cx] = 0
while q:
    y, x = q.popleft()
    worp = worps[y][x]

    for h, w in d:
        ny, nx = y+h, x+w
        if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == "." and worp < worps[ny][nx]:
            worps[ny][nx] = worp
            q.appendleft([ny, nx])

    worp += 1
    for h in range(-2+y, 3+y):
        for w in range(-2+x, 3+x):
            if 0 <= h < H and 0 <= w < W and S[h][w] == "." and worp < worps[h][w]:
                worps[h][w] = worp
                q.append([h, w])

ans = worps[dy][dx]
if ans != float('inf'):
    print(ans)
else:
    print(-1)
### BUGGY CODE ###

A,B=map(int,input().split())
n=0
tmp=B
while tmp>0:
    tmp=tmp//2
    n+=1
L=[]
for a in range(A,B+1):
    l=[]
    for i in range(n):
        if a%2==0:
            l.append(0)
        else:
            l.append(1)
        a=a//2
    L.append(l)
rv=[1 if sum(k)%2==1 else 0 for k in zip(*L)]
v=0
for i,b in enumerate(rv):
    v+=b*(2**i)
print(v)

### FIXED CODE ###

A,B=map(int,input().split())
A-=1
if A%4==1:
    A=1
elif A%4==2:
    A+=1
elif A%4==3:
    A=0
if B%4==1:
    B=1
elif B%4==2:
    B+=1
elif B%4==3:
    B=0
print(A^B)
### BUGGY CODE ###

from collections import deque
N = int(input())
edges = [[] for _ in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    edges[a].append((b,i))
    edges[b].append((a,i))

color = [None]*(N-1)
SIZE = 1
q = deque([(0,None)])
while q:
    v, prev_color = q.popleft()
    colors = list(range(1,SIZE+1))
    if prev_color:
        colors.remove(prev_color)
    for w, i in edges[v]:
        if not color[i]:
            if colors:
                color[i] = colors.pop(0)
            else:
                SIZE += 1
                color[i] = SIZE
            q.append((w,color[i]))
print(SIZE)
for n in range(N-1):
    print(color[n])

### FIXED CODE ###

if __name__ == "__main__":
    import sys
    from collections import deque
    input = sys.stdin.readline
    N = int(input())
    edges = [[] for _ in range(N)]
    for i in range(N-1):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        edges[a].append((b,i))
        edges[b].append((a,i))

    color = [None]*(N-1)
    SIZE = 1
    q = deque([(0,None)])
    while q:
        v, prev_color = q.popleft()
        c = 1
        for w, i in edges[v]:
            if not color[i]:
                if c == prev_color:
                    c += 1
                color[i] = c
                c += 1
                q.append((w,color[i]))
        SIZE = max(SIZE, c-1)
    print(SIZE)
    for n in range(N-1):
        print(color[n])
### BUGGY CODE ###

import bisect

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

list.sort(A)  # 昇順にソート

for i in range(M):
    A[N-1] = A[N-1] // 2
    tmp = A[N-1]
    del A[N-1]
    insert_index = bisect.bisect_left(A, tmp)
    A.insert(insert_index, tmp)
    
print(sum(A))

### FIXED CODE ###

import heapq

N, M = map(int, input().split())
A = list(map(int, input().split()))
hq = []

for i in range(N):
    heapq.heappush(hq, A[i]*(-1))

for i in range(M):
    tmp = heapq.heappop(hq) * (-1) // 2
    heapq.heappush(hq, tmp*(-1))
    
print(sum(hq)*(-1))
### BUGGY CODE ###

def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a = sorted(a)
    for i in range(m):
        a[-1] //= 2
        a.sort()
    print(sum(a))


if __name__ == '__main__':
    main()

### FIXED CODE ###

import heapq


def main():
    n, m = map(int, input().split())
    a = list(map(lambda x: int(x)*(-1), input().split()))
    heapq.heapify(a)
    for i in range(m):
        item = heapq.heappop(a)
        heapq.heappush(a, (-1) * (-item // 2))
    print(-sum(a))


if __name__ == '__main__':
    main()
### BUGGY CODE ###

# -*- coding:utf-8 -*-

def solve():
    n = int(input())
    A = input().split()

    """方針
    A = 0 6 7 6 7 0
    i | b
    ---------------
    1 | 0
    2 | 0 6 (本当は逆で6 0)
    3 | 7 0 6
    4 | 7 0 6 6 (本当は逆で 6 6 0 7)
    5 | 7 7 0 6 6
    6 | 7 7 0 6 6 0 (本当は逆で0 6 6 0 7 7) 

    ・iが偶数のとき、bの後ろにA[i]を追加する
    ・iが奇数のとき、bの前にA[i]を追加する

    上の操作をn回すべてしたあとに、

    ・nが偶数なら答えを反転する
    ・nが奇数なら答えはそのまま

    ただし、A[i]は2桁以上の数字の可能性も考慮すること
    """

    # 1 2 3 -> 3 1 2
    b = []
    for i in range(1, n+1):
        if i%2 == 0:
            b.append(A[i-1])
        else:
            b.insert(0, A[i-1])

    if n%2 == 0:
        ans = ""
        for i in range(n-1, -1, -1):
            ans += b[i] + " "
        print(ans[:-1])
    else:
        ans = " ".join(b)
        print(ans)


if __name__ == "__main__":
    solve()


### FIXED CODE ###

# -*- coding:utf-8 -*-
from collections import deque

def solve():
    n = int(input())
    A = input().split()

    """方針
    A = 0 6 7 6 7 0
    i | b
    ---------------
    1 | 0
    2 | 0 6 (本当は逆で6 0)
    3 | 7 0 6
    4 | 7 0 6 6 (本当は逆で 6 6 0 7)
    5 | 7 7 0 6 6
    6 | 7 7 0 6 6 0 (本当は逆で0 6 6 0 7 7) 

    ・iが偶数のとき、bの後ろにA[i]を追加する
    ・iが奇数のとき、bの前にA[i]を追加する

    上の操作をn回すべてしたあとに、

    ・nが偶数なら答えを反転する
    ・nが奇数なら答えはそのまま

    ただし、A[i]は2桁以上の数字の可能性も考慮すること

    あとPythonは、list.insert()はとても遅いので（TLEしたので）、代わりにcollections.dequeを使用する

    ・collections.deque
    https://docs.python.org/ja/3/library/collections.html#collections.deque

    dequeとは、「両端における append や pop を高速に行えるリスト風のコンテナ」
    """

    # 1 2 3 -> 3 1 2
    b = deque()
    for i in range(1, n+1):
        if i%2 == 0:
            b.append(A[i-1])
        else:
            b.appendleft(A[i-1])

    if n%2 == 0:
        ans = ""
        for i in range(n-1, -1, -1):
            ans += b[i] + " "
        print(ans[:-1])
    else:
        ans = " ".join(b)
        print(ans)


if __name__ == "__main__":
    solve()
### BUGGY CODE ###

N=int(input())
LST=[]
count=0
for i in range(N):
    a=input()
    lst=[]
    for j in range(len(a)):
        lst.append(a[j])
    LST.append(sorted(lst))
for i in range(N-1):
    for j in range(i+1,N):
        if LST[i]==LST[j]:
            count+=1
print(count)


### FIXED CODE ###

N=int(input())

count=0
dic=dict()

for i in range(N):
    s="".join(sorted(input()))
#    print(s)

    if s in dic:
        count+=dic[s]
#        print(dic[s])
        dic[s]+=1
    else:
        dic[s]=1
#        print(dic)

print(count)
### BUGGY CODE ###

import itertools
from itertools import permutations
N=int(input())
ab=list(list(map(int,input().split())) for _ in range(N))
cd=list(list(map(int,input().split())) for _ in range(N))
perab=list(itertools.permutations(ab,N))
l=[[0] for _ in range(len(perab))]
for mab in perab:
    for i in range(N):
        if cd[i][0]-mab[i][0]>0 and cd[i][1]-mab[i][1]>0:
            l[perab.index(mab)].append(1)
print(max(sum(ml) for ml in l))

### FIXED CODE ###

import copy
N=int(input())
ab=list(list(map(int,input().split())) for _ in range(N))
cd=list(list(map(int,input().split())) for _ in range(N))
ab.sort(key=lambda x :x[1],reverse=True)#abをy座標で逆ソート
#ab.sort(key=lambda x :x[0])#abをx座標でソート
cd.sort(key=lambda x :x[0])#cdをx座標でソート
ans=0
for c,d in cd:#あるc,dに対して、適切なabを探しともに消去する
    for a,b in ab:#
        if b<d and a<c:
            ab[ab.index([a,b])]=[1000,1000] #abは糞でかく、cdはクソ小さくする
            cd[cd.index([c,d])]=[-1000,-1000]
            ans+=1
            break#次のc,dを見に行く
print(ans)
### BUGGY CODE ###

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

for _ in range(min(K,200)):
    imos = [0] * (N+1)
    for i in range(N):
        l, r = max(i-A[i],0), min(i+A[i],N-1)
        imos[l] += 1
        imos[r+1] += -1
    
    for i in range(N): imos[i+1] += imos[i]
    
    A = imos[:N]

for a in A:
    print(a, end=" ")
print()



### FIXED CODE ###

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

for _ in range(K):
    flag = True
    for a in A:
        if a != N: flag = False
    if flag: break

    imos = [0] * (N+1)
    for i in range(N):
        l = max(i-A[i],0)
        r = min(i+A[i],N-1)
        imos[l] += 1
        imos[r+1] += -1
    
    for i in range(N):
        imos[i+1] += imos[i]
    
    A = imos[:N]

for a in A[:N]:
    print(a, end=" ")
print()
### BUGGY CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

K = len(bin(np.max(A))) - 2

ans = 0
mod = 10 ** 9 + 7
N %= mod

for i in range(K):
    bit = sum(A >> i & 1) % mod
    ans += pow(2, i, mod) * bit * (N - bit)
    ans %= mod
    
print(ans)

### FIXED CODE ###

import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

K = len(bin(np.max(A))) - 2

ans = 0
mod = 10 ** 9 + 7
N %= mod

for i in range(K):
    bit = np.count_nonzero(A >> i & 1) % mod
    ans += 2 ** i * bit * (N - bit)
    ans %= mod
    
print(ans)
### BUGGY CODE ###

N, M = [int(nm) for nm in input().split()]
A = []
for m in range(M):
  A.append(int(input()))

C = [1, 0] # [-2段目, -1段目]
C += [0]*(N+10) # C = [-2段目, -1段目, 0段目, 1段目, 2段目, ...]
for n in range(0, N+1):
  if n not in A:
    C[n+2] = (C[n] + C[n+1]) % 1000000007
  else: # n in A:
    C[n+2] = 0

ans = C[N+2]  % 1000000007
print(ans)

### FIXED CODE ###

N, M = [int(nm) for nm in input().split()]
if M != 0:
  a = int(input())

C = [1, 0] # [-2段目, -1段目]
C += [0]*(N+10) # C = [-2段目, -1段目, 0段目, 1段目, 2段目, ...]
for n in range(0, N+1):
  if M == 0:
    C[n+2] = (C[n] + C[n+1]) % 1000000007
  else: # M > 0:
    if n != a:
      C[n+2] = (C[n] + C[n+1]) % 1000000007
    else: # n == a:
      C[n+2] = 0
      M -= 1
      if M > 0:
        a = int(input())

ans = C[N+2]  % 1000000007
print(ans)
### BUGGY CODE ###

n, h, *t=map(int,open(0).read().split())
b_high = [i for i in sorted(t[1::2]) if all(i >= j for j in sorted(t[::2]))]
if (h - sum(b_high)) % sorted(t[::2])[-1] == 0:
  print(len(b_high) + (h - sum(b_high)) // sorted(t[::2])[-1])
else:
  print(len(b_high) + ((h - sum(b_high)) // sorted(t[::2])[-1]) + 1)


### FIXED CODE ###

n, h, *t=map(int,open(0).read().split())
a = t[::2]
b = t[1::2]
ma = max(a)
b_high = [i for i in b if i > ma]
if sum(b_high) > h:
  ans = 0
  b_high = sorted(b_high)
  while h > 0:
    h = h - b_high.pop()
    ans += 1
  print(ans)
else:
  if (h - sum(b_high)) % ma == 0:
    print(len(b_high) + (h - sum(b_high)) // ma)
  else:
    print(len(b_high) + ((h - sum(b_high)) // ma) + 1)
### BUGGY CODE ###

import numpy as np
n = int(input())
ary = [list(map(int,input().split())) for _ in range(n)]
ary = np.array(ary,dtype=int)
dp = np.zeros((n,3),dtype=int)
dp[0] = ary[0]
for i in range(1,n):
    for j in range(3):
        for k in range(3):
            if j == k:continue
            if dp[i][j] < dp[i-1][k]+ary[i][j]:
                dp[i][j] = dp[i-1][k] + ary[i][j]
print(max(dp[n-1]))

### FIXED CODE ###

nums = int(input())
ary = [0 for i in range(nums)]
for i in range(nums):
    ary[i] = list(map(int,input().split()))

dp = [0 for i in range(nums+1)]
for i in range(len(dp)):
    dp[i] = [0,0,0]

for i in range(nums):
    for x in range(3):
        for y in range(3):
            if x == y: continue
            check = dp[i][x] + ary[i][y]
            if check >= dp[i+1][y]:
                dp[i+1][y] = check

result= 0
for i in range(3):
    if result < dp[nums][i]:
        result = dp[nums][i]
print(result)
### BUGGY CODE ###

from sys import stdin
N = int(input())
A = list(map(int, stdin.readline().split()))
ans = 1
for a in A:
    ans = ans*a
    if ans == 0:
        break


if ans >10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

from sys import stdin
N = int(input())
A = list(map(int, stdin.readline().split()))
if 0 in A:
    print(0)
else:
    ans = 1
    for a in A:
        ans = ans*a
        if ans > 10**18:
            break
    """from functools import reduce
    ans = reduce(lambda x,y:x*y,A)"""
    """import numpy as np
    A = np.array(A)
    ans = np.prod(A)"""
    if ans >10**18:
        print(-1)
    else:
        print(ans)
### BUGGY CODE ###

from collections import Counter
n = int(input())
A = list(map(int,input().split()))
A = sorted(A)

s = set()
for a in A:
  for si in s:
    if a%si==0:
      break
  else:
    s.add(a)
      
ans = 0
C = Counter(A)
for si in s:
  if C[si]==1:
    ans += 1
print(ans)          

### FIXED CODE ###

n = int(input())
A = list(map(int,input().split()))
A = sorted(A)

limit = 10**6 + 2
A += [limit]
dp = [True] * (limit)

ans = 0
for i in range(n):
  a = A[i]
  if dp[a] and a != A[i+1]:
    ans += 1
  
  if dp[a] == False:
    continue
  for t in range(a,limit,a):    
    dp[t] = False
    t += a

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

def answer(X, Y):
    m = len(X)
    n = len(Y)
    C = [[0 for _ in range(n + 1)] for _ in range(m + 1)]

    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                C[i][j] = C[i - 1][j - 1] + 1
            else:
                C[i][j] = max(C[i][j - 1], C[i - 1][j])
    return C[m][n]


if __name__ == '__main__':
    q = int(input())
    for _ in range(q):
        X = input()
        Y = input()
        print(answer(X, Y))

### FIXED CODE ###

def lcs_better(X, Y):
    costs = [0]
    for c in Y:
        for i in range(len(costs) - 1, -1, -1):
            tmp = X.find(c, costs[i])
            if tmp + 1:
                if i + 1 < len(costs):
                    costs[i + 1] = min(costs[i + 1], tmp + 1)
                else:
                    costs.append(tmp + 1)
    return len(costs) - 1


def answer(X, Y):
    m, n = len(X), len(Y)
    C = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                C[i][j] = C[i-1][j-1] + 1
            else:
                C[i][j] = max(C[i][j -1], C[i -1][j])
    return C[m][n]


if __name__ == '__main__':
    q = int(input())
    for _ in range(q):
        X = input()
        Y = input()
        print(lcs_better(X, Y))
### BUGGY CODE ###

from itertools import product, combinations

n = int(input())

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

result = 0

for x, y in combinations(a, 2):
	result += x * y

print(result % (10 ** 9 + 7))

### FIXED CODE ###

from itertools import accumulate

MOD = 10 ** 9 + 7

n = int(input())

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

suffix_sum = list(accumulate(a[::-1]))[::-1]

req_arr = [x -  y for x, y in zip(suffix_sum, a)]

result = sum([(x * y) % MOD for x, y in zip(req_arr, a)])

print(result % MOD)
### BUGGY CODE ###

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

ans = 0

for n in range(N):
    for i in range(n-1):
        if H[i] - H[n] >= n - i:
            ans += 1
        else:
            pass

if ans == 0:
    print('Yes')
else:
    print('No')


### FIXED CODE ###

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

ans = 'Yes'

for i in reversed(range(0, N-1)):
    if H[i] > H[i+1] + 1:
        ans = 'No'
    elif H[i] == H[i+1] + 1:
        H[i] -= 1

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

n = int(input())
ans = 0
for i in range(n):
  ans += i
print(ans)

### FIXED CODE ###

n = int(input())

ans = 0
if n%2 != 0:
  ans = n*((n-1)//2)
else:
  ans = n*((n//2)-1) + n//2

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

#qに入れるlistをtupleにしてみる

from collections import deque 
def main():
  h,w=map(int,input().split()) 
  mp=[list(input()) for _ in range(h)]
  dist=[[-1]*w for i in range(h)]
  q=deque([])
  
  for i in range(h):
    for j in range(w):
      if mp[i][j]=="#":
        dist[i][j]=0
        q.append((i,j))
      else:
        dist[i][j]=-1
        
  while q:
    x,y=q.popleft()
    for i,j in ((1,0),(0,1),(-1,0),(0,-1)):
      xx,yy=x+i,y+j
      if (0<=xx<h and 0<=yy<w and dist[xx][yy]==-1):
        dist[xx][yy]=dist[x][y]+1
        q.append((xx,yy))
        
  ret=0
  for i in range(h):
    for j in range(w):
      ret=max(ret,dist[i][j])
      
  print(ret)
  return

main()

### FIXED CODE ###

#copipe
#1次元で格納

def main():
    h,w = map(int,input().split())
    a = []
    for i in range(h):
        a.append(input())

    used = [True]*(h*w)

    p = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == '#':
                p.append(i*w+j)
                used[i*w+j] = False


    
    res = 0
    while p:
        tank = []
        for e in p:
            if e>w-1 and used[e-w]:
                tank.append(e-w)
                used[e-w] = False
            if e%w > 0 and used[e-1]:
                tank.append(e-1)
                used[e-1] = False
            if e<(h-1)*w and used[e+w]:
                tank.append(e+w)
                used[e+w] = False
            if e%w<w-1 and used[e+1]:
                tank.append(e+1)
                used[e+1] =  False
        p = tank
        res += 1
    print(res-1)

if __name__ == '__main__':
    main()
### BUGGY CODE ###


a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

canArrive = False
i = 0
now = 0
while True:
    if now % 60 <= c and c <= (now + a) % 60:
        canArrive = True
        break
    #end if
    now += (a + b)
#end while
if canArrive:
    print c + 60 * (now // 60)
else:
    print -1
    #end if

#end while

### FIXED CODE ###


a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

canArrive = False
i = 0
now = 0
while True:
    if now % 60 <= c and c <= now % 60 + a:
        canArrive = True
        break
    #end if
    now += (a + b)
    if now % 60 == 0 : break
#end while
if canArrive:
    print c + 60 * (now // 60)
else:
    print -1
    #end if

#end while