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

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for j in range(max(W, H)):
            if 0<=i-j<min(W, H):
                if j==0:
                    if s[j][i-j-1]=='.' and s[j][i-j]=='#':
                        ans[j][i-j] = ans[j][i-j-1] + 1
                    else:
                        ans[j][i-j] = ans[j][i-j-1]
                elif i-j==0:
                    if s[j-1][i-j]=='.' and s[j][i-j]=='#':
                        ans[j][i-j] = ans[j-1][i-j] + 1
                    else:
                        ans[j][i-j] = ans[j-1][i-j]
                else:
                    if (s[j-1][i-j]=='.' or s[j][i-j-1]=='.') and s[j][i-j]=='#':
                        ans[j][i-j] = min(ans[j-1][i-j], ans[j][i-j-1]) + 1
                    else:
                        ans[j][i-j] = min(ans[j-1][i-j], ans[j][i-j-1])
    # print(ans)
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for y in range(max(0, i-(W-1)), min(i+1, H)):
            x = i-y
            if y==0:
                if s[y][x-1]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y][x-1] + 1
                else:
                    ans[y][x] = ans[y][x-1]
            elif x==0:
                if s[y-1][x]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y-1][x] + 1
                else:
                    ans[y][x] = ans[y-1][x]
            else:
                if s[y][x]=='#':
                    ans[y][x] = min(ans[y-1][x]+(1 if s[y-1][x]=='.' else 0), ans[y][x-1]+(1 if s[y][x-1]=='.' else 0))
                else:
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1])
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

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

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H):
        for j in range(max(W, H)):
            if 0<=i-j<min(W, H):
                if j==0:
                    if s[j][i-j-1]=='.' and s[j][i-j]=='#':
                        ans[j][i-j] = ans[j][i-j-1] + 1
                    else:
                        ans[j][i-j] = ans[j][i-j-1]
                elif i-j==0:
                    if s[j-1][i-j]=='.' and s[j][i-j]=='#':
                        ans[j][i-j] = ans[j-1][i-j] + 1
                    else:
                        ans[j][i-j] = ans[j-1][i-j]
                else:
                    if (s[j-1][i-j]=='.' or s[j][i-j-1]=='.') and s[j][i-j]=='#':
                        ans[j][i-j] = min(ans[j-1][i-j], ans[j][i-j-1]) + 1
                    else:
                        ans[j][i-j] = min(ans[j-1][i-j], ans[j][i-j-1])
    # print(ans)
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for y in range(max(0, i-(W-1)), min(i+1, H)):
            x = i-y
            if y==0:
                if s[y][x-1]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y][x-1] + 1
                else:
                    ans[y][x] = ans[y][x-1]
            elif x==0:
                if s[y-1][x]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y-1][x] + 1
                else:
                    ans[y][x] = ans[y-1][x]
            else:
                if s[y][x]=='#':
                    ans[y][x] = min(ans[y-1][x]+(1 if s[y-1][x]=='.' else 0), ans[y][x-1]+(1 if s[y][x-1]=='.' else 0))
                else:
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1])
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

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

def resolve():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    # 全ルートの中で、連続する#の数が一番少ないものを探す

    ans = [H+W]
    dir = ((1, 0), (0, 1))
    # 前の色、前の進行方向、今までの連続ブロックの数
    
    def dfs(x, y, pw, pd, cnt):
        if x==W-1 and y==H-1:
            if pw=='.' and s[y][x]=='#':
                cnt += 1
            ans[0] = min(cnt, ans[0])
            if ans[0]==0:
                print(ans[0])
                sys.exit()
        else:
            for d in dir:
                nx = x+d[0]
                ny = y+d[1]
                if nx<W and ny<H:
                    ncnt = cnt
                    # 前が白で今が黒
                    if pw=='.' and s[y][x]=='#':
                        ncnt += 1
                    dfs(nx, ny, s[y][x], d, ncnt)

    dfs(0, 0, '.', dir[0], 0)
    print(ans[0])

