-- 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

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

subf :: Char -> Char -> Char
subf c '#' = '#'
subf '#' c = '#'
subf '.' '.' = '.'

pilerow :: [Char] -> [Char] -> [Char]
pilerow [] [] = []
pilerow (x:xs) (y:ys) = [(subf x y)] ++ (pilerow xs ys)

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

dotrow :: Int -> [Char]
dotrow 0 = []
dotrow n = '.' : (dotrow (n-1))

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

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

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

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

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

checkrow :: [Char] -> Bool
checkrow [] = False
checkrow (x:xs) =
    case x of
        '.' -> True
        '#' -> False || (checkrow xs)

checkbrd :: [[Char]] -> Bool
checkbrd [] = False
checkbrd (x:xs) = 
    case (checkrow x) of
        True -> True
        False -> False || (checkbrd xs)

agc033a :: [[Char]] -> Int -> Int -> Int
agc033a brd h w = if checkbrd brd 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
  brd <- replicateM h getLine
  print (agc033a brd h w)