Buggy python submission (verdict: Time Limit Exceeded):
H, W, *S = open(0).read().split()
H, W = [int(_) for _ in [H, W]]
dist = [[float('inf')] * W for _ in range(H)]
dist[0][0] = 0
stack = [0]
while True:
    if not stack:
        break
    i = stack.pop()
    x, y = divmod(i, W)
    d = dist[x][y]
    for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W:
            if S[nx][ny] == '#' and S[x][y] == '.':
                if dist[nx][ny] > d + 1:
                    dist[nx][ny] = d + 1
                    stack += [nx * W + ny]
            elif dist[nx][ny] > d:
                dist[nx][ny] = d
                stack += [nx * W + ny]
ans = dist[-1][-1]
if S[0][0] == '#':
    ans += 1
print(ans)


Fixed submission:
import collections
h, w = [int(_) for _ in input().split()]
S = [list('.' * (w + 1))] + [['.'] + list(input()) for _ in range(h)]
B = [[99999] * (w + 1) for _ in range(h + 1)]
B[1][1] = 0 if S[1][1] == '.' else 1
cand = collections.deque([(1, 2), (2, 1)])
while cand:
    x, y = cand.popleft()
    if B[x][y] == 99999:
        B[x][y] = min(B[x][y - 1] + (S[x][y] == '#' != S[x][y - 1]),
                      B[x - 1][y] + (S[x][y] == '#' != S[x - 1][y]))
        if x < h:
            cand += [(x + 1, y)]
        if y < w:
            cand += [(x, y + 1)]
print(B[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h, w = [int(_) for _ in input().split()]
S = [list(input()) for _ in range(h)]
B = [[-1] * w for _ in range(h)]
B[0][0] = 0 if S[0][0] == '.' else 1
cand = collections.deque([(0, 1), (1, 0)])
while cand:
    x, y = cand.popleft()
    s = 999
    if y:
        s = B[x][y - 1] + (S[x][y] == '#' != S[x][y - 1])
    if x:
        s = min(s, B[x - 1][y] + (S[x][y] == '#' != S[x - 1][y]))
    B[x][y] = s
    if x + 1 < h:
        cand += [(x + 1, y)]
    if y + 1 < w:
        cand += [(x, y + 1)]
print(B[-1][-1])


Fixed submission:
import collections
h, w = [int(_) for _ in input().split()]
S = [list('.' * (w + 1))] + [['.'] + list(input()) for _ in range(h)]
B = [[99999] * (w + 1) for _ in range(h + 1)]
B[1][1] = 0 if S[1][1] == '.' else 1
cand = collections.deque([(1, 2), (2, 1)])
while cand:
    x, y = cand.popleft()
    if B[x][y] == 99999:
        B[x][y] = min(B[x][y - 1] + (S[x][y] == '#' != S[x][y - 1]),
                      B[x - 1][y] + (S[x][y] == '#' != S[x - 1][y]))
        if x < h:
            cand += [(x + 1, y)]
        if y < w:
            cand += [(x, y + 1)]
print(B[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
import collections
h, w = [int(_) for _ in input().split()]
S = [list(input()) for _ in range(h)]
B = [[-1] * w for _ in range(h)]
B[0][0] = 0 if S[0][0] == '.' else 1
cand = collections.deque([(0, 1), (1, 0)])
while cand:
    x, y = cand.popleft()
    s = 999
    if y:
        s = B[x][y - 1] + (S[x][y] == '#' != S[x][y - 1])
    if x:
        s = min(s, B[x - 1][y] + (S[x][y] == '#' != S[x - 1][y]))
    B[x][y] = s
    if x + 1 < h:
        cand += [(x + 1, y)]
    if y + 1 < w:
        cand += [(x, y + 1)]
print(B[-1][-1])


Fixed submission:
import collections
h, w = [int(_) for _ in input().split()]
S = [list('.' * (w + 1))] + [['.'] + list(input()) for _ in range(h)]
B = [[99999] * (w + 1) for _ in range(h + 1)]
B[1][1] = 0 if S[1][1] == '.' else 1
cand = collections.deque([(1, 2), (2, 1)])
while cand:
    x, y = cand.popleft()
    if B[x][y] == 99999:
        B[x][y] = min(B[x][y - 1] + (S[x][y] == '#' != S[x][y - 1]),
                      B[x - 1][y] + (S[x][y] == '#' != S[x - 1][y]))
        if x < h:
            cand += [(x + 1, y)]
        if y < w:
            cand += [(x, y + 1)]
print(B[-1][-1])