if __name__ == '__main__':
    resolve()

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for y in range(max(0, i-(W-1)), min(i+1, H)):
            x = i-y
            if y==0:
                if s[y][x-1]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y][x-1] + 1
                else:
                    ans[y][x] = ans[y][x-1]
            elif x==0:
                if s[y-1][x]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y-1][x] + 1
                else:
                    ans[y][x] = ans[y-1][x]
            else:
                if s[y][x]=='#':
                    ans[y][x] = min(ans[y-1][x]+(1 if s[y-1][x]=='.' else 0), ans[y][x-1]+(1 if s[y][x-1]=='.' else 0))
                else:
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1])
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

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

def resolve():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    # 全ルートの中で、連続する#の数が一番少ないものを探す

    ans = [H+W]
    dir = ((1, 0), (0, 1))
    # 前の色、前の進行方向、今までの連続ブロックの数
    
    def dfs(x, y, pw, pd, cnt):
        if x==W-1 and y==H-1:
            if pw=='.' and s[y][x]=='#':
                cnt += 1
            ans[0] = min(cnt, ans[0])
        else:
            for d in dir:
                nx = x+d[0]
                ny = y+d[1]
                if nx<W and ny<H:
                    ncnt = cnt
                    # 前が白で今が黒
                    if pw=='.' and s[y][x]=='#':
                        ncnt += 1
                    dfs(nx, ny, s[y][x], d, ncnt)

    dfs(0, 0, '.', dir[0], 0)
    print(ans[0])

if __name__ == '__main__':
    resolve()

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for y in range(max(0, i-(W-1)), min(i+1, H)):
            x = i-y
            if y==0:
                if s[y][x-1]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y][x-1] + 1
                else:
                    ans[y][x] = ans[y][x-1]
            elif x==0:
                if s[y-1][x]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y-1][x] + 1
                else:
                    ans[y][x] = ans[y-1][x]
            else:
                if s[y][x]=='#':
                    ans[y][x] = min(ans[y-1][x]+(1 if s[y-1][x]=='.' else 0), ans[y][x-1]+(1 if s[y][x-1]=='.' else 0))
                else:
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1])
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()

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

def resolve():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    # 全ルートの中で、連続する#の数が一番少ないものを探す

    ans = [H+W]
    dir = ((1, 0), (0, 1))
    # 前の色、前の進行方向、今までの連続ブロックの数
    
    def dfs(x, y, pw, pd, cnt):
        if x==W-1 and y==H-1:
            if pw=='.' and s[y][x]=='#':
                cnt += 1
            ans[0] = min(cnt, ans[0])
        else:
            for d in dir:
                nx = x+d[0]
                ny = y+d[1]
                if nx<W and ny<H:
                    ncnt = cnt
                    # 前が白で今が黒
                    if pw=='.' and s[y][x]=='#':
                        ncnt += 1
                    dfs(nx, ny, s[y][x], d, ncnt)

    dfs(0, 0, '.', dir[0], 0)
    print(ans[0])

if __name__ == '__main__':
    resolve()

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip() 

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

    ans = [[0]*W for _ in range(H)]
    if s[0][0]=='#':
        ans[0][0] = 1
    for i in range(1, W+H-1):
        for y in range(max(0, i-(W-1)), min(i+1, H)):
            x = i-y
            if y==0:
                if s[y][x-1]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y][x-1] + 1
                else:
                    ans[y][x] = ans[y][x-1]
            elif x==0:
                if s[y-1][x]=='.' and s[y][x]=='#':
                    ans[y][x] = ans[y-1][x] + 1
                else:
                    ans[y][x] = ans[y-1][x]
            else:
                if s[y][x]=='#':
                    ans[y][x] = min(ans[y-1][x]+(1 if s[y-1][x]=='.' else 0), ans[y][x-1]+(1 if s[y][x-1]=='.' else 0))
                else:
                    ans[y][x] = min(ans[y-1][x], ans[y][x-1])
    print(ans[-1][-1])

if __name__ == '__main__':
    resolve()