import Control.Monad
f s b w=map(\x->if fst x=='.' then minimum[let q=abs(snd x-z) in (q`div`w)+(q`mod`w)|z<-b] else 0)s
main=do
 [h,w]<-map read.words<$>getLine
 sr<-replicateM h getLine
 let s=zip (concat sr) [1..h*w]
     b=map snd$filter(\x->if fst x=='#' then True else False)s
 print$maximum$f s b w