import Control.Monad
import Data.List

main = do
  [h,w] <- map read . words <$> getLine
  as <- zip [1..] <$> replicateM h (fmap (zip [1..]) getLine)
  print $ paints (getPos as) (h,w) 0

getPos :: [(Int,[(Int,Char)])] -> [(Int,Int)]
getPos [] = []
getPos ((_,[]):zs) = getPos zs
getPos ((x,(y,'#'):ys):zs) = (x,y) : getPos ((x,ys):zs)
getPos ((x,_:ys):zs) = getPos ((x,ys):zs)
getPos (_:zs)              = getPos zs

paint (x,y) = [(x,y),(x+1,y),(x-1,y),(x,y+1),(x,y-1)]

paints as (h,w) acc = if alls \\ as == [] then acc else paints as' (h,w) (acc+1)
  where 
    as' = filter p . concat $ map paint as
    p (x,y) = 1 <= x && x <= h && 1 <= y && y <= w
    alls = [(x,y) | x <- [1..h], y <- [1..w]]