{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}

import Control.Monad
import Data.Maybe (mapMaybe, isNothing)
import Data.List (foldl', partition)
import Data.Array.Unboxed ((!), UArray)
import qualified Data.Array.Unboxed as U

main = do
    [h, w] <- map (read :: String -> Int) . words <$> getLine
    -- a <- concat . words <$> getContents
    a <- concat <$> replicateM h getLine
    let ls = U.listArray ((1, 1), (w, h)) a :: UArray (Int, Int) Char
    let (black, white) = partition (\(i, e) -> e == '#') $ U.assocs ls
    let (!blacks, !whites) = (map fst black, map fst white)
    let answer = foldl' (hoge h w blacks) 0 whites
    print answer
    where getRange (ex, ey) (x, y) = abs (ex - x) + abs (ey - y)
          minRange h w blacks !ec = foldl' (hoho ec) (h + w) blacks 
          hoge h w blacks a b = max a (minRange h w blacks b)
          hoho ec a b = min a (getRange ec b)
