Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
A = [input().strip() for _ in range(H)]
d = 0
if A[0][0]=="#":
    d += 1
que = deque([(0,0,d)])
hist = [[H*W for _ in range(W)] for _ in range(H)]
hist[0][0] = d
while que:
    i,j,d = que.popleft()
    if j+1<W and A[i][j+1]==A[i][j]:
        hist[i][j+1] = min(hist[i][j+1],d)
        que.append((i,j+1,hist[i][j+1]))
    elif j+1<W and A[i][j+1]!=A[i][j]:
        hist[i][j+1] = min(hist[i][j+1],d+1)
        que.append((i,j+1,hist[i][j+1]))
    if i+1<H and A[i+1][j]==A[i][j]:
        hist[i+1][j] = min(hist[i+1][j],d)
        que.append((i+1,j,hist[i+1][j]))
    elif i+1<H and A[i+1][j]!=A[i][j]:
        hist[i+1][j] = min(hist[i+1][j],d+1)
        que.append((i+1,j,hist[i+1][j]))
m = hist[H-1][W-1]
print((m+1)//2)

Fixed submission:
from collections import deque
H,W = map(int,input().split())
A = [input().strip() for _ in range(H)]
d = 0
if A[0][0]=="#":
    d += 1
que = deque([(0,0,d)])
hist = [[H*W for _ in range(W)] for _ in range(H)]
hist[0][0] = d
while que:
    i,j,d = que.popleft()
    if j+1<W and A[i][j+1]==A[i][j]:
        if hist[i][j+1]>d:
            hist[i][j+1] = d
            que.append((i,j+1,d))
    elif j+1<W and A[i][j+1]!=A[i][j]:
        if hist[i][j+1]>d+1:
            hist[i][j+1] = d+1
            que.append((i,j+1,d+1))
    if i+1<H and A[i+1][j]==A[i][j]:
        if hist[i+1][j]>d:
            hist[i+1][j] = d
            que.append((i+1,j,d))
    elif i+1<H and A[i+1][j]!=A[i][j]:
        if hist[i+1][j]>d+1:
            hist[i+1][j] = d+1
            que.append((i+1,j,d+1))
m = hist[H-1][W-1]
print((m+1)//2)

Buggy python submission (verdict: Runtime Error):
INFTY = 10**3
def paint0(i,j):
    global cnt
    if j+1<W and C[i][j+1]<0 and A[i][j+1]==0:
        C[i][j+1]=cnt
        paint0(i,j+1)
    if i-1>=0 and C[i-1][j]<0 and A[i-1][j]==0:
        C[i-1][j]=cnt
        paint0(i-1,j)
    if j-1>=0 and C[i][j-1]<0 and A[i][j-1]==0:
        C[i][j-1]==cnt
        paint0(i,j-1)
    if i+1<H and C[i+1][j]<0 and A[i+1][j]==0:
        C[i+1][j]=cnt
        paint0(i+1,j)
def paint1(i,j):
    if j+1<W and A[i][j+1]==1:
        A[i][j+1]=0
        paint1(i,j+1)
    if i-1>=0 and A[i-1][j]==1:
        A[i-1][j]=0
        paint1(i-1,j)
    if j-1>=0 and A[i][j-1]==1:
        A[i][j-1]=0
        paint1(i,j-1)
    if i+1<H and A[i+1][j]==1:
        A[i+1][j]=0
        paint1(i+1,j)
def paint2(i,j):
    global ind
    if j+1<W and A[i][j+1]==1 and C[i][j+1]<0:
        C[i][j+1]=ind
        paint2(i,j+1)
    elif j+1<W and A[i][j+1]==0 and dist[C[i][j+1]]>=INFTY:
        dist[C[i][j+1]] = dist[ind]+1
    if i-1>=0 and A[i-1][j]==1 and C[i-1][j]<0:
        C[i-1][j]=ind
        paint2(i-1,j)
    elif i-1>=0 and A[i-1][j]==0 and dist[C[i-1][j]]>=INFTY:
        dist[C[i-1][j]]=dist[ind]+1
    if j-1>=0 and A[i][j-1]==1 and C[i][j-1]<0:
        C[i][j-1]=ind
        paint2(i,j-1)
    elif j-1>=0 and A[i][j-1]==0 and dist[C[i][j-1]]>=INFTY:
        dist[C[i][j-1]]=dist[ind]+1
    if i+1<H and A[i+1][j]==1 and C[i+1][j]<0:
        C[i+1][j]=ind
        paint2(i+1,j)
    elif i+1<H and A[i+1][j]==0 and dist[C[i+1][j]]>=INFTY:
        dist[C[i+1][j]]=dist[ind]+1
    
H,W = map(int,input().split())
S = [input() for _ in range(H)]
A = [[0 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j]=="#":
            A[i][j] = 1
C = [[-1 for _ in range(W)] for _ in range(H)]
ans = 0
if A[0][0]==1:
    A[0][0]=0
    paint1(0,0)
    ans += 1
if A[H-1][W-1]==1:
    A[H-1][W-1]=0
    paint1(H-1,W-1)
    ans += 1
cnt = 0
for i in range(H):
    for j in range(W):
        if C[i][j]<0 and A[i][j]==0:
            C[i][j] = cnt
            paint0(i,j)
            cnt += 1
dist = [INFTY for _ in range(cnt)]
dist[0] = 0
for i in range(H):
    for j in range(W):
        if A[i][j]==0:
            ind = C[i][j]
            paint2(i,j)
x = C[H-1][W-1]
ans += dist[x]
print(ans)

Fixed submission:
from collections import deque
H,W = map(int,input().split())
A = [input().strip() for _ in range(H)]
d = 0
if A[0][0]=="#":
    d += 1
que = deque([(0,0,d)])
hist = [[H*W for _ in range(W)] for _ in range(H)]
hist[0][0] = d
while que:
    i,j,d = que.popleft()
    if j+1<W and A[i][j+1]==A[i][j]:
        if hist[i][j+1]>d:
            hist[i][j+1] = d
            que.append((i,j+1,d))
    elif j+1<W and A[i][j+1]!=A[i][j]:
        if hist[i][j+1]>d+1:
            hist[i][j+1] = d+1
            que.append((i,j+1,d+1))
    if i+1<H and A[i+1][j]==A[i][j]:
        if hist[i+1][j]>d:
            hist[i+1][j] = d
            que.append((i+1,j,d))
    elif i+1<H and A[i+1][j]!=A[i][j]:
        if hist[i+1][j]>d+1:
            hist[i+1][j] = d+1
            que.append((i+1,j,d+1))
m = hist[H-1][W-1]
print((m+1)//2)