import copy,collections,itertools
H,W=[int(_) for _ in input().split()]
S=[list(input()) for _ in range(H)]
for i,j in itertools.product(range(H),range(W)):
  if S[i][j]=='.':
   break
q=collections.deque([(i,j,0)])
while q:
 x,y,d=q.popleft()
 S[x][y]=','
 for dx,dy in [[-1,0],[1,0],[0,-1],[0,1]]:
  if 0<=x+dx<H and 0<=y+dy<W and S[x+dx][y+dy]=='.':
   X,Y=x+dx,y+dy
   q+=[(X,Y,d+1)]
q=collections.deque([(X,Y,0)])
while q:
 x,y,d=q.popleft()
 S[x][y]='!'
 for dx,dy in [[-1,0],[1,0],[0,-1],[0,1]]:
  if 0<=x+dx<H and 0<=y+dy<W and S[x+dx][y+dy]==',':
   q+=[(x+dx,y+dy,d+1)]
print(d)
