Buggy python submission (verdict: Runtime Error):
from collections import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
stack=deque([[0,0,0,S[0][0]=="#"]])
RuiW=[[0] for i in range(H)]
RuiH=[[0] for i in range(W)]
if S[0][0]=="#":
    RuiW[0][0]=1
    RuiH[0][0]=1

for h in range(H):
    flag=0
    for w in range(1,W):
        RuiW[h].append(RuiW[h][-1]+bool(S[h][w]=="#" and S[h][w-1]=="."))
for w in range(W):
    for h in range(1,H):
        RuiH[w].append(RuiH[w][-1]+bool(S[h][w]=="#" and S[h-1][w]=="."))

Range=range(1,100)
idle_max=10**9
visited=[[idle_max for i in range(W)] for m in range(H)]
#print(RuiW)
#print(RuiH)
while stack:
    h,w,m,Flag=stack.popleft()
    if visited[h][m]<m:
        continue
    for i in Range:
        if h+i>=H:
            break
        M=RuiH[w][h+i]-(RuiH[w][h]-RuiH[w][h]%2)+m

        if visited[h+i][w]>M:
            visited[h+i][w]=M
            stack.append([h+i,w,M,0])
    for i in Range:
        if w+i>=W:
            break
        M=RuiW[h][w+i]-(RuiW[h][w]-RuiW[h][w]%2)+m
        if visited[h][w+i]>M:
            visited[h][w+i]=M
            stack.append([h,w+i,M,0])
#print(visited)
print(visited[-1][-1])


Fixed submission:
from collections import *
from heapq import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
if S[0][0]=="#":
    count=1
else:
    count=0
idlemax=10**5
visited=[[idlemax for w in range(W)] for h in range(H)]
stack=deque([[0,0,bool(S[0][0]=="#")*1,bool(S[0][0]=="#")*1]])
while stack:

    h,w,m,flag=stack.popleft()
    if m>visited[h][w]:
        continue
    for y,x in [[0,1],[1,0]]:
        Y=h+y;X=w+x
        if Y<H and X<W:
            if flag:
                if visited[Y][X]>m:
                    if S[Y][X]==".":
                        nflag=0
                    else:
                        nflag=1
                    visited[Y][X]=m
                    stack.append([Y,X,m,nflag])
            else:
                if S[Y][X]=="#":
                    nflag=1
                    M=m+1
                else:
                    nflag=0
                    M=m
                if visited[Y][X]>M:
                    visited[Y][X]=M
                    stack.append([Y,X,M,nflag])
#print(visited)
print(visited[-1][-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
count=0
stack=deque([[0,0,0,S[0][0]=="#"]])
RuiW=[[0] for i in range(H)]
RuiH=[[0] for i in range(W)]
if S[0][0]=="#":
    RuiW[0][0]=1
    RuiH[0][0]=1

for h in range(H):
    flag=0
    for w in range(1,W):
        RuiW[h].append(RuiW[h][-1]+bool(S[h][w]=="#" and S[h][w-1]=="."))
for w in range(W):
    for h in range(1,H):
        RuiH[w].append(RuiH[w][-1]+bool(S[h][w]=="#" and S[h-1][w]=="."))

Range=range(1,100)
idle_max=10**9
visited=[[idle_max for i in range(W)] for m in range(H)]
#print(RuiW)
#print(RuiH)
while stack:
    h,w,m,Flag=stack.popleft()
    for i in Range:
        if h+i>=H:
            break
        M=RuiH[w][h+i]-(RuiH[w][h]-RuiH[w][h]%2)+m

        if visited[h+i][w]>M:
            visited[h+i][w]=M
            stack.append([h+i,w,M,0])
    for i in Range:
        if w+i>=W:
            break
        M=RuiW[h][w+i]-(RuiW[h][w]-RuiW[h][w]%2)+m
        if visited[h][w+i]>M:
            visited[h][w+i]=M
            stack.append([h,w+i,M,0])
#print(visited)
print(visited[-1][-1])


Fixed submission:
from collections import *
from heapq import *
H,W=map(int,input().split())
S=[input() for i in range(H)]
if S[0][0]=="#":
    count=1
else:
    count=0
idlemax=10**5
visited=[[idlemax for w in range(W)] for h in range(H)]
stack=deque([[0,0,bool(S[0][0]=="#")*1,bool(S[0][0]=="#")*1]])
while stack:

    h,w,m,flag=stack.popleft()
    if m>visited[h][w]:
        continue
    for y,x in [[0,1],[1,0]]:
        Y=h+y;X=w+x
        if Y<H and X<W:
            if flag:
                if visited[Y][X]>m:
                    if S[Y][X]==".":
                        nflag=0
                    else:
                        nflag=1
                    visited[Y][X]=m
                    stack.append([Y,X,m,nflag])
            else:
                if S[Y][X]=="#":
                    nflag=1
                    M=m+1
                else:
                    nflag=0
                    M=m
                if visited[Y][X]>M:
                    visited[Y][X]=M
                    stack.append([Y,X,M,nflag])
#print(visited)
print(visited[-1][-1])