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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;


public class Main {
    public static void main(String[] args) {
        Out out = new Out();
        solve(out);
        out.outln();
        out.flush();
        out.close();
    }

    public static void solve(Out out) {
        int h = In.ni();
        int w = In.ni();
        boolean[][] g = In.inputGrid(h, w, '#');
        ArrayDeque<Integer> dq = new ArrayDeque<>();
        dq.add(0);
        int[][] d = new int[h][w];
        for (int i = 0; i < h; i++) {
            Arrays.fill(d[i], Const.IINF);
        }
        
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (i == 0 && j == 0) {
                    d[i][j] = g[i][j] ? 1 : 0;
                } else if (i == 0) {
                    if (g[i][j] ^ g[i][j - 1]) {
                        d[i][j] = d[i][j - 1] + 1;
                    } else {
                        d[i][j] = d[i][j - 1];
                    }
                } else if (j == 0) {
                    if (g[i][j] ^ g[i - 1][j]) {
                        d[i][j] = d[i - 1][j] + 1;
                    } else {
                        d[i][j] = d[i - 1][j];
                    }
                } else {
                    int v1 = g[i][j] ^ g[i][j - 1] ? d[i][j - 1] + 1 : d[i][j - 1];
                    int v2 = g[i][j] ^ g[i - 1][j] ? d[i - 1][j] + 1 : d[i - 1][j];
                    d[i][j] = Math.min(v1, v2);
                }
            }
        }

        out.append((d[h - 1][w - 1] + 1) / 2);
    }
}


class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte()) {
            return buffer[ptr++];
        } else {
            return -1;
        }
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
            ptr++;
        }
        return hasNextByte();
    }

    public String next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
            throw new NumberFormatException();
        }
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


class Out {
    private StringBuilder sb = new StringBuilder();
    private PrintWriter w = new PrintWriter(System.out);

    public Out() {
    }

    public void append(int v) {
        sb.append(v);
    }

    public void append(int[] a, String sep) {
        for (int i = 0; i < a.length; i++) {
            append(a[i]);
            if (i < a.length - 1) {
                append(sep);
            }
        }
    }

    public void append(int[] a, char sep) {
        append(a, String.valueOf(sep));
    }

    public void append(int[] a) {
        append(a, "\n");
    }

    public void append(long v) {
        sb.append(v);
    }

    public void append(long[] a, String sep) {
        for (int i = 0; i < a.length; i++) {
            append(a[i]);
            if (i < a.length - 1) {
                append(sep);
            }
        }
    }

    public void append(long[] a, char sep) {
        append(a, String.valueOf(sep));
    }

    public void append(long[] a) {
        append(a, "\n");
    }

    public void append(double v) {
        sb.append(v);
    }

    public void append(double[] a, String sep) {
        for (int i = 0; i < a.length; i++) {
            append(a[i]);
            if (i < a.length - 1) {
                append(sep);
            }
        }
    }

    public void append(double[] a, char sep) {
        append(a, String.valueOf(sep));
    }

    public void append(double[] a) {
        append(a, "\n");
    }

    public void append(char c) {
        sb.append(c);
    }

    public void append(char[] c) {
        sb.append(c);
    }

    public void append(char[][] c) {
        for (char[] e : c) {
            append(e);
            append('\n');
        }
    }

    public void append(String[] a, String sep) {
        for (int i = 0; i < a.length; i++) {
            append(a[i]);
            if (i < a.length - 1) {
                append(sep);
            }
        }
    }

    public void append(String[] a, char sep) {
        append(a, String.valueOf(sep));
    }

    public void append(String[] a) {
        append(a, "\n");
    }

    public void append(Object e) {
        sb.append(e);
    }

    public void append(Object[] a, String sep) {
        for (int i = 0; i < a.length; i++) {
            append(a[i]);
            if (i < a.length - 1) {
                append(sep);
            }
        }
    }

    public void append(Object[] a, char sep) {
        append(a, String.valueOf(sep));
    }

    public void append(Object[] a) {
        append(a, "\n");
    }

    public void out() {
        w.print(sb);
        clearBuilder();
    }

    public void outln() {
        w.println(sb);
        clearBuilder();
    }

    // public void print(String s) {
    //     w.print(s);
    // }

    // public void println() {
    //     w.println();
    // }

    // public void println(int v) {
    //     w.println(v);
    // }

    // public void println(long v) {
    //     w.println(v);
    // }

    // public void println(double v) {
    //     w.println(v);
    // }

    // public void println(char c) {
    //     w.println(c);
    // }

    // public void println(String s) {
    //     w.println(s);
    // }

    // public void println(Object e) {
    //     w.println(e);
    // }

    // public void println(String sep, int... a) {
    //     StringBuilder s = new StringBuilder();
    //     for (int i = 0; i < a.length; i++) {
    //         s.append(a[i]);
    //         if (i < a.length - 1) {
    //             s.append(sep);
    //         }
    //     }
    //     println(s.toString());
    // }

    // public void println(char sep, int... a) {
    //     println(String.valueOf(sep), a);
    // }

