Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29


def main():
    H,W  =isRaw()
    ss = [sRaw() for _ in range(H)]
    memo = [[INF for _ in range(W)] for _ in range(H)]
    qu = deque()
    isF = 1 if ss[0][0] == "#" else 0
    qu.append([0, 0, isF, isF])
    ans = INF
    while len(qu) != 0:
        x, y, z, isF = qu.popleft()
        if x != W-1:
            nx = x+1
            nisF = 1 if ss[y][nx] == "#" else 0
            z2 = z
            if nisF == 1 and isF == 0:
                z2 += 1
            if memo[y][nx] < z2:
                continue
            memo[y][nx]=z2
            if nx == W-1 and y == H-1:
                ans = min(ans, z2)
                continue
            qu.append([nx, y, z2, nisF])
        if y != H-1:
            ny = y+1
            nisF = 1 if ss[ny][x] == "#" else 0
            z2 = z
            if nisF == 1 and isF == 0:
                z2 += 1
            if memo[ny][x] < z2:
                continue
            memo[ny][x] = z2
            if x == W-1 and ny == H-1:
                ans = min(ans, z2)
                continue
            qu.append([x, ny, z2, nisF])

    return ans

    
if __name__ == "__main__":
    print(main())


Fixed submission:
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29


def main():
    H,W  =isRaw()
    ss = [sRaw() for _ in range(H)]
    memo = [[INF for _ in range(W)] for _ in range(H)]
    qu = deque()
    isF = 1 if ss[0][0] == "#" else 0
    ans = INF
    memo[0][0] = isF
    for y in range(H):
        for x in range(W):
            s = ss[y][x]
            if x!=0:
                prevM = memo[y][x-1]
                prevS = ss[y][x-1]
                if s=="#" and prevS!="#":
                    memo[y][x] = min(memo[y][x],prevM+1)
                else:
                    memo[y][x] = min(memo[y][x], prevM)
            if y!=0:
                prevM = memo[y-1][x]
                prevS = ss[y-1][x]
                if s == "#" and prevS != "#":
                    memo[y][x] = min(memo[y][x], prevM+1)
                else:
                    memo[y][x] = min(memo[y][x], prevM)
    return memo[H-1][W-1]

    
if __name__ == "__main__":
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % mod) % mod
    return CONV()


def matchAB(a,b):
    #print ("matching",a,b)
    tmp = -1
    tmp_res =""
    for ai_start in range(len(a)):
        ai = ai_start
        notMached= False
        for bi,bc in enumerate(b):
            ac = a[ai]
            if ac!="?" and bc!="?" and ac!=bc:
                notMached = True
                break
            ai+=1
            if ai == len(a):
                if bi!=len(b):
                    tmp_res = b[bi+1:]
                break
        
        if notMached == False:
            tmp = ai_start
            if ai != len(a):
                tmp_res = a[ai+1:]  
            break
    if tmp == -1:
        #print(a+b, tmp, tmp_res)
        return a+b
    else:
        ans = a[:ai_start]
        for aidx in range(ai_start, ai_start+ min(len(a)-ai_start,len(b))):
            if a[aidx]=="?":
                ans += b[aidx-ai_start]
            else:
                ans+=a[aidx]
        #print (ans,tmp,tmp_res)
        return ans+tmp_res

def matchABC(a,b,c):
    s = matchAB(matchAB(a, b), c)
    #print(s)
    return len(s)

def isReach(ss):
    W = len(ss[0])
    H = len(ss)
    qu = deque()
    isF = 1 if ss[0][0] == "#" else 0
    qu.append([0,0,isF,isF])
    ans = INF
    while len(qu)!=0:
        x,y,z,isF = qu.popleft()
        if x == W-1 and y == H-1:
            ans = min(ans, z)
            continue
        if x!=W-1:
            nx = x+1
            nisF = 1 if ss[nx][y] == "#" else 0
            z2 = z
            if nisF==1 and isF==0:
                z2+=1
            qu.append([nx,y,z2,nisF])
        if y!=H-1:
            ny = y+1
            nisF = 1 if ss[x][ny] == "#" else 0
            z2 = z
            if nisF == 1 and isF == 0:
                z2 += 1
            qu.append([x, ny, z2, nisF])
        
    return ans

