### BUGGY CODE ###

## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
#from math import gcd 旧環境でこれを有効化するとREを吐きます
 
s = input().split()
p = [int(w) for w in s] #これで勝手にNまで拾ってくれます
H = p[0]
W = p[1]
 
a = []
for i in range(H):
    a.append(input())
 
 
#白ベースで最寄りの黒を探す方針
#パディングは'.'でも'#'でもないもので埋める
b = ''
for i in range(W+2):
    b += 'w'
#print(b)
#上と下と右と左に空きマスを挿入する
a.insert(0, b)
for i in range(1, 1+H):
    a[i] = 'w' + a[i] + 'w'
a.append(b)
 
 
header = ' '
for i in range(len(a[0])):
    header += ('{}'.format(i))
 
c=[]
for i in range(H+2):
    c.append([])
 
for i in range(H+2):
    for j in range(W+2):
        c[i].append(a[i][j])
 
 
def see_banmen():
    print(header)
    for i in range(len(c)):
        print('{}{}'.format(i,''.join(c[i])))
#see_banmen()
 
 
 
#queue = [start]
next_list = [] #次の歩数で探すものを格納しておく
step = 1
#mode = 0
#answer_mode = 0
status = True #ゴール判定でゴールについていたらFalseにする
#answer = -1
 
queue = []
for i in range(H+2):
    for j in range(W+2):
        if(c[i][j] == '#'):
            queue.append([i, j])
 
while status:
    if(len(queue) > 0):
        pivot = queue.pop(0) #軸足はqueueの先頭
        x = pivot[0]
        y = pivot[1]
        if(c[x+1][y] == '.'):
            c[x+1][y] = str(step)
            next_list.append([x+1, y])
        if(c[x-1][y] == '.'):
            c[x-1][y] = str(step)
            next_list.append([x-1, y])
        if(c[x][y+1] == '.'):
            c[x][y+1] = str(step)
            next_list.append([x, y+1])
        if(c[x][y-1] == '.'):
            c[x][y-1] = str(step)
            next_list.append([x, y-1])
    else:
        #queueの中身が空っぽになった
        #基本的にはstep += 1
        if(len(next_list) > 0):
            #next_listの中身がある場合（まだ探索が終了していない場合）
            #next_list -> queue にして next_listはからにする
            for i in range(len(next_list)):
                queue.append(next_list.pop())
            next_list = []
            #print()
            #print('step{}end'.format(step))
            #see_banmen()
            #次の探索に移る
            step += 1
        else:
            #初めてnext_listの中身がなくなった場合（探索は終了したい）
            #もう進めない
            #print()
            #print('もう進めない')
            #print('step{}end'.format(step))
            #see_banmen()
            status = False
 
print(step - 1)

### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.readline
    h,w = map(int,input().split())
    a = []
    for i in range(h):
        inp = list(input())
        a.append(inp)
    
    dp = [[10**9]*w for i in range(h)]
    
    for i in range(h):
        for j in range(w):
            if a[i][j] == '.' and [i,j] != [0,0]:
                if i >= 1:
                    dp[i][j] = dp[i-1][j]+1
                    if j >= 1:
                        dp[i][j] = min(dp[i][j],dp[i][j-1]+1)
                else:
                    dp[i][j] = dp[i][j-1]+1
            elif a[i][j] == '#':
                dp[i][j] = 0
    for i in range(h):
        for j in range(w-1,-1,-1):
            if a[i][j] == '.' and [i,j] != [0,w-1]:
                if i >= 1:
                    dp[i][j] = min(dp[i-1][j]+1,dp[i][j])
                    if j+1 <= w-1:
                        dp[i][j] = min(dp[i][j],dp[i][j+1]+1)
                else:
                    dp[i][j] = min(dp[i][j+1]+1,dp[i][j])
    for i in range(h-1,-1,-1):
        for j in range(w):
            if a[i][j] == '.' and [i,j] != [h-1,0]:
                if i+1 <= h-1:
                    dp[i][j] = min(dp[i+1][j]+1,dp[i][j])
                    if j >= 1:
                        dp[i][j] = min(dp[i][j],dp[i][j-1]+1)
                else:
                    dp[i][j] = min(dp[i][j-1]+1,dp[i][j])
    for i in range(h-1,-1,-1):
        for j in range(w-1,-1,-1):
            if a[i][j] == '.' and [i,j] != [h-1,w-1]:
                if i+1 <= h-1:
                    dp[i][j] = min(dp[i+1][j]+1,dp[i][j])
                    if j+1 <= w-1:
                        dp[i][j] = min(dp[i][j],dp[i][j+1]+1)
                else:
                    dp[i][j] = min(dp[i][j+1]+1 ,dp[i][j])
        
    res = -1
    for i in range(h):
        for j in range(w):
            res = max(res,dp[i][j])

    print(res)



if __name__ == '__main__':
    main()