-- AGC033 A problem
import Control.Monad

-- conversion from String to Int
toInt :: String -> Int
toInt x = read x :: Int

-- obtain [Int] from I/O
getInts :: IO [Int]
getInts = do
  x <- getLine
  let y = (map $ toInt) $ words x
  return y

-- mapping
sw :: Char -> Int
sw '.' = 1
sw '#' = 0

rowmap :: [Char] -> [Int]
rowmap [] = []
rowmap (x:xs) = [(sw x)] ++ (rowmap xs)

brdmap :: [[Char]] -> [[Int]]
brdmap [] = []
brdmap (x:xs) = [(rowmap x)] ++ (brdmap xs)

transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose xs = map head xs : transpose (map tail xs)

pilerow :: [Int] -> [Int] -> [Int]
pilerow [] [] = []
pilerow (x:xs) (y:ys) = [x*y] ++ (pilerow xs ys)

pilebrd :: [[Int]] -> [[Int]] -> [[Int]]
pilebrd [] [] = []
pilebrd (x:xs) (y:ys) = [(pilerow x y)] ++ (pilebrd xs ys)

dotrow :: Int -> [Int]
dotrow 0 = []
dotrow n = [1] ++ (dotrow (n-1))

shiftU :: [[Int]] -> Int -> [[Int]]
shiftU x n = (tail x) ++ [(dotrow n)]

shiftD :: [[Int]] -> Int -> [[Int]]
shiftD x n  = [(dotrow n)] ++ (init x)

shiftL :: [[Int]] -> Int -> [[Int]]
shiftL x n = transpose $ shiftU (transpose x) n

shiftR :: [[Int]] -> Int -> [[Int]]
shiftR x n = transpose $ shiftD (transpose x) n

turnbrd :: [[Int]] -> Int -> Int -> [[Int]]
turnbrd brd h w =
    pilebrd brd (pilebrd (shiftU brd w) (pilebrd (shiftD brd w) (pilebrd (shiftL brd h) (shiftR brd h))))

checkrow :: [Int] -> Int
checkrow [] = 0
checkrow (x:xs) = x + (checkrow xs)

checkbrd :: [[Int]] -> Int
checkbrd [] = 0
checkbrd (x:xs) = (checkrow x) + (checkbrd xs)

agc033a :: [[Int]] -> Int -> Int -> Int
agc033a brd h w =
    if checkbrd brd > 0 then 1 + (agc033a (turnbrd brd h w) h w) else 0
    
-- main function
main :: IO ()
main = do
  hnw <- getInts
  let h = head hnw :: Int
  let w = last hnw :: Int
  rawbrd <- replicateM h getLine
  let brd = brdmap rawbrd
  print (agc033a brd h w)