def main():
    H,W  =isRaw()
    ss = [sRaw() for _ in range(H)]
    return isReach(ss)
    
if __name__ == "__main__":
    print(main())


Fixed submission:
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29


def main():
    H,W  =isRaw()
    ss = [sRaw() for _ in range(H)]
    memo = [[INF for _ in range(W)] for _ in range(H)]
    qu = deque()
    isF = 1 if ss[0][0] == "#" else 0
    ans = INF
    memo[0][0] = isF
    for y in range(H):
        for x in range(W):
            s = ss[y][x]
            if x!=0:
                prevM = memo[y][x-1]
                prevS = ss[y][x-1]
                if s=="#" and prevS!="#":
                    memo[y][x] = min(memo[y][x],prevM+1)
                else:
                    memo[y][x] = min(memo[y][x], prevM)
            if y!=0:
                prevM = memo[y-1][x]
                prevS = ss[y-1][x]
                if s == "#" and prevS != "#":
                    memo[y][x] = min(memo[y][x], prevM+1)
                else:
                    memo[y][x] = min(memo[y][x], prevM)
    return memo[H-1][W-1]

    
if __name__ == "__main__":
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % mod) % mod
    return CONV()


def matchAB(a,b):
    #print ("matching",a,b)
    tmp = -1
    tmp_res =""
    for ai_start in range(len(a)):
        ai = ai_start
        notMached= False
        for bi,bc in enumerate(b):
            ac = a[ai]
            if ac!="?" and bc!="?" and ac!=bc:
                notMached = True
                break
            ai+=1
            if ai == len(a):
                if bi!=len(b):
                    tmp_res = b[bi+1:]
                break
        
        if notMached == False:
            tmp = ai_start
            if ai != len(a):
                tmp_res = a[ai+1:]  
            break
    if tmp == -1:
        #print(a+b, tmp, tmp_res)
        return a+b
    else:
        ans = a[:ai_start]
        for aidx in range(ai_start, ai_start+ min(len(a)-ai_start,len(b))):
            if a[aidx]=="?":
                ans += b[aidx-ai_start]
            else:
                ans+=a[aidx]
        #print (ans,tmp,tmp_res)
        return ans+tmp_res

def matchABC(a,b,c):
    s = matchAB(matchAB(a, b), c)
    #print(s)
    return len(s)

def isReach(ss):
    W = len(ss[0])
    H = len(ss)
    qu = deque()

    qu.append([0,0,[1 if ss[0][0]=="#" else 0]])
    ans = INF
    while len(qu)!=0:
        x,y,z = qu.popleft()
        if x!=W-1:
            z2 = z+[1 if ss[x+1][y]=="#" else 0]
            qu.append([x+1,y,z2])
        if y!=H-1:
            z2 = z+[1 if ss[x][y+1] == "#" else 0]
            qu.append([x, y+1, z2])
        if x==W-1 and y==H-1:
            buf = 0
            for zidx,az in enumerate(z):
                if az==1 and (zidx==0 or z[zidx-1]==0):
                    buf+=1
            ans = min(ans,buf)
    return ans

def main():
    H,W  =isRaw()
    ss = [sRaw() for _ in range(H)]
    return isReach(ss)
    
if __name__ == "__main__":
    print(main())


Fixed submission:
import bisect
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29


def main():
    H,W  =isRaw()
    ss = [sRaw() for _ in range(H)]
    memo = [[INF for _ in range(W)] for _ in range(H)]
    qu = deque()
    isF = 1 if ss[0][0] == "#" else 0
    ans = INF
    memo[0][0] = isF
    for y in range(H):
        for x in range(W):
            s = ss[y][x]
            if x!=0:
                prevM = memo[y][x-1]
                prevS = ss[y][x-1]
                if s=="#" and prevS!="#":
                    memo[y][x] = min(memo[y][x],prevM+1)
                else:
                    memo[y][x] = min(memo[y][x], prevM)
            if y!=0:
                prevM = memo[y-1][x]
                prevS = ss[y-1][x]
                if s == "#" and prevS != "#":
                    memo[y][x] = min(memo[y][x], prevM+1)
                else:
                    memo[y][x] = min(memo[y][x], prevM)
    return memo[H-1][W-1]

    
if __name__ == "__main__":
    print(main())