import Control.Applicative
import Control.Monad
import Data.Array.IArray
import Data.Array.IO

main = do
  [h,w] <- map read.words <$> getLine
  field <- readField (h,w)
  num <- countPD field
  print num

readField :: (Int,Int) -> IO (Array (Int,Int) Char)
readField (h,w) = do
  cs <- concat <$> replicateM h getLine
  return $ listArray ((1,1),(h,w)) cs

countPD :: Array (Int,Int) Char -> IO Int
countPD arr = do
  let bs = bounds arr
      ixList = indices arr
  ioarr <- newArray bs False
  foldl (>>=) (return 0) $ map (countPDs arr ioarr) ixList

countPDs :: Array (Int, Int) Char -> IOArray (Int, Int) Bool -> (Int, Int) -> Int -> IO Int
countPDs arr ioarr ix num = do
  checkedIx <- readArray ioarr ix
  let numIx = if checkedIx then 0 else 1
  checkProperty arr ioarr ix checkedIx
  return $ num + numIx

printField :: IOArray (Int, Int) Bool -> IO ()
printField ioarr = do
  ((hmin,wmin),(hmax,wmax)) <- getBounds ioarr
  forM_ [hmin..hmax] $ \i -> do
    forM_ [wmin..wmax] $ \j -> do
      b <- readArray ioarr (i,j)
      if b then putStr "T" else putStr "F"
    putStrLn ""

checkProperty :: Array (Int, Int) Char -> IOArray (Int, Int) Bool -> (Int, Int) -> Bool -> IO ()
checkProperty arr ioarr ix checkedIx = do
  if checkedIx
     then return ()
     else do
       let (h,w) = ix
       writeArray ioarr ix True
       updateField arr ioarr ix

updateField :: Array (Int, Int) Char -> IOArray (Int, Int) Bool -> (Int, Int) -> IO ()
updateField arr ioarr (h,w) = do
  let ((hmin,wmin),(hmax,wmax)) = bounds arr
  putStrLn $ "run updateField " ++ show (h,w) ++ " " ++ show (hmax == h, wmax == w)
  case (hmax == h, wmax == w) of
       (True, True) -> return ()
       (True, False) -> rightUpdate arr ioarr (h,w)
       (False, True) -> underUpdate arr ioarr (h,w)
       (False, False) -> rightUpdate arr ioarr (h,w) >> underUpdate arr ioarr (h,w)

rightUpdate arr ioarr (h,w) = do
  putStrLn $ "run rightUpdate " ++ show (h,w)
  let originProp = arr ! (h,w)
      rightProp = arr ! (h,w+1)
  if originProp == rightProp
     then writeArray ioarr (h,w+1) True >> updateField arr ioarr (h,w+1)
     else return ()

underUpdate arr ioarr (h,w) = do
  putStrLn $ "run underUpdate " ++ show (h,w)
  let originProp = arr ! (h,w)
      underProp = arr ! (h+1,w)
  if originProp == underProp
     then writeArray ioarr (h+1,w) True >> updateField arr ioarr (h+1,w)
     else return ()

