import Control.Monad
f s w n
 |all(=='#')s = n
 |otherwise=let sp=concat(map(\x->if fst x=='#' then [snd x+w,snd x-w,snd x+1,snd x-1] else [])s) in f (map(\x->if snd x`elem`sp then ('#',snd x) else x)s) n+1
main=do
 [h,w]<-map read.words<$>getLine
 sr<-replicateM h (map read.words<$>getLine)
 let s=zip (concat sr) [1..]
 print$f s w 0