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

n, x = map(int, input().split())
a = [int(i) for i in input().split()]
l = len(a)

ans = 0

for i in range(l-1):
    while a[i] + a[i+1] > x:
        a[i+1] -= 1
        ans += 1

print(ans)


### FIXED CODE ###

n, x = map(int, input().split())
a = [int(i) for i in input().split()]
l = len(a)

ans = 0

for i in range(l-1):
    a_0 = a[i]
    a_1 = a[i+1]
    dif = x - a_0
    if a_1 > dif:
        ans += a_1 - dif
        if dif > 0:
            a[i+1] = dif
        else:
            a[i+1] = 0

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

#ダイクストラ法
#O(ElogV)
import heapq
def dijkstra_heap(s,edge):
  #始点sから各頂点への最短距離
  d = [float("inf")] * (n+1)
  used = [False] * (n+1)
  d[s] = 0
  used[s] = True
  edgelist = []
  #edgelist : [sからの暫定(未確定)最短距離,頂点]のリスト
  #edge[s] : sから出る枝の[重み,終点]の配列
  for e in edge[s]:
    heapq.heappush(edgelist,e) #sの隣の点は枝の重さがそのまま暫定最短距離となる
  while len(edgelist):
    #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
    minedge = heapq.heappop(edgelist)
    #minedge : [sからの(確定)最短距離,頂点]
    v = minedge[1]
    if used[v]:
      continue
    d[v] = minedge[0]
    used[v] = True
    for e in edge[v]:
      if not used[e[1]]:
        heapq.heappush(edgelist,[d[v]+e[0],e[1]])
  return d

################################
n,m,s,t = map(int,input().split()) #n:頂点数　w:辺の数

edge_yen = [[] for _ in range(n+1)]
edge_snuke = [[] for _ in range(n+1)]

for i in range(m):
  u,v,a,b = map(int, input().split())
  edge_yen[u].append([a,v])
  edge_yen[v].append([a,u])
  edge_snuke[u].append([b,v])
  edge_snuke[v].append([b,u])

d_yen = dijkstra_heap(s,edge_yen)
d_snuke = dijkstra_heap(t,edge_snuke)
ans = [0]*(n)
m = float("inf")
for i in range(n,0,-1):
  m = min(d_yen[i]+d_snuke[i],m)
  ans[i-1]=10**15-m
print(*ans,sep='\n')

### FIXED CODE ###

#ダイクストラ法
#O(ElogV)
import heapq
def dijkstra_heap(s,edge):
  #始点sから各頂点への最短距離
  d = [float("inf")] * (n+1)
  used = [False] * (n+1)
  d[s] = 0
  used[s] = True
  edgelist = []
  #edgelist : (sからの暫定(未確定)最短距離,頂点)のリスト
  #edge[s] : sから出る枝の(重み,終点)の配列
  for w,v in edge[s]:
    heapq.heappush(edgelist,w*10**6+v) #sの隣の点は枝の重さがそのまま暫定最短距離となる
  while len(edgelist):
    #まだ使われてない頂点の中から最小の距離のものを探す→確定させる
    minedge = heapq.heappop(edgelist)
    #minedge : (sからの(確定)最短距離,頂点)
    v = minedge%10**6
    if used[v]:
      continue
    d[v] = minedge//10**6
    used[v] = True
    for w,u in edge[v]:
      if not used[u]:
        heapq.heappush(edgelist,(d[v]+w)*10**6+u)
  return d

################################
n,m,s,t = map(int,input().split()) #n:頂点数　w:辺の数

edge_yen = [[] for _ in range(n+1)]
edge_snuke = [[] for _ in range(n+1)]

for i in range(m):
  u,v,a,b = map(int, input().split())
  edge_yen[u].append((a,v))
  edge_yen[v].append((a,u))
  edge_snuke[u].append((b,v))
  edge_snuke[v].append((b,u))

d_yen = dijkstra_heap(s,edge_yen)
d_snuke = dijkstra_heap(t,edge_snuke)
ans = [0]*(n)
m = float("inf")
for i in range(n,0,-1):
  m = min(d_yen[i]+d_snuke[i],m)
  ans[i-1]=10**15-m
