#!/usr/bin/env python3

from collections import deque



h, w = list(map(int, input().split()))

maze = [[i == "#" for i in eval(input())] for _ in range(h)]



dx = [1, 0]

dy = [0, 1]



que = deque([])

que.append((0, 0, maze[0][0], 1 if maze[0][0] else 0))

count = [[h + w] * w for _ in range(h)]



while que:

    x, y, b, c = que.pop()

    if c >= count[x][y]:

        continue

    count[x][y] = c

    if x == h - 1 and y == w - 1:

        continue

    for i in range(2):

        nx = x + dx[i]

        ny = y + dy[i]



        if not (0 <= nx < h and 0 <= ny < w):

            continue

        que.append((nx, ny, maze[nx][ny], c + (not b and maze[nx][ny])))

print((count[h - 1][w - 1]))

h, w = list(map(int, input().split()))

s = [[i == "#" for i in eval(input())] for _ in range(h)]

dp = [[10**9] * w for _ in range(h)]

dp[0][0] = int(s[0][0])

for i in range(h):

    for j in range(w):

        a = dp[i - 1][j] + (not s[i - 1][j] and s[i][j])

        b = dp[i][j - 1] + (not s[i][j -1] and s[i][j])

        dp[i][j] = min(a, b,dp[i][j])

print((dp[-1][-1]))

#!/usr/bin/env python3
from collections import deque

h, w = list(map(int, input().split()))
maze = [[i == "#" for i in eval(input())] for _ in range(h)]
dx = [1, 0]
dy = [0, 1]
que = deque([])
que.append((0, 0, maze[0][0], 1 if maze[0][0] else 0))
count = [[h + w] * w for _ in range(h)]
while que:
    x, y, b, c = que.pop()
    if c >= count[x][y]:
        continue
    count[x][y] = c
    if x == h - 1 and y == w - 1:
        continue
    for i in range(2):
        nx = x + dx[i]
        ny = y + dy[i]
        if not (0 <= nx < h and 0 <= ny < w):
            continue
        que.append((nx, ny, maze[nx][ny], c + (not b and maze[nx][ny])))
print((count[h - 1][w - 1]))

h, w = list(map(int, input().split()))
s = [[i == "#" for i in eval(input())] for _ in range(h)]
dp = [[10**9] * w for _ in range(h)]
dp[0][0] = int(s[0][0])
for i in range(h):
    for j in range(w):
        a = dp[i - 1][j] + (not s[i - 1][j] and s[i][j])
        b = dp[i][j - 1] + (not s[i][j - 1] and s[i][j])
        dp[i][j] = min(a, b, dp[i][j])
print((dp[-1][-1]))

-#!/usr/bin/env python3 -from collections import deque - -maze = [[i == "#" for i in eval(input())] for _ in range(h)] -dx = [1, 0] -dy = [0, 1] -que = deque([]) -que.append((0, 0, maze[0][0], 1 if maze[0][0] else 0)) -count = [[h + w] * w for _ in range(h)] -while que: -    x, y, b, c = que.pop() -    if c >= count[x][y]: -        continue -    count[x][y] = c -    if x == h - 1 and y == w - 1: -        continue -    for i in range(2): -        nx = x + dx[i] -        ny = y + dy[i] -        if not (0 <= nx < h and 0 <= ny < w): -            continue -        que.append((nx, ny, maze[nx][ny], c + (not b and maze[nx][ny]))) -print((count[h - 1][w - 1])) +s = [[i == "#" for i in eval(input())] for _ in range(h)] +dp = [[10**9] * w for _ in range(h)] +dp[0][0] = int(s[0][0]) +for i in range(h): +    for j in range(w): +        a = dp[i - 1][j] + (not s[i - 1][j] and s[i][j]) +        b = dp[i][j - 1] + (not s[i][j - 1] and s[i][j]) +        dp[i][j] = min(a, b, dp[i][j]) +print((dp[-1][-1]))