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

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


def long_substr(data):
    substr = ''
    if len(data) > 1 and len(data[0]) > 0:
        for i in range(len(data[0])):
            for j in range(len(data[0])-i+1):
                if j > len(substr) and is_substr(data[0][i:i+j], data):
                    substr = data[0][i:i+j]
    return substr


def is_substr(find, data):
    if len(data) < 1 and len(find) < 1:
        return False
    for i in range(len(data)):
        if find not in data[i]:
            return False
    return True


ans = 0
for i in range(1, N):
    ls = long_substr([S[:i], S[i:]])
    # print(ls)
    if len(ls) > ans:
        ans = len(ls)


print(ans)


### FIXED CODE ###

n = int(input())
s = input()
ans = 0
for d in range(1, n):
    count = 0
    for i in range(n-d):
        if s[i] == s[i+d]:
            count += 1
        else:
            count = 0
        ans = max(min(count, d), ans)
print(ans)
### BUGGY CODE ###

n = int(input())
a = [int(input()) for _ in range(n)]
cnt = 0
for i in set(a):
    if a.count(i) % 2 == 1:
        cnt += 1
print(cnt)

### FIXED CODE ###

import collections
n = int(input())
a = [int(input()) for _ in range(n)]
A = collections.Counter(a)
ans = 0
for i in A.values():
    if i % 2 != 0:
        ans += 1
print(ans)
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import itertools
import numpy


def main():
  h, w, _ = map(int, input().split())
  values = numpy.array([[int(v == '#') for v in input().strip()] for _ in range(h)])
  flags = numpy.zeros_like(values)
  areas = []

  for y, x in itertools.product(range(h), range(w)):
    if values[y][x] == 1:
      flags[y][x] = 1
      areas.append([x, y, x, y])

  for area in areas:
    sx, sy, ex, ey = area

    while True:
      if sx > 0 and numpy.sum(flags[sy:ey + 1, sx - 1]) == 0:
        flags[sy:ey + 1, sx - 1] += 1
        sx -= 1
      elif sy > 0 and numpy.sum(flags[sy - 1, sx:ex + 1]) == 0:
        flags[sy - 1, sx:ex + 1] += 1
        sy -= 1
      elif ex + 1 < w and numpy.sum(flags[sy:ey + 1, ex + 1]) == 0:
        flags[sy:ey + 1, ex + 1] += 1
        ex += 1
      elif ey + 1 < h and numpy.sum(flags[ey + 1, sx:ex + 1]) == 0:
        flags[ey + 1, sx:ex + 1] += 1
        ey += 1
      else:
        break

    area[0], area[1], area[2], area[3] = sx, sy, ex, ey

  rets = numpy.zeros_like(values)

  for i, (sx, sy, ex, ey) in enumerate(areas, start=1):
    for x, y in itertools.product(range(sx, ex + 1), range(sy, ey + 1)):
      rets[y, x] = i

  for r in rets:
    print(' '.join(map(str, r)))


if __name__ == '__main__':
  main()


### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools


def main():
  h, w, _ = map(int, input().split())
  values = [[int(v == '#') for v in input().strip()] for _ in range(h)]
  flags = [[0] * w for _ in range(h)]
  areas = []

  for y, x in itertools.product(range(h), range(w)):
    if values[y][x] == 1:
      flags[y][x] = 1
      areas.append([x, y, x + 1, y + 1])

  for area in areas:
    sx, sy, ex, ey = area

    while True:
      if sx > 0 and sum(flags[y][sx - 1] for y in range(sy, ey)) == 0:
        for y in range(sy, ey):
          flags[y][sx - 1] += 1
        sx -= 1
      elif sy > 0 and sum(flags[sy - 1][x] for x in range(sx, ex)) == 0:
        for x in range(sx, ex):
          flags[sy - 1][x] += 1
        sy -= 1
      elif ex < w and sum(flags[y][ex] for y in range(sy, ey)) == 0:
        for y in range(sy, ey):
          flags[y][ex] += 1
        ex += 1
      elif ey < h and sum(flags[ey][x] for x in range(sx, ex)) == 0:
        for x in range(sx, ex):
          flags[ey][x] += 1
        ey += 1
      else:
        break

    area[0], area[1], area[2], area[3] = sx, sy, ex, ey

  rets = [[0] * w for _ in range(h)]

  for i, (sx, sy, ex, ey) in enumerate(areas, start=1):
    for x, y in itertools.product(range(sx, ex), range(sy, ey)):
      rets[y][x] = i

  for r in rets:
    print(' '.join(map(str, r)))


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