print(*ans,sep='\n')
### BUGGY CODE ###

N=int(input())

mod=10**9+7

FACT=[1]
for i in range(1,10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0

ANS=0
MINUS=0

def k_perm(k):
    return Combi(k-1,N-k-1)*FACT[k]*FACT[N-k-1]%mod

for k in range(N):
    ANS=(ANS+(k_perm(k)-k_perm(k-1))*k)%mod

print(ANS)

### FIXED CODE ###

N=int(input())

mod=10**9+7

FACT=[1]
for i in range(1,10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0

ANS=0
MINUS=0

def k_perm(k):
    return Combi(k-1,N-k-1)*FACT[k]*FACT[N-k-1]%mod

for k in range(N):
    ANS=(ANS+(k_perm(k)-k_perm(k-1))*k)%mod

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

import sys

def longestcommonsubsequence():
    seq1 = raw_input()
    seq2 = raw_input()

    a,b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i,e1 in enumerate(seq1):
        e1 = seq1[i]
        x2 = x1
        x1 = [0 for i in range(b+1)]
        for j, e2 in enumerate(seq2):
            if e1 == e2:
                x1[j+1]=x2[j] + 1
            else:
                x1[j+1]=max(x2[j+1], x1[j])
    return x1[-1]

n = input()
for i2 in range(n):

    print longestcommonsubsequence()

### FIXED CODE ###

import sys

def longestcommonsubsequence():
    seq1 = raw_input()
    seq2 = raw_input()

    a, b = len(seq1), len(seq2)
    x1 = [0 for i in range(b+1)]
    for i in range(a):
        e1 = seq1[i]
        x2 = x1+[]
        #x1 = [0 for i2 in range(b+1)]
        for j in range(b):
            if e1 == seq2[j]:
                x1[j+1] = x2[j] + 1
            elif x1[j+1]<x1[j]:
                x1[j+1] = x1[j]
    return x1[-1]

n = input()
for i2 in range(n):

    print longestcommonsubsequence()
### BUGGY CODE ###

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

A.sort(reverse=True)
cnt = []
for _ in range(N):
    cnt.append(A[1])
    del A[0:2]
    del A[-1]
    
print(sum(cnt))

### FIXED CODE ###

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

A.sort(reverse=True)
i = 1
n = 1
cnt = 0
while 2*i <= 2*N:
    cnt += A[n]
    i += 1
    n += 2
    
print(cnt)
### BUGGY CODE ###

#!/usr/bin/env python3

# from typing import Optional
from typing import List

# from typing import Dict
from typing import Tuple

# from typing import Sequence

Cod = Tuple[int, int]


def main():
    args = input().split()
    H = int(args[0])
    W = int(args[1])
    M = int(args[2])

    codList: List[Cod] = []
    for i in range(M):
        args = input().split()
        codList.append((int(args[0]), int(args[1])))

    columnSumList: List[int] = [0] * W
    rowSumList: List[int] = [0] * H

    maxColumnSum = 0
    maxColumIndexList: List[int] = []
    maxRowSum = 0
    maxRowIndexList: List[int] = []
    for item in codList:
        columnSum = columnSumList[item[1] - 1] + 1
        if columnSum > maxColumnSum:
            maxColumnSum = columnSum
            maxColumIndexList = [item[1]]
        elif columnSum == maxColumnSum:
            maxColumIndexList.append(item[1])

        rowSum = rowSumList[item[0] - 1] + 1
        if rowSum > maxRowSum:
            maxRowSum = rowSum
            maxRowIndexList = [item[0]]
        elif rowSum == maxRowSum:
            maxRowIndexList.append(item[0])

        columnSumList[item[1] - 1] = columnSum
        rowSumList[item[0] - 1] += 1

    result = maxColumnSum + maxRowSum
    for row in maxRowIndexList:
        for column in maxColumIndexList:
            if (row, column) in codList:
                continue
            else:
                print(result)
                return

    print(result - 1)


if __name__ == "__main__":
    main()


### FIXED CODE ###

#!/usr/bin/env python3

# from typing import Optional
from typing import List

# from typing import Dict
from typing import Tuple
from typing import Set

# from typing import Sequence

Cod = Tuple[int, int]


def main():
    args = input().split()
    H = int(args[0])
    W = int(args[1])
    M = int(args[2])

    codList = {}
    columnSumList: List[int] = [0] * W
    rowSumList: List[int] = [0] * H
    for i in range(M):
        h, w = map(lambda x: int(x), input().split())
        item = (h, w)
        codList.setdefault(h, {})
        codList[h][w] = item
        columnSumList[w - 1] += 1
        rowSumList[h - 1] += 1

    maxColumnSum = max(columnSumList)
    maxColumIndexList = [i for i, x in enumerate(columnSumList) if x == maxColumnSum]
    maxRowSum = max(rowSumList)
    maxRowIndexList = [i for i, x in enumerate(rowSumList) if x == maxRowSum]

    result = maxColumnSum + maxRowSum
    for row in maxRowIndexList:
        for column in maxColumIndexList:
            if row + 1 in codList and column + 1 in codList[row + 1]:
                continue
            else:
                print(result)
                return

    print(result - 1)


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

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    S = readline().strip().decode('utf-8')
    Q = int(readline())
    Query = [tuple(readline().decode('utf-8').split()) for _ in range(Q)]
    for q in Query:
        if q[0] == '1':
            S = S[::-1]
        else:
            if q[1] == '1':
                S = q[2] + S
            else:
                S += q[2]
    print(S)

    return
if __name__ == '__main__':
    main()



### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    from collections import deque
    S = deque(readline().strip().decode())
    Q = int(readline())
    Query = readlines()
    rev_flg = False
    for q in Query:
        if q[0] == ord('1'):
            rev_flg = not rev_flg
        else:
            _, f, c = q.decode().split()
            if int(f)-(rev_flg) == 1:
                S.appendleft(c)
            else:
                S.append(c)
    S = ''.join(S)
    if rev_flg is True:
        S = S[::-1]
    print(S)

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

while 1:
    n,p = map(int,input().split())
    if n==0:break
    man =  [0]*n
    ball,a = p,0
    while 1:
        a%=n
        if ball >0:ball,man[a] =ball-1, man[a]+1
        elif ball == 0:ball,man[a] = man[a],ball
        a+=1
        if p in man:break
    print(man.index(max(man)))
              

### FIXED CODE ###

while 1:
    n,p = map(int,input().split())
    if n==0:break
    man =  [0]*n
    ball,a = p,0
    while 1:
        a%=n
        if ball >0:
            ball-=1
            man[a]+=1
        elif ball == 0:ball,man[a] = man[a],ball
        if man[a]==p:break
        a+=1
    print(a)
### BUGGY CODE ###

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

breakstep = []

for _ in range(m):
    breakstep.append(int(input()))

sl = [1]
def solve(i):
    global sl
    l = len(sl)
    if l < i+1:
        while l < i+1:
            if l in breakstep: 
                sl.append(0)
                l += 1
                continue
            elif l == 1:
                l += 1
                sl.append(1)
                continue
            sl.append((sl[l-1] + sl[l-2])%1000000007)
            l += 1
    return sl[i]

print(solve(n)%1000000007)

### FIXED CODE ###

import math
from collections import deque

memo = deque([1, 2])
floor = 1
def search(x):
    global memo, floor
    while x > floor:
        memo.append(memo[-1] + memo[-2])
        floor += 1
    return memo[x-1]

CONST = 1000000007
n,m = map(int,input().split())
lis = []
prev = -1
for _ in range(m):
    a = int(input())
    if prev + 1 == a:
        print(0)
        exit()
    lis.append(a-prev-2)
    prev = a
lis.append(n-prev-1)


ans = 1
for v in lis:
    if v == 0: continue
    ans = (ans * search(v)) % CONST
print(ans)
### BUGGY CODE ###

a, b = list(map(int, input().split()))
c = list(map(int, input().split()))
itta = []
mati = 1
i = 0
while True:
    if mati in itta:
        saisyo = mati
        break
    else:
        itta.append(mati)
        mati = c[mati-1]
        i += 1
risuto = []
while True:
    if mati in risuto:
        break
    else:
        risuto.append(mati)
        mati = c[mati-1]
if b <= i-1:
    print(itta[b])
else:
    print(risuto[(b-i) % len(risuto)])

### FIXED CODE ###

a, b = list(map(int, input().split()))
c = list(map(int, input().split()))
itta2 = set([])
mati = 1
i = 0
itta = []
while True:
    if mati in itta2:
        saisyo = mati
        break
    else:
        itta2.add(mati)
        itta.append(mati)
        mati = c[mati-1]
        i += 1
risuto2 = set([])
risuto = []

while True:
    if mati in risuto2:
        break
    else:
        risuto2.add(mati)
        risuto.append(mati)
        mati = c[mati-1]
if b <= i-1:
    print(itta[b])
else:
    print(risuto[(b-i) % len(risuto)])
### BUGGY CODE ###

n = int(input())
op = 1
p = list(map(int,input().split()))
for i in p:
    op = op*i

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

### FIXED CODE ###

n = int(input())
# for i in range(n):
#     m = int(input())
#     k = k*m
op = 1
p = list(map(int,input().split()))
p.sort()
for i in p:
    op = op*i

    if op > 10**18:
        print(-1)
        exit(0)

print(op)
### BUGGY CODE ###

import copy

def f1(l1):
    ret = []
    for i in l1:
      for n,j in enumerate(li[i]):
        if j == 1:
            if n in used or n in l1:
                return False
            elif n in tree[nest-1]:
            	pass
            else:
                ret.append(n)
    return list(set(ret))


N = int(input())
li = []
for _ in range(N):
    li.append(list(map(int,list(input()))))
    
max_nest = 0
for i in range(N):
    tree = [[]]
    used = []
    tree.append([i])
    nest = 1
    while True:
        ret = f1(tree[nest])
        nest += 1
        if ret == False:
            break
        elif ret != []:
            tree.append(ret)
            used.extend(tree[nest-2])
        else:
            if max_nest < nest:
                max_nest = nest
            break
        
print(max_nest-1)

### FIXED CODE ###

def f1(l1):
    ret = []
    for i in l1:
      for n,j in enumerate(li[i]):
        if j:
            if n in used or n in l1:
                return False
            elif n not in tree[nest-1] and n not in ret:
                ret.append(n)
    return ret


N = int(input())
li = []
for _ in range(N):
    li.append(list(map(int,list(input()))))
    
max_nest = 0
for i in range(N):
    tree = [[]]
    used = []
    tree.append([i])
    nest = 1
    while True:
        ret = f1(tree[nest])
        nest += 1
        if ret == False:
            break
        elif ret != []:
            tree.append(ret)
            used.extend(tree[nest-2])
        else:
            if max_nest < nest:
                max_nest = nest
            break
        
print(max_nest-1)
### BUGGY CODE ###

n = int(input())

ans = 0
ans1 = [0] * n
for i in range(1,100):
  for j in range(1,100):
    for k in range(1,100):
      ans = i**2 + j**2 +k**2 + i*j + j*k + k*i
      for l in range(n-1):
        if(l+1 == ans):
          ans1[l] += 1
for i in range(n):
  print(ans1[i])

### FIXED CODE ###

n = int(input())

ans = 0
ans1 = [0] * 10050
for i in range(1,105):
  for j in range(1,105):
    for k in range(1,105):
      ans = i**2 + j**2 +k**2 + i*j + j*k + k*i
      if(ans < 10050):
        ans1[ans] += 1
for i in range(n):
  print(ans1[i+1])
### BUGGY CODE ###

while True:
    n = int(raw_input())
    r = 0
    if n == 0:
        break
    s = [map(int, raw_input().replace('.','0').replace('*','1')) for i in range(n)]
    sum = [[0] * (n + 1) for i in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(1, n + 1):
            sum[i][j] += s[i - 1][j - 1]
            sum[i][j] += (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1])
    for size in range(n, 0, -1):
        for i in range(size, n + 1):
            for j in range(size, n + 1):
                s = sum[i][j] - sum[i - size][j] - sum[i][j - size] + sum[i - size][j - size]
                if s == 0:
                    r = max(r, size)
                    break
    print r

### FIXED CODE ###

while True:
    n = int(raw_input())
    if n == 0:
        break
    s = [raw_input() for i in range(n)]
    dp = [[0] * (n + 1) for i in range(n + 1)]
    for i in range(n):
        for j in range(n):
            if s[i][j] == '.':
                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
    print max(map(max, dp))
### BUGGY CODE ###

N = int(input())
A = input().split()
for j in range(N):
    for i in A:
        A.remove(i)
        if A.count(i) == 0:
            N -= 1
        else:
            print("NO")
            break
if N == 0:
    print("YES")


### FIXED CODE ###

N = int(input())
A = input().split()
if len(A) == len(set(A)):
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, m = map(int, readline().split())
a = list(map(int, read().split()))

dp = [0] * (n + 1)
dp[0] = 1
if 1 not in a:
    dp[1] = 1
cnt = 0
for i in range(2, n + 1):
    if i not in a:
        dp[i] = dp[i - 1] + dp[i - 2]
        cnt = 0
    else:
        cnt += 1
        if cnt == 2:
            print('0')
            exit()
print(dp[-1]%(10**9+7))

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
MOD = 10**9+7

n, m = map(int, readline().split())
a = set(map(int, read().split()))

dp = [0] * (n + 1)
dp[0] = 1
if 1 not in a:
    dp[1] = 1
cnt = 0
for i in range(2, n + 1):
    if i not in a:
        dp[i] = (dp[i - 1] + dp[i - 2]) % MOD
        cnt = 0
    else:
        cnt += 1
        if cnt == 2:
            print('0')
            exit()
print(dp[n])
### BUGGY CODE ###

from collections import deque

N = int(input())
A = deque(map(int, input().split()))
first = A.popleft()


def round(a):
        B = list(A)
        eval = 0
        pre = first
        post = a
        B.remove(a)
        while len(B) != 0:
                c = pre ^ post
                try:
                        B.remove(c)
                        pre = post
                        post = c
                except:
                        return 0
                        eval = 1
                        break
        
        if eval == 0 and post ^ first == a:
                return 1
        elif eval == 0:
                return 0

ans = 0

for i in range(0, N - 1):
        ans = round(A[i])
        if ans == 1:
                print('Yes')
                break
                

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




### FIXED CODE ###

from collections import deque

N = int(input())
A = deque(map(int, input().split()))
first = A.popleft()


def round(a):
        B = list(A)
        eval = 0
        pre = first
        post = a
        B.remove(a)
        num = N - 2
        while num != 0:
                c = pre ^ post
                try:
                        B.remove(c)
                        pre = post
                        post = c
                        num -= 1
                except:
                        return 0
                        eval = 1
                        break
        
        if eval == 0 and post ^ first == a:
                return 1
        elif eval == 0:
                return 0

val = first
for i in range(N - 1):
        val = val ^ A[i]

if val != 0:
        print('No')
else:
        ans = 0
        for i in range(0, N - 1):
                ans = round(A[i])
                if ans == 1:
                        print('Yes')
                        break
                        

        if ans == 0:
                print('No')
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
b = []
for i in range(n):
	b.append(A[i])
	b.reverse()
print(*b)

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
odd_b = A[::2]
even_b = A[1::2]
even_b.reverse()
ans = even_b+odd_b
if n%2 == 0:
	print(*ans)
else:
	ans.reverse()
	print(*ans)
### BUGGY CODE ###

N = int(input())
t = list(map(int,input().split()))
v = list(map(int,input().split()))
T = sum(t)*2
vt = [min(i/2,(T-i)/2) for i in range(T+1)]
ts = 0
te = 0
for i in range(N):
    te += t[i]*2
    vi = v[i]
    vt = [min(vtj,vi+max(0,-(j-ts)/2,(j-te)/2)) for j,vtj in enumerate(vt)]
    ts = te
print(sum(vt)/2)

### FIXED CODE ###

N = int(input())
t = list(map(int,input().split()))
v = list(map(int,input().split()))
T = sum(t)*2
vt = [min(i/2,(T-i)/2) for i in range(T+1)]
ts = 0
te = 0
for i in range(N):
    te += t[i]*2
    for j in range(T+1):
        vt[j] = min(vt[j],v[i]+max(0,-(j-ts)/2,(j-te)/2))
    ts = te
print(sum(vt)/2)
### BUGGY CODE ###

import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
C=[1]
for i in range(N):
  if C.count(A[C[i]-1])==0:
    C.append(A[C[i]-1])
  else:
    l=C.index(A[C[i]-1])+1
    break
if K+1<=l:
  print(C[K])
else:
  print(C[l-1+(K+1-l)%(len(C)-l+1)])

### FIXED CODE ###

import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
C=[1]
s=set([1])
for i in range(N):
  if A[C[i]-1] in s:
    l=C.index(A[C[i]-1])+1
    break
  else:
    C.append(A[C[i]-1])
    s.add(A[C[i]-1])
if K+1<=l:
  print(C[K])
else:
  print(C[l-1+(K+1-l)%(len(C)-l+1)])
### BUGGY CODE ###

n = int(input())
print(0,flush=True)

prev = input()
if prev=='Vacant': exit()

now=[]

def isok(ans, should_change):
    print(ans,flush=True)
    now = input()

    if (now!=prev and should_change)or(now==prev and not should_change):
        return True
    else:
        return False

ok = n
ng = 1

while ok - ng > 1:
    m = (ok+ng)//2
    should_change = (m-ng)%2!=0
    if isok(m, should_change):
        prev=now
        ok = m
    else:
        prev=now
        ng = m


### FIXED CODE ###

n = int(input())
print(0,flush=True)

prev = input()
if prev=='Vacant': exit()
zero = prev
now=[]

def isok(ans, odd_diff):
    print(ans,flush=True)
    now = input()

    if now == 'Vacant': exit()

    if (now==zero and odd_diff)or(now!=zero and not odd_diff):
        return True
    else:
        return False

ok = n-1 #ok以前にVacantが存在
ng = 0 #ng以前にVacantは存在しない

while ok - ng > 1:
    m = (ok+ng)//2
    odd_diff = (m%2==1)
    if isok(m, odd_diff):
        ok = m
    else:
        ng = m

print(ok,flush=True)
exit()
### BUGGY CODE ###

from collections import deque
import copy
import numpy as np

H, W = map(int, input().split())
blacks = deque()
maps = []
inf = 10 ** 7
for h in range(H):
  line = list(input())
  for w in range(W):
    if line[w] == '#':
      blacks.append([h,w])
  maps.append([0 if x == '#' else inf for x in line])
maps = np.array(maps)

def fill():
  new_blacks = deque()
  while len(blacks) > 0:
    x, y = blacks.popleft()
    for dx, dy in [[0,1],[0,-1],[1,0],[-1,0]]:
      nx = x+dx
      ny = y+dy
      if H > nx >= 0 and W > ny >= 0 and maps[nx][ny] != 0:
        maps[nx][ny] = 0
        new_blacks.append([nx,ny])
  return new_blacks

end = False
for i in range(H*W):
  blacks = fill()
  if len(blacks) == 0:
      print(i)
      exit()

### FIXED CODE ###

# python には結構厳しいTLE・・・python以外のお勉強時間ないよ～( ;∀;)
# . から見た # の最大距離の方が楽ではあるが・・・
# 嫌いだけど def main も使ってみた．
from collections import deque
import copy
 
def main():
  H, W = list(map(int, input().split()))
  L = [[j=="#" for j in input()] for i in range(H)]
  q = deque([(i, j) for i in range(H) for j in range(W) if L[i][j]])
  cands = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  result = 0
  while True:
    nq = deque()
    while q:
      i, j = q.popleft()
      if i > 0 and not L[i-1][j]:
        L[i-1][j] = True
        nq.append((i-1, j))
      if j > 0 and not L[i][j-1]:
        L[i][j-1] = True
        nq.append((i, j-1))
      if i < H-1 and not L[i+1][j]:
        L[i+1][j] = True
        nq.append((i+1, j))
      if j < W-1 and not L[i][j+1]:
        L[i][j+1] = True
        nq.append((i, j+1))
    if nq:
      q = copy.copy(nq)
    else:
      break    
    result += 1
  print(result)
 
if __name__ == "__main__":
  main()