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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.util.*

fun main(args: Array<String>) = AGC043A.main()

object AGC043A {
    fun readInt() = readLine()!!.toInt()
    fun readInts() = readLine()!!.split(" ").map { it.toInt() }

    val width: Int
    val height: Int
    val grid: Grid<Boolean> // is white
    val flipsGrid: Grid<Int>

    init {
        val (h, w) = readInts()
        val rows = (1..h).map { readLine()!!.map { (it == '.') } }
        width = w
        height = h
        grid = Grid(rows)
        CellNode.field = grid
        flipsGrid = grid.map { Int.MAX_VALUE }
    }

    fun main() {
        //println(grid)
        val good = grid.mapIndexed { x, y, it ->
            (x == width - 1 && y == height - 1 && it)
        }
        val start = grid.get(width - 1, height - 1)

        val root = if (start == true) CellNode(width - 1, height - 1)
        else CellNode(Dim2(width - 1, height - 1), flips = 0, friendColor = false)

        DepthFirstSearch<Dim2>().search(root)
            .filterIsInstance<CellNode>()
            .forEach {
                if (flipsGrid.get(it.x, it.y) > it.flips) flipsGrid.set(it.value.x, it.value.y, it.flips)
            }
        //println("flips:")
        //println(flipsGrid)

        val result = flipsGrid.get(0,0)
        println(result)
    }


    fun Grid<Boolean>.flip(x1: Int, y1: Int, x2: Int, y2: Int) {
        this.mapIndexed { x, y, it ->
            if (x in x1 - 1 until x2 && y in y1 - 1 until y2) this.set(x, y, !it)
        }
    }

    data class Dim2(val x: Int, val y: Int) {
        override fun toString(): String = "($x,$y)"
    }

    class CellNode(
        override val value: Dim2,
        val flips: Int = 0,  // 白黒をまたぐ回数
        val friendColor: Boolean = true // 通れる色
    ) :
        TreeNode<Dim2> {
        companion object {
            lateinit var field: Grid<Boolean>

        }

        constructor(x: Int, y: Int) : this(Dim2(x, y))

        val x: Int get() = value.x
        val y: Int get() = value.y
        fun exists(): Boolean {
            return (x in 0 until field.width) &&
                    (y in 0 until field.height) &&
                    field.get(x, y) == friendColor
        }

        private fun exists(x: Int, y: Int): Boolean {
            return (x in 0 until field.width) &&
                    (y in 0 until field.height) &&
                    field.get(x, y) == friendColor
        }

        private fun childTo(x: Int, y: Int): CellNode {
            return if (exists(x, y)) CellNode(Dim2(x, y), flips, friendColor)
            else CellNode(Dim2(x, y), flips + 1, !friendColor)
        }

        override val children: Sequence<TreeNode<Dim2>>
            get() = sequenceOf(
                childTo(x - 1, y),
                childTo(x, y - 1)
            ).filter {
                (it.x in 0 until Companion.field.width) && (it.y in 0 until Companion.field.height)
            }

        override fun toString(): String = "$value: $flips : $friendColor"
    }

    class Grid<T>(rows: List<List<T>>) {
        val width: Int
            get() = rows.first().size
        val height: Int
            get() = rows.size
        private val rows: List<MutableList<T>> = rows.map { it.toMutableList() }
        fun get(x: Int, y: Int): T = rows[y][x]
        fun set(x: Int, y: Int, value: T) {
            rows[y][x] = value
        }

        fun <R> map(transform: (T) -> R): Grid<R> =
            Grid(
                rows = rows.map { it.map(transform) }
            )

        fun <R> mapIndexed(transform: (x: Int, y: Int, T) -> R): Grid<R> =
            Grid(
                rows = rows.mapIndexed { y, it ->
                    it.mapIndexed { x, it ->
                        transform(x, y, it)
                    }
                }
            )

        fun flatten(): List<T> = rows.flatten()

        override fun toString(): String {
            return rows.joinToString("\n") { it.joinToString(" ") }
        }
    }

    interface TreeNode<T> {
        val value: T
        val children: Sequence<TreeNode<T>>
    }

    interface TreeSearch<T> {
        fun search(root: TreeNode<T>): Sequence<TreeNode<T>>
    }

    abstract class TreeSearchBase<T : Any> : TreeSearch<T> {
        private val visited = mutableSetOf<Any>()
        protected fun TreeNode<T>.isNotVisited() = true//!visited.contains(this.value)
        protected fun TreeNode<T>.markVisited() {
            visited.add(this.value)
        }
    }

    class DepthFirstSearch<T : Any> : TreeSearchBase<T>() {
        private val stack = LinkedList<TreeNode<T>>()
        private fun push(node: TreeNode<T>) {
            stack.addFirst(node)
            node.markVisited()
        }

        private fun pop(): TreeNode<T>? {
            val first: TreeNode<T>? = stack.first
            if (first != null) stack.removeFirst()
            return first
        }

        override fun search(root: TreeNode<T>): Sequence<TreeNode<T>> {
            if (root.isNotVisited()) push(root)
            return searchAll()
        }

        private fun searchAll(): Sequence<TreeNode<T>> = generateSequence {
            if (stack.isNotEmpty()) {
                val first = pop()!!
                first.children.filter { it.isNotVisited() }.forEach { push(it) }
                first
            } else null
        }
    }

    class BreadthFirstSearch<T : Any> : TreeSearchBase<T>() {
        private val queue = LinkedList<TreeNode<T>>()
        private fun queue(node: TreeNode<T>) {
            queue.addFirst(node)
            node.markVisited()
        }

        private fun dequeue(): TreeNode<T>? {
            val last: TreeNode<T>? = queue.last
            if (last != null) queue.removeLast()
            return last
        }

        override fun search(root: TreeNode<T>): Sequence<TreeNode<T>> {
            if (root.isNotVisited()) queue(root)
            return searchAll()
        }

        private fun searchAll(): Sequence<TreeNode<T>> = generateSequence {
            if (queue.isNotEmpty()) {
                val last = dequeue()!!
                last.children.filter { it.isNotVisited() }.forEach { queue(it) }
                last
            } else null
        }
    }
}