Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

def dfs(h, w, count=0):
    if s[h][w]=='#':
        count+=1
    if h==H-1 and w==W-1:
        return count
    else:
        ans = float('inf')
        if w+1<W:
            ans = min(ans, dfs(h, w+1, count=count))
            if ans == 0:
                return 0
        if h+1<H:
            ans = min(ans, dfs(h+1, w, count=count))
            if ans == 0:
                return 0
        return ans
print(dfs(0,0,0))

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

dp = [[float('inf')]* W for _ in range(H)]

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

for c in range(W):
    for r in range(H):
        if r > 0:
            count=0
            if s[r-1][c]=='.' and s[r][c]=='#':
                count = 1
            dp[r][c] = min(dp[r][c], dp[r-1][c]+count)
        if c > 0:
            count=0
            if s[r][c-1]=='.' and s[r][c]=='#':
                count = 1
            dp[r][c] = min(dp[r][c], dp[r][c-1]+count)
print(dp[H-1][W-1])

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

def dfs(h, w, count=0):
    if s[h][w]=='#':
        count+=1
    if h==H-1 and w==W-1:
        return count
    else:
        ans = float('inf')
        if w+1<W:
            ans = min(ans, dfs(h, w+1, count=count))
        if h+1<H:
            ans = min(ans, dfs(h+1, w, count=count))
        return ans
print(dfs(0,0,0))

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

dp = [[float('inf')]* W for _ in range(H)]

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

for c in range(W):
    for r in range(H):
        if r > 0:
            count=0
            if s[r-1][c]=='.' and s[r][c]=='#':
                count = 1
            dp[r][c] = min(dp[r][c], dp[r-1][c]+count)
        if c > 0:
            count=0
            if s[r][c-1]=='.' and s[r][c]=='#':
                count = 1
            dp[r][c] = min(dp[r][c], dp[r][c-1]+count)
print(dp[H-1][W-1])

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

def dfs(h, w, count=0):
    if s[h][w]=='#':
        count+=1
    if h==H-1 and w==W-1:
        return count
    else:
        ans = float('inf')
        if w+1<W:
            ans = min(ans, dfs(h, w+1, count=count))
        if h+1<H:
            ans = min(ans, dfs(h+1, w, count=count))
        return ans
print(dfs(0,0,0))

Fixed submission:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]

dp = [[float('inf')]* W for _ in range(H)]

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

for c in range(W):
    for r in range(H):
        if r > 0:
            count=0
            if s[r-1][c]=='.' and s[r][c]=='#':
                count = 1
            dp[r][c] = min(dp[r][c], dp[r-1][c]+count)
        if c > 0:
            count=0
            if s[r][c-1]=='.' and s[r][c]=='#':
                count = 1
            dp[r][c] = min(dp[r][c], dp[r][c-1]+count)
print(dp[H-1][W-1])