import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import qualified Data.Vector.Unboxed as V
import qualified Data.IntSet as S
import Data.Bool (bool)
import Data.List (partition)

main :: IO ()
main = do
  (h : w : _) <- map unsafeSignedDecimal . T.words <$> T.getLine :: IO [Int]
  ass <- map (V.fromList . T.foldr ((:) . (== '#')) []) . T.lines <$> T.getContents :: IO [V.Vector Bool]
  let
    bwjss = map (((,) <$> map fst . fst <*> map fst . snd) . partition snd) . zipWith ($) (map (map . ((,) <$> id <*>) . (V.!)) ass) $ (replicate h [0..(pred w)]) :: [([Int], [Int])]
    n = search 0 bwjss :: Int
    search :: Int -> [([Int], [Int])] -> Int
    search k bwjss = if all null (map snd bwjss) then k else search (succ k) bwjss'
      where
        bwjss' = map fW . uncurry zip . ((,) <$> candidates . fst <*> snd) . unzip $ bwjss
  print n

candidates :: [[Int]] -> [[Int]]
candidates = snd . foldr step (undefined, [])
  where
    step :: [Int] -> ([Int], [[Int]]) -> ([Int], [[Int]])
    step olds (news, news' : newss) = (olds, merge (candidatesW olds) news : merge olds news' : newss)
    step olds _ = (olds, candidatesW olds : [])
    merge :: [Int] -> [Int] -> [Int]
    merge xs@(x : xs') ys@(y : ys') = if y < x then y : merge xs ys' else x : merge xs' ys
    merge xs [] = xs
    merge [] ys = ys
    candidatesW :: [Int] -> [Int]
    candidatesW = dropWhile (< 0) . foldr step []
      where
        step :: Int -> [Int] -> [Int]
        step = (.) <$> (:) . pred <*> ins . succ
        ins :: Int -> [Int] -> [Int]
        ins x xs@(y : ys) = case compare x y of
          LT -> x : xs
          EQ -> xs
          GT -> y : ins x ys
        ins x _ = [x]

fW :: ([Int], [Int]) -> ([Int], [Int])
fW (bjs@(bj : bjs'), wjs@(wj : wjs')) = case compare bj wj of
  LT -> fW (bjs', wjs)
  EQ -> ((,) <$> (bj :) . fst <*> snd) (fW (bjs', wjs'))
  GT -> ((,) <$> fst <*> (wj :) . snd) (fW (bjs, wjs'))
fW (_, []) = ([], [])
fW bwjs = bwjs

unsafeSignedDecimal :: T.Text -> Int
unsafeSignedDecimal s = case T.signed T.decimal s of
  Right (n, _) -> n