    // public void println(String sep, long... a) {
    //     StringBuilder s = new StringBuilder();
    //     for (int i = 0; i < a.length; i++) {
    //         s.append(a[i]);
    //         if (i < a.length - 1) {
    //             s.append(sep);
    //         }
    //     }
    //     println(s.toString());
    // }

    // public void println(char sep, long... a) {
    //     println(String.valueOf(sep), a);
    // }

    // public void println(String sep, double... a) {
    //     StringBuilder s = new StringBuilder();
    //     for (int i = 0; i < a.length; i++) {
    //         s.append(a[i]);
    //         if (i < a.length - 1) {
    //             s.append(sep);
    //         }
    //     }
    //     println(s.toString());
    // }

    // public void println(char sep, double... a) {
    //     println(String.valueOf(sep), a);
    // }

    // public void println(String sep, String... a) {
    //     StringBuilder s = new StringBuilder();
    //     for (int i = 0; i < a.length; i++) {
    //         s.append(a[i]);
    //         if (i < a.length - 1) {
    //             s.append(sep);
    //         }
    //     }
    //     println(s.toString());
    // }

    // public void println(char sep, String... a) {
    //     println(String.valueOf(sep), a);
    // }

    // public void println(String sep, Object... a) {
    //     StringBuilder s = new StringBuilder();
    //     for (int i = 0; i < a.length; i++) {
    //         s.append(a[i]);
    //         if (i < a.length - 1) {
    //             s.append(sep);
    //         }
    //     }
    //     println(s.toString());
    // }

    // public void println(char sep, Object... a) {
    //     println(String.valueOf(sep), a);
    // }

    public void flush() {
        w.flush();
    }

    public void close() {
        w.close();
    }

    public void clearBuilder() {
        sb = new StringBuilder();
    }

    public void reverse() {
        sb.reverse();
    }

    public void delete(int start, int end) {
        sb.delete(start, end);
    }

    public void delete(int length) {
        sb.delete(sb.length() - length, sb.length());
    }

    public int length() {
        return sb.length();
    }
}

class Const {
    public static final long MOD7 = 1_000_000_007;
    public static final long MOD9 = 1_000_000_009;
    public static final long MOD99 = 998_244_353;

    public static final long LINF = 1_000_000_000_000_000_000l;
    public static final int IINF = 1_000_000_000;
    public static final double DINF = 1e150;

    public static final double DELTA = 0.000_000_000_1;
    public static final double LDELTA = 0.000_001;

    public static final String YES = "YES";
    public static final String NO = "NO";
    public static final String Yes = "Yes";
    public static final String No = "No";
    public static final String POSSIBLE = "POSSIBLE";
    public static final String IMPOSSIBLE = "IMPOSSIBLE";
    public static final String Possible = "Possible";
    public static final String Impossible = "Impossible";

    public static final int[] dx8 = {1, 0, -1, 0, 1, -1, -1, 1};
    public static final int[] dy8 = {0, 1, 0, -1, 1, 1, -1, -1};
    public static final int[] dx = {1, 0, -1, 0};
    public static final int[] dy = {0, 1, 0, -1};

    public static long MOD = MOD7;

    public static void setMod(long mod) {
        MOD = mod;
    }

    private Const(){}
}


class In {
    public static final FastScanner fsc = new FastScanner();

    public static int ni() {
        return fsc.nextInt();
    }

    public static int[] ni(int n) {
        int[] a = new int[n];
        Arrays.setAll(a, i -> fsc.nextInt());
        return a;
    }

    public static int[][] ni(int n, int m) {
        int[][] a = new int[n][m];
        Arrays.setAll(a, i -> ni(m));
        return a;
    }

    public static long nl() {
        return fsc.nextLong();
    }

    public static long[] nl(int n) {
        long[] a = new long[n];
        Arrays.setAll(a, i -> fsc.nextLong());
        return a;
    }

    public static long[][] nl(int n, int m) {
        long[][] a = new long[n][m];
        Arrays.setAll(a, i -> nl(m));
        return a;
    }

    public static double nd() {
        return fsc.nextDouble();
    }

    public static double[] nd(int n) {
        double[] a = new double[n];
        Arrays.setAll(a, i -> nd());
        return a;
    }

    public static double[][] nd(int n, int m) {
        double[][] a = new double[n][m];
        Arrays.setAll(a, i -> nd(m));
        return a;
    }

    public static String ns() {
        return fsc.next();
    }

    public static String[] ns(int n) {
        String[] s = new String[n];
        Arrays.setAll(s, i -> ns());
        return s;
    }

    public static char[] nc() {
        return fsc.next().toCharArray();
    }

    public static char[][] nc(int n, int m) {
        char[][] c = new char[n][m];
        Arrays.setAll(c, i -> nc());
        return c;
    }

    public static boolean[][] inputGrid(int h, int w, char truechar) {
        boolean[][] grid = new boolean[h][w];
        for (int i = 0; i < h; i++) {
            char[] s = fsc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                grid[i][j] = s[j] == truechar;
            }
        }
        return grid;
    }
}