n = int(input())

line = list(map(int, input().strip().split(" ")))

for i in range(1, n + 1):
    print(line.count(i))


### FIXED CODE ###

n = int(input())

line = list(map(int, input().strip().split(" ")))

ans = [0 for s in range(n)]

for i in line:
    ans[i - 1] += 1

for i in range(n):
    print(ans[i])
### BUGGY CODE ###

from sys import stdin
from queue import deque


def push(d, v, n, q, dist):
    if v < 0 or n <= v:
        return
    if dist[v] is not None:
        return
    dist[v] = d
    q.append(v)


def main():
    n, x, y = [int(x) for x in stdin.readline().rstrip().split()]
    x -= 1
    y -= 1
    ans = [0] * n
    for i in range(n):
        dist = [None] * n
        q = deque()

        push(0, i, n, q, dist)
        while len(q):
            v = q.popleft()
            d = dist[v]
            push(d + 1, v - 1, n, q, dist)
            push(d + 1, v + 1, n, q, dist)
            if v == x:
                push(d + 1, y, n, q, dist)
            if v == y:
                push(d + 1, x, n, q, dist)

        for d in dist:
            ans[d] += 1

    ans = [x // 2 for x in ans]
    print(*ans[1:], sep='\n')


if __name__ == "__main__":
    main()


### FIXED CODE ###

from sys import stdin


def main():
    N, X, Y = [int(x) for x in stdin.readline().rstrip().split()]
    result = [0] * (N - 1)
    for left in range(1, N):
        for right in range(left + 1, N + 1):
            if (left <= X) and (Y <= right):
                x = (X - left) + 1 + (right - Y)
            elif (left <= X) and (X <= right <= Y):
                x = (X - left) + min((right - X), (Y - right) + 1)
            elif (X <= left <= Y) and (Y <= right):
                x = min((Y - left), (left - X) + 1) + (right - Y)
            elif (X <= left <= Y) and (X <= right <= Y):
                x = min((right - left), (left - X) + (Y - right) + 1)
            else:
                x = right - left
            result[x - 1] += 1
    print(*result, sep='\n')


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

N=int(input())
A=[input() for i in range(N)]
for i in range(N):
    D=A[0:i]+A[i+1:]
    print(max(D))
    

### FIXED CODE ###

N=int(input())
A=[int(input()) for i in range(N)]
D=sorted(A)
num=max(D)
num2=D[-2]
for i in range(N):
    if A[i]==num:
        print(num2)
    else:
        print(num)
### BUGGY CODE ###

def opp(i):  #対戦希望相手
    return a[i][nex[i]]

from sys import stdin
#入力
readline=stdin.readline
n=int(readline())
m=n*(n-1)//2
a=[list(map(lambda x:int(x)-1,readline().split())) for _ in range(n)]

nex=[0]*n
before=set(list(range(n)))
day=0
while len(before)>0:
    day+=1
    now=set()
    flags=[False]*n
    while len(before)>0:
        p=before.pop()
        q=opp(p)
        if opp(q)==p and flags[p]==False and flags[q]==False:
            flags[p]=True
            flags[q]=True
            nex[p]+=1
            nex[q]+=1
            if nex[p]<n-1:
                now.add(p)
            if nex[q]<n-1:
                now.add(q)
            if q in before:
                before.remove(q)
    before=now

if min(nex)==n-1:
    print(day)
else:
    print(-1)

### FIXED CODE ###

def opp(i):  #対戦希望相手
    return a[i][nex[i]]

from sys import stdin
#入力
readline=stdin.readline
n=int(readline())
m=n*(n-1)//2
a=[list(map(lambda x:int(x)-1,readline().split())) for _ in range(n)]

nex=[0]*n
before=set(range(n))
day=0
for day in range(1,m+1):
    now=set()
    while len(before)>0:
        p=before.pop()
        q=opp(p)
        if opp(q)==p and p not in now and q not in now:
            nex[p]+=1
            nex[q]+=1
            if nex[p]<n-1:
                now.add(p)
            if nex[q]<n-1:
                now.add(q)
            if q in before:
                before.remove(q)
    before=now
    if len(before)==0:
        break

if min(nex)==n-1:
    print(day)
else:
    print(-1)
### BUGGY CODE ###


n=int(input())

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

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

### FIXED CODE ###

import numpy as np
n=int(input())
ans=1
a=input().split()
#print(a)
if '0' in a:
    ans=0
    print(ans)
if ans!=0:
    for i in a:
        ans=ans*int(i)
        if ans>10**18:
            ans=-1
            break
    print(ans)
### BUGGY CODE ###

import math

n,m=map(int,input().split())
mod=10**9+7
a=math.factorial(n)*math.factorial(m)
if abs(n-m)==0:
    print(2*a%mod)
    exit()
elif abs(n-m)>1:
    a=0
print(a)


### FIXED CODE ###

import math

n,m=map(int,input().split())
a=math.factorial(n) * math.factorial(m) 
if abs(n-m) == 0:
    a *= 2
elif abs(n-m) > 1:
    a = 0
print(a%1000000007)
### BUGGY CODE ###

import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    def dfs(cur):
        if not edge[cur]:
            return 0
        else:
            m = [0] * len(edge[cur])
            for i, nx in enumerate(edge[cur]):
                m[i] = dfs(nx)
            m.sort(reverse=True)
            for i in range(len(m)):
                m[i] += (i + 1)
            return max(m)

    from collections import defaultdict, deque
    n = int(readline())
    edge = defaultdict(list)

    for i in range(2, n + 1):
        a = int(readline())
        edge[a].append(i)

    ans = dfs(1)
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    def dfs(cur):
        if not edge[cur]:
            return 0
        else:
            for i, nx in enumerate(edge[cur]):
                dp[nx] = dfs(nx)
            m = []
            for nx in edge[cur]:
                m.append(dp[nx])
            m.sort(reverse=True)
            for i, x in enumerate(m):
                m[i] = x + i + 1
            return max(m)

    from collections import defaultdict
    n = int(readline())
    edge = defaultdict(list)
    dp = [-1] * (n + 1)

    for i in range(2, n + 1):
        a = int(readline())
        edge[a].append(i)

    ans = dfs(1)
    print(ans)


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

N, M = map(int, input().split())  # N段の階段, 壊れている床の個数M

# A = []

# flag = True
# for i in range(M):
#     A.append(int(input()))
#     if len(A) > 1:
#         if A[i] == A[i-1]+1:
#             print(0)
#             flag = False
#             break

# if flag:

A = [int(input()) for _ in range(M)]

dp = [0, 1]

for i in range(1, N+1):
    if i in A:
        dp.append(0)
    else:
        dp.append((dp[-2] + dp[-1]))

print(dp[-1] % 1000000007)


### FIXED CODE ###

N, M = map(int, input().split())  # N段の階段, 壊れている床の個数M

A = set([int(input()) for _ in range(M)])

dp = [0, 1]

for i in range(1, N+1):
    if i in A:
        dp.append(0)
    else:
        dp.append((dp[-2] + dp[-1]) % 1000000007)

print(dp[-1] % 1000000007)
### BUGGY CODE ###


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop
import math
from collections import *
from functools import reduce,cmp_to_key,lru_cache
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
import sys
# input = sys.stdin.readline



M = mod = 10**9 + 7 
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
 
def li():return [int(i) for i in input().rstrip().split()]
def st():return str(input().rstrip())[2:-1]
def val():return int(input().rstrip())
def li2():return [str(i)[2:-1] for i in input().rstrip().split()]
def li3():return [int(i) for i in st()]
 



sys.setrecursionlimit(10 ** 5 + 10)

g = 0
d = {}
def dp(i, par = -1, black = 0):
    global g
    ans = 1
    if (i, par, black) in d:return d[(i, par, black)]
    nokids = 1
    if black:
        for j in g[i]:
            if j != par:
                nokids = 0
                ans = ans * dp(j, i, 0) % mod
    else:
        for j in g[i]:
            if j != par:
                nokids = 0
                ans = ( ans * (dp(j, i, 0) + dp(j, i, 1)) ) % mod

    if nokids:ans = 1
    d[(i, par, black)] = ans
    return ans



n = val()
g = defaultdict(set)

l = []
for _ in range(n - 1):
    a, b = li()
    l.append([a, b])
for i in range(n - 1):
    a, b = l[i]
    g[a].add(b)
    g[b].add(a)


root = 1
ans = dp(root, -1, 0) + dp(root, -1, 1)

print(ans % mod)

### FIXED CODE ###

from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline

from itertools import accumulate
from functools import lru_cache

M = mod = 10 ** 9 + 7
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
 
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
 



sys.setrecursionlimit(10 ** 6)

g = 0
@lru_cache(None)
def dp(i, par = -1, black = 0):
    global g
    ans = 1
    nokids = 1
    if black:
        for j in g[i]:
            if j != par:
                nokids = 0
                ans = ans * dp(j, i, 0) % mod
    else:
        for j in g[i]:
            if j != par:
                nokids = 0
                ans = ( ans * (dp(j, i, 0) + dp(j, i, 1)) ) % mod

    if nokids:ans = 1
    return ans



n = val()
g = [[] for i in range(n + 1)]

l = []
for _ in range(n - 1):
    a, b = li()
    g[a].append(b)
    g[b].append(a)


root = 1    
ans = dp(root, -1, 0) + dp(root, -1, 1)

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

n=int(input())
lis=[]
vot=[]
for i in range(n):
  s=input()
  if not s in lis:
    lis.append(s)
    vot.append(1)
  else:
    vot[lis.index(s)]+=1
inum = [n for n, v in enumerate(vot) if v==max(vot)]

ans=[]
for i in inum:
  ans.append(lis[i])
ans.sort()
for i in ans:
  print(i)

### FIXED CODE ###

n=int(input())
you=[]
al=[]
vot=[]
ans=[]
for i in range(n):
  s=input()
  al.append(s)
al.sort()
bef="zzzzzzzzzzz"
co=0
mx=1
for x in al:
  if bef!=x:
    if mx<co:
      mx=co
      ans=[bef]
    elif mx==co:
      ans.append(bef)
    co=1
    bef=x
  else:
    co+=1
if mx<co:
  mx=co
  ans=[bef]
elif mx==co:
  ans.append(bef)
for x in ans:
  print(x)
### BUGGY CODE ###

numberOfInput=int(input())
primeNumbers=0

while numberOfInput>0:
    researchNumber=int(input())
    if researchNumber==1:
        pass
    else:
        for devidedBy in range(1,researchNumber+1):
            if researchNumber%devidedBy==0 and devidedBy!=1 and devidedBy!=researchNumber:
                break
            elif researchNumber%devidedBy==0 and devidedBy==researchNumber:
                primeNumbers+=1
    numberOfInput-=1

print(primeNumbers)


### FIXED CODE ###

n =  int(input())
index=0
count=0
while index<n:
    number = int(input())
    stock =[]
    devideN = 1
    while devideN <= number:
        if len(stock)>2:
            break
        elif devideN==10000:
            stock.append(devideN)
            break
        elif number%devideN==0:
            if devideN!=1 and devideN!=number:
                break
            else:
                stock.append(devideN)
        else:
            pass
        devideN+=1
    if len(stock) == 2:
        count+=1
    index+=1
print(count)
### BUGGY CODE ###

def main():
    N,K = map(int, input().split())
    p = list(map(int, input().split()))
    eval = [ (1+pi)*0.5  for pi in p ]
    sums = [ sum(eval[i:i+K]) for i in range(N-K+1) ]
    print(max(sums))
    return

if __name__=="__main__":
    main()

### FIXED CODE ###

def main():
    N,K = map(int, input().split())
    p = list(map(int, input().split()))
    if N>K:
        esum = [0]*N
        psum = [0]*(N-K)
        esum[0] = p[1]
        for i in range(1,K):
            esum[i] = esum[i-1]+p[i]
        for i in range(K,N):
            esum[i] = esum[i-1]+p[i]
            psum[i-K] = esum[i]-esum[i-K]
        ans = (max(psum)+K)*0.5
    else:
        ans = (sum(p)+K)*0.5

    print(ans)
    return

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

a,b,c,k=map(int,input().split())
sums=0
while(k!=0):
  if(a!=0):
    a-=1
    k-=1
    sums+=1
  elif(b!=0):
    b-=1
    k-=1
  else:
    c-=1
    k-=1
    sums-=1
print(sums)

### FIXED CODE ###

a,b,c,k=map(int,input().split())
if(a>k):
  print(k)
elif(a+b>k):
  print(a)
else:
  print(a+(a+b-k))
### BUGGY CODE ###

H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(input())

L = [[0 for i in range(W)] for j in range(H)]
R = [[0 for i in range(W)] for j in range(H)]
U = [[0 for i in range(W)] for j in range(H)]
D = [[0 for i in range(W)] for j in range(H)]

ans=0

for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            if j==0:
                L[i][j]=1
            else:
                L[i][j]=L[i][j-1]+1
        if S[i][-j-1]=='.':
            if j==0:
                R[i][-j-1]=1
            else:
                R[i][-j-1]=R[i][-j]+1
        if S[i][j]=='.':
            if i==0:
                U[i][j]=1
            else:
                U[i][j]=U[i-1][j]+1
        if S[-i-1][j]=='.':
            if i==0:
                D[-i-1][j]=1
            else:
                D[-i-1][j]=D[-i][j]+1
for i in range(H):
    for j in range(W):
        ans = max(ans,L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)
print(ans)


### FIXED CODE ###

H,W=map(int,input().split())
S=[]
for i in range(H):
    S.append(input())

L = [[0 for i in range(W)] for j in range(H)]
R = [[0 for i in range(W)] for j in range(H)]
U = [[0 for i in range(W)] for j in range(H)]
D = [[0 for i in range(W)] for j in range(H)]

ans=0

for i in range(H):
    for j in range(W):
        if S[i][j]=='.':
            if j==0:
                L[i][j]=1
            else:
                L[i][j]=L[i][j-1]+1
        if S[i][-j-1]=='.':
            if j==0:
                R[i][-j-1]=1
            else:
                R[i][-j-1]=R[i][-j]+1
        if S[i][j]=='.':
            if i==0:
                U[i][j]=1
            else:
                U[i][j]=U[i-1][j]+1
        if S[-i-1][j]=='.':
            if i==0:
                D[-i-1][j]=1
            else:
                D[-i-1][j]=D[-i][j]+1
for i in range(H):
    for j in range(W):
        ans = max(ans,L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)
print(ans)
### BUGGY CODE ###

n, ma, mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(n)]

