import Control.Monad
f s bx by=map(\x->if fst x=='.' then minimum[abs(x2-fst(snd x))+abs(y2-fst(smd x))|x2<-bx,y2<-by] else 1000)s
main=do
 [h,w]<-map read.words<$>getLine
 sr<-replicateM h getLine
 let s=zip (concat sr) [(x,y)|x<-[1..w],y<-[1..h]]
     b=filter(\x->if fst x=='#' then True else False)s
     bx=map fst$map snd$b
     by=map snd$map snd$b
 print$minimum$f s bx by