Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
s = ['0' * (W+2)]
for _ in range(H):
    s.append('0' + input().rstrip() + '0')
s.append('0' * (W+2))

ans = []
if s[1][1] == '#': a = 1
else: a = 0
temp = [[1, 1, a]]

while temp:
    p = temp.pop()
    for y, x in [[p[0]+1, p[1]], [p[0], p[1]+1]]:
        if s[y][x] == '0':continue
        if s[y][x] == '#' and s[p[0]][p[1]] == '.':
            d = p[2] + 1
        if y == H and x == W:
            ans.append(d)
        else:
            temp.append([y, x, d])
            
print(min(ans))

Fixed submission:
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
s = [input().rstrip() for _ in range(H)]

dp = [[1000]*W for _ in range(H)]

if s[0][0] == '#': dp[0][0] = 1
else: dp[0][0] = 0

for j in range(W):
    for i in range(H):
        if i <= H - 2:

            if s[i][j] == '.' and s[i+1][j] == '#':
                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1)
            else: dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        if j <= W - 2:

            if s[i][j] == '.' and s[i][j+1] == '#':
                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)
            else: dp[i][j+1] = min(dp[i][j+1], dp[i][j])


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

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
s = ['0' * (W+2)]
for _ in range(H):
    s.append('0' + input().rstrip() + '0')
s.append('0' * (W+2))

ans = []
if s[1][1] == '#': a, b = 1, 1
else: a, b = 0, 0
temp = [[1, 1, a, b]]

while temp:
    p = temp.pop()
    for y, x in [[p[0]+1, p[1]], [p[0], p[1]+1]]:
        c = p[2]
        d = p[3]
        if s[y][x] == '0':continue
        if s[y][x] == '#':
            if c == 0:
                d += 1
                c = 1
        else:
            c = 0
        if y == H and x == W:
            ans.append(d)
        else:
            temp.append([y, x, c, d])
            
print(min(ans))

Fixed submission:
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
s = [input().rstrip() for _ in range(H)]

dp = [[1000]*W for _ in range(H)]

if s[0][0] == '#': dp[0][0] = 1
else: dp[0][0] = 0

for j in range(W):
    for i in range(H):
        if i <= H - 2:

            if s[i][j] == '.' and s[i+1][j] == '#':
                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1)
            else: dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        if j <= W - 2:

            if s[i][j] == '.' and s[i][j+1] == '#':
                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)
            else: dp[i][j+1] = min(dp[i][j+1], dp[i][j])


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

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
s = ['0' * (W+2)]
for _ in range(H):
    s.append('0' + input() + '0')
s.append('0' * (W+2))

ans = []
if s[1][1] == '#': a, b = 1, 1
else: a, b = 0, 0
temp = [[1, 1, a, b]]

while temp:
    p = temp.pop()
    if p[0] == H and p[1] == W:
        ans.append(p[3])
    for y, x in [[p[0]+1, p[1]], [p[0], p[1]+1]]:
        c = p[2]
        d = p[3]
        if s[y][x] == '0':continue
        if s[y][x] == '#':
            if c == 0:
                d += 1
                c = 1
        else:
            c = 0
        temp.append([y, x, c, d])
             
print(min(ans))

Fixed submission:
import sys
input = sys.stdin.readline

H, W = map(int, input().split())
s = [input().rstrip() for _ in range(H)]

dp = [[1000]*W for _ in range(H)]

if s[0][0] == '#': dp[0][0] = 1
else: dp[0][0] = 0

for j in range(W):
    for i in range(H):
        if i <= H - 2:

            if s[i][j] == '.' and s[i+1][j] == '#':
                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1)
            else: dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        if j <= W - 2:

            if s[i][j] == '.' and s[i][j+1] == '#':
                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1)
            else: dp[i][j+1] = min(dp[i][j+1], dp[i][j])


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