Problem:
Score : 400 points

Problem Statement

Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
Each square is painted black or white.

The grid is said to be good if and only if the following condition is satisfied:

From (1, 1), we can reach (H, W) by moving one square right or down repeatedly, while always being on a white square.

Note that (1, 1) and (H, W) must be white if the grid is good.

Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.

Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) - that is, from white to black and vice versa.

Constraints

2 \leq H, W \leq 100

Input

Input is given from Standard Input in the following format:

H W
s_{11} s_{12} \cdots s_{1W}
s_{21} s_{22} \cdots s_{2W}
\vdots
s_{H1} s_{H2} \cdots s_{HW}

Here s_{rc} represents the color of (r, c) - # stands for black, and . stands for white.

Output

Print the minimum number of operations needed.

Sample Input 1

3 3
.##
.#.
##.

Sample Output 1

1

Do the operation with (r_0, c_0, r_1, c_1) = (2, 2, 2, 2) to change just the color of (2, 2), and we are done.

Sample Input 2

2 2
#.
.#

Sample Output 2

2

Sample Input 3

4 4
..##
#...
###.
###.

Sample Output 3

0

No operation may be needed.

Sample Input 4

5 5
.#.#.
#.#.#
.#.#.
#.#.#
.#.#.

Sample Output 4

4

Solution in Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

module Main where

import           Control.Monad
import           Debug.Trace
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import           Data.List
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.Maybe
import           Data.Ord
--import           Data.Vector.Algorithms.Search
import qualified Data.Array                    as A
import qualified Data.Array.Unboxed            as AU
import qualified Data.Array.IO                 as AIO



getVUIL :: IO (VU.Vector Int)
getVUIL = VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
readI :: BSC8.ByteString -> Int
readI = fst . fromJust . BSC8.readInt
readIL :: BSC8.ByteString -> [Int]
readIL = map readI . BSC8.words
getI :: IO Int
getI = readI <$> BSC8.getLine
getIL :: IO [Int]
getIL = readIL <$> BSC8.getLine

getNIS :: Int -> IO [(Int, BSC8.ByteString)]
getNIS 0 = return []
getNIS n = do
    [aFst, aSnd] <- BSC8.words <$> BSC8.getLine
    next         <- (getNIS (n - 1))
    return ((readI aFst, aSnd) : next)

il2ss :: [Int] -> String
il2ss = unwords . map show

il2sn :: [Int] -> String
il2sn = unlines . map show

modNum = 10 ^ 9 + 7
newtype Mint = M Int
instance Num Mint where
    (M a) + (M b) = M $ mod (a + b) modNum
    (M a) - (M b) = M (a - b)
    (M a) * (M b) = M $ mod (a * b) modNum
    fromInteger a = M $ mod (fromInteger a) modNum
    abs (M a) = M $ abs a
    signum (M a) = M $ signum a
instance Show Mint where
    show (M a) = show (mod a modNum)

mFromInt :: Int -> Mint
mFromInt a = M (mod a modNum)

main :: IO ()
main = do
    [aH, aW] <- getIL
    aS       <- getString aH
    let
        (sIntList, _) = foldr
            (\x acc1@(acc1List, acc1N@(acc1NL, acc1NR)) ->
                let retAcc@(retAccList, retAccN@(retAccNL, retAccNR)) = foldr
                        (\y acc2@(acc2List, acc2N@(acc2NL, acc2NR)) ->
                            if y == '#'
                                then
                                    ( (acc2N, 1) : acc2List
                                    , (acc2NL, acc2NR - 1)
                                    )
                                else
                                    ( (acc2N, 0) : acc2List
                                    , (acc2NL, acc2NR - 1)
                                    )
                        )
                        (acc1List, (acc1NL, acc1NR))
                        x
                in  (retAccList, (retAccNL - 1, aW))
            )
            ([], (aH, aW))
            aS
    let sArray =
            AU.array ((1, 1), (aH, aW)) sIntList :: AU.Array (Int, Int) Int

    let combi = makeCombi (aH - 1) (aW - 1)
    let
        scanList = map
            (\x ->
                let (cnt, _, (_, _)) = foldl
                        (\acc@(num, flg, (pY, pX)) y -> if y == 1
                            then
                                let point     = (pY, pX + 1)
                                    nextBlock = sArray AU.! point
                                in  if nextBlock == 1
                                        then if flg == 1
                                            then (num, flg, point)
                                            else (num + 1, 1, point)
                                        else if flg == 1
                                            then (num, 0, point)
                                            else (num, flg, point)
                            else
                                let point     = (pY + 1, pX)
                                    nextBlock = sArray AU.! point
                                in  if nextBlock == 1
                                        then if flg == 1
                                            then (num, flg, point)
                                            else (num + 1, 1, point)
                                        else if flg == 1
                                            then (num, 0, point)
                                            else (num, flg, point)
                        )
                        (sArray AU.! (1, 1), sArray AU.! (1, 1), (1, 1))
                        x
                in  cnt
            )
            combi

    print $ minimum scanList

getString :: Int -> IO [String]
getString 0 = return []
getString n = do
    line <- getLine
    next <- getString (n - 1)
    return ([line] ++ next)

makeCombi :: Int -> Int -> [[Int]]
makeCombi h 0 = [replicate h 1]
makeCombi 0 w = [replicate w 0]
makeCombi h w = makeCombi' h w [[1], [0]] 1
makeCombi' :: Int -> Int -> [[Int]] -> Int -> [[Int]]
makeCombi' h w back n
    | h + w == n
    = back
    | otherwise
    = let
          (unPro, pro) = foldl
              (\acc@(accUnPro, accPro) x ->
                  let sumx = sum x
                      lenx = length x
                  in  if sumx == h
                          then
                              ( accUnPro
                              , accPro
                                  ++ [ foldl (\acc y -> 0 : acc)
                                             x
                                             [1 .. w - (lenx - sumx)]
                                     ]
                              )
                          else if (sumx - lenx) == w
                              then
                                  ( accUnPro
                                  , accPro
                                      ++ [ foldl (\acc y -> 1 : acc)
                                                 x
                                                 [1 .. h - sumx]
                                         ]
                                  )
                              else (accUnPro ++ [x], accPro)
              )
              ([], [])
              [ front : nBack | front <- [1, 0], nBack <- back ]
      in  if (h + w - 1) == n
              then pro
              else (makeCombi' h w unPro (n + 1)) ++ pro