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 C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            input_iter = LazyLoadAllLines();
            //------------------------------------------------//
            //var n = Read<long>();
            //var s = Read();
            //var als = Read<long>(n);
            //var sls = Read(n);
            //List<List<long>> abls = new List<List<long>>();
            //List<long> cls = new List<long>();
            /*List<long> edgeNum = Enumerable.Repeat(0L, (int)n).ToList();
            for (int i = 0; i < n - 1; ++i)
            {
                abls.Add(Read<long>(2));
                abls[i].Add(i);
                abls[i].Add(-1);
                --abls[i][0];
                --abls[i][1];
                //cls.Add(Read<long>());
                ++edgeNum[(int)abls[i][0]];
                ++edgeNum[(int)abls[i][1]];
            }*/

            //als.Sort((l, r) => (r - l));
            //sls.Sort(StringComparer.OrdinalIgnoreCase);

            var h = Read<long>();
            var w = Read<long>();
            var s = Read(h);

            Stack<Data> dats = new Stack<Data>();
            dats.Push(new Data(0, 0, 0, null, false));
            int cnt = int.MaxValue;

            while (dats.Count > 0)
            {
                var dat = dats.Pop();

                if (dat.row == h - 1 && dat.column == w - 1)
                {
                    if (s[dat.row][dat.column] == '#' && dat.isVecRight == null) ++dat.num;
                    cnt = Math.Min(cnt, dat.num);
                    continue;
                }

                if (s[dat.row][dat.column] == '#')
                {
                    if(dat.isVecRight == null)
                    {
                        if (dat.column < w - 1) dats.Push(new Data(dat.row, dat.column + 1, dat.num + 1, true, false));
                        if (dat.row < h - 1) dats.Push(new Data(dat.row + 1, dat.column, dat.num + 1, false, false));
                    }
                    else
                    {
                        if (dat.isVecRight.Value)
                        {
                            if (dat.column < w - 1 && s[dat.row][dat.column] == '#')
                            {
                                dats.Push(new Data(dat.row, dat.column + 1, dat.num, true, false));
                            }
                            else
                            {
                                if (!dat.isTurned && dat.row < h - 1) dats.Push(new Data(dat.row + 1, dat.column, dat.num, false, true));
                                else
                                {
                                    if(dat.row < h - 1)dats.Push(new Data(dat.row + 1, dat.column, dat.num, null, true));
                                    if(dat.column < w - 1)dats.Push(new Data(dat.row, dat.column + 1, dat.num, null, true));
                                }
                            }
                        }
                        else
                        {
                            if (dat.row < h - 1 && s[dat.row][dat.column] == '#')
                            {
                                dats.Push(new Data(dat.row + 1, dat.column, dat.num, false, false));
                            }
                            else
                            {
                                if (!dat.isTurned && dat.column < w - 1) dats.Push(new Data(dat.row, dat.column + 1, dat.num, true, true));
                                else
                                {
                                    if (dat.row < h - 1) dats.Push(new Data(dat.row + 1, dat.column, dat.num, null, true));
                                    if (dat.column < w - 1) dats.Push(new Data(dat.row, dat.column + 1, dat.num, null, true));
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (dat.row < h - 1) dats.Push(new Data(dat.row + 1, dat.column, dat.num, null, false));
                    if (dat.column < w - 1) dats.Push(new Data(dat.row, dat.column + 1, dat.num, null, false));
                }
            }

            Console.WriteLine(cnt);


        }

        struct Data
        {
            public int row;
            public int column;
            public int num;
            public bool? isVecRight;
            public bool isTurned;

            public Data(int r, int c, int n, bool? isv, bool ist) {
                row = r; column = c; num = n; isVecRight = isv; isTurned = ist;
            }
        }

        static IEnumerator<string> input_iter;

        static IEnumerator<string> LazyLoadAllLines(bool doSplit = true)
        {
            string line;
            if (doSplit)
            {
                do
                {
                    line = Console.ReadLine();
                    foreach (var s in line.Split(' '))
                        yield return s;
                } while (line != null);
            }
            else
            {
                do
                {
                    line = Console.ReadLine();
                    yield return line;
                } while (line != null);
            }
        }
        // single 1 elem.
        static _T Read<_T>() where _T : struct
        {
            input_iter.MoveNext();
            return (_T)Convert.ChangeType(input_iter.Current, typeof(_T));
        }
        static string Read()
        {
            input_iter.MoveNext();
            return input_iter.Current;
        }
        // single n-elems.
        static List<_T> Read<_T>(long num) where _T : struct
        {
            List<_T> res = new List<_T>();
            for (long i = 0; i < num; ++i)
            {
                input_iter.MoveNext();
                res.Add((_T)Convert.ChangeType(input_iter.Current, typeof(_T)));
            }
            return res;
        }
        static List<string> Read(long num)
        {
            List<string> res = new List<string>();
            for (long i = 0; i < num; ++i)
            {
                input_iter.MoveNext();
                res.Add(input_iter.Current);
            }
            return res;
        }
        // multiple(single type) n-elems. (Range, Edge etc.)
        static List<List<_T>> Read<_T>(long elemNum, long typeNum, bool toSort = false) where _T : struct
        {
            List<List<_T>> res = new List<List<_T>>();
            for (int i = 0; i < elemNum; ++i)
            {
                var elem = Read<_T>(typeNum);
                if (toSort) elem.Sort();
                res.Add(elem);
            }
            return res;
        }
        // multiple(multiple type) n-elems.
        static List<Tuple<_T1, _T2>> Read<_T1, _T2>(long num) where _T1 : struct where _T2 : struct
        {
            List<Tuple<_T1, _T2>> res = new List<Tuple<_T1, _T2>>();
            for (int i = 0; i < num; ++i)
            {
                res.Add(Tuple.Create(Read<_T1>(), Read<_T2>()));
            }
            return res;
        }
    }
}