INF = 40 * 100 + 1
ab_max = 10 * n

dp = [[[INF] * (ab_max + 1) for _ in range(ab_max + 1)] for _ in range(n + 1)]
dp[0][0][0] = 0

for i, (a, b, c) in enumerate(abc, 1):
    for j in range(ab_max + 1):
        for k in range(ab_max + 1):
            dp[i][j][k] = dp[i-1][j][k]
            if j - a >= 0 and k - b >= 0:
                if dp[i-1][j-a][k-b] != INF:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b] + c)


ans = INF
for j in range(1, ab_max + 1):
    for k in range(1, ab_max + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

print(ans)


### FIXED CODE ###

n, ma, mb = map(int, input().split())
abc = [list(map(int, input().split())) for _ in range(n)]

INF = 40 * 100 + 1
ab_max = 10 * n

dp = [[[INF] * (ab_max + 1) for _ in range(ab_max + 1)] for _ in range(n + 1)]
dp[0][0][0] = 0

for i, (a, b, c) in enumerate(abc, 1):
    for j in range(ab_max + 1):
        for k in range(ab_max + 1):
            dp[i][j][k] = dp[i-1][j][k]
            if j - a >= 0 and k - b >= 0:
                if dp[i-1][j-a][k-b] != INF:
                    dp[i][j][k] = min(dp[i][j][k], dp[i-1][j-a][k-b] + c)


ans = INF
for j in range(1, ab_max + 1):
    for k in range(1, ab_max + 1):
        if j * mb == k * ma:
            ans = min(ans, dp[n][j][k])

if ans == INF:
    ans = -1

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

n,m = map(int,input().split())

in_p = []
in_y = []

p = []
for i in range(n):
    p.append([])

for i in range(m):
    a,b = map(int,input().split())
    in_p.append(a)
    in_y.append(b)
    p[a-1].append(b)  

for i in range(n):
    p[i].sort()


for i in range(m):
    year=str(in_p[i]).zfill(6)
    a = p[in_p[i]-1].index(in_y[i]) + 1
    num=str(a).zfill(6)
    print("{0}{1}".format(year,num))



### FIXED CODE ###

import copy

n,m = map(int,input().split())

cnt = [0]*(10**5+4)

yy = []
yp = []
for _ in range(m):
    a,b = map(int,input().split())
    a,b = b,a
    yy.append([a,0])
    yp.append([a,b])

inlist = copy.deepcopy(yp)
yp.sort()
yy = dict(yy)

for i in range(m):
    cnt[yp[i][1]]+=1
    yy[yp[i][0]] = cnt[yp[i][1]]

for key,value in inlist:
    print("{:06}{:06}".format(value,yy[key]))
### BUGGY CODE ###

from collections import deque
H, W = map(int,input().split())

A = [[False for _ in range(W)] for _ in range(H)]

Q = deque()
for y in range(H):
    S = input()
    for x, s in enumerate(S):
        if s == "#":
            Q.append((y, x))
            A[y][x] = True

ans = 0
while True:
    ans += 1
    NQ = deque()
    while Q:
        y, x = Q.popleft()
        if x > 0 and A[y][x-1] == False:
            A[y][x-1] = True
            NQ.append((y,x-1))
        if x < W-1 and A[y][x+1] == False:
            A[y][x+1] = True
            NQ.append((y,x+1))
        if y > 0 and A[y-1][x] == False:
            A[y-1][x] = True
            NQ.append((y-1, x))
        if y < H-1 and A[y+1][x] == False:
            A[y+1][x] = True
            NQ.append((y+1,x))
    if NQ:
        Q = NQ
    else:
        break

print(ans-1)

### FIXED CODE ###

def main():
    H, W = map(int,input().split())

    A = [[False for _ in range(W)] for _ in range(H)]

    Q = []
    for y in range(H):
        S = input()
        for x, s in enumerate(S):
            if s == "#":
                Q.append((y, x))
                A[y][x] = True

    ans = 0
    while Q:
        ans += 1
        NQ = []
        for y, x in Q:
            if x > 0 and A[y][x-1] == False:
                A[y][x-1] = True
                NQ.append((y,x-1))
            if x < W-1 and A[y][x+1] == False:
                A[y][x+1] = True
                NQ.append((y,x+1))
            if y > 0 and A[y-1][x] == False:
                A[y-1][x] = True
                NQ.append((y-1, x))
            if y < H-1 and A[y+1][x] == False:
                A[y+1][x] = True
                NQ.append((y+1,x))
        if NQ:
            Q = NQ
        else:
            break

    print(ans-1)

if __name__ == '__main__':
    main()