Problem p02735 (python)

Fixed submission:
from collections import deque
h,w = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
d = deque()
rl = [[h*w+1 for i in range(w)] for i in range(h)]
if s[0][0] == ".":
    d.append([0,0,0])
    rl[0][0] = 0
else:
    d.append([0,0,1])
    rl[0][0] = 1
while len(d):
    a,b,c = d.popleft()
    #print(a,b,rl)
    if a == h-1:
        if b < w-1:
            if rl[a][b+1] == h*w+1:
                if s[a][b+1] == "#":
                    if c > 0:
                        rl[a][b+1] = rl[a][b]
                        d.append([a,b+1,1])
                    else:
                        rl[a][b+1] = rl[a][b]+1
                        d.append([a,b+1,0])
                else:
                    rl[a][b+1] = rl[a][b]
                    d.append([a,b+1,0])
            else:
                if s[a][b+1] == "#":
                    if c > 0:
                        rl[a][b+1]= min(rl[a][b+1],rl[a][b])
                    else:
                        rl[a][b+1]= min(rl[a][b+1],rl[a][b]+1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
    else:
        if b == w-1:
            if rl[a+1][b] == h*w+1:
                if s[a+1][b] == "#":
                    if c > 0:
                        rl[a+1][b] = rl[a][b]
                        d.append([a+1,b,1])
                    else:
                        rl[a+1][b] = rl[a][b]+1
                        d.append([a+1,b,1])
                else:
                    rl[a+1][b] = rl[a][b]
                    d.append([a+1,b,0])
            else:
                if s[a+1][b] == "#":
                    if c > 0:
                        rl[a+1][b]= min(rl[a+1][b],rl[a][b])
                    else:
                        rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
        else:
            if rl[a][b+1] == h*w+1:
                if s[a][b+1] == "#":
                    if c > 0:
                        rl[a][b+1] = rl[a][b]
                        d.append([a,b+1,1])
                    else:
                        rl[a][b+1] = rl[a][b]+1
                        d.append([a,b+1,0])
                else:
                    rl[a][b+1] = rl[a][b]
                    d.append([a,b+1,0])
            else:
                if s[a][b+1] == "#":
                    if c > 0:
                        rl[a][b+1]= min(rl[a][b+1],rl[a][b])
                    else:
                        rl[a][b+1]= min(rl[a][b+1],rl[a][b]+1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
            if rl[a+1][b] == h*w+1:
                if s[a+1][b] == "#":
                    if c > 0:
                        rl[a+1][b] = rl[a][b]
                        d.append([a+1,b,1])
                    else:
                        rl[a+1][b] = rl[a][b]+1
                        d.append([a+1,b,1])
                else:
                    rl[a+1][b] = rl[a][b]
                    d.append([a+1,b,0])
            else:
                if s[a+1][b] == "#":
                    if c > 0:
                        rl[a+1][b]= min(rl[a+1][b],rl[a][b])
                    else:
                        rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
print(rl[h-1][w-1])

Buggy submission (verdict: Runtime Error):
from collections import deque
h,w = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
d = deque()
rl = [[h*w+1 for i in range(w)] for i in range(h)]
if s[0][0] == ".":
    d.append([0,0])
    rl[0][0] = 0
else:
    d.append([0,0])
    rl[0][0] = 1
while len(d):
    a,b = d.popleft()
    print(a,b,rl)
    if a == h-1:
        if b < w-1:
            if rl[a][b+1] == h*w+1:
                if s[a][b+1] == "#":
                    rl[a][b+1] = rl[a][b] + 1
                else:
                    rl[a][b+1] = rl[a][b]
                d.append([a,b+1])
            else:
                if s[a][b+1] == "#":
                    rl[a][b+1]= min(rl[a][b+1],rl[a][b] + 1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
    else:
        if b == w-1:
            if rl[a+1][b] == h*w+1:
                if s[a+1][b] == "#":
                    rl[a+1][b] = rl[a][b] + 1
                else:
                    rl[a+1][b] = rl[a][b]
                d.append([a+1,b])
            else:
                if s[a+1][b] == "#":
                    rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
        else:
            if rl[a][b+1] == h*w+1:
                if s[a][b+1] == "#":
                    rl[a][b+1] = rl[a][b] + 1
                else:
                    rl[a][b+1] = rl[a][b]
                d.append([a,b+1])
            else:
                if s[a][b+1] == "#":
                    rl[a][b+1]= min(rl[a][b+1],rl[a][b] + 1)
                else:
                    rl[a][b+1] = min(rl[a][b+1],rl[a][b])
            if rl[a+1][b] == h*w+1:
                if s[a+1][b] == "#":
                    rl[a+1][b] = rl[a][b] + 1
                else:
                    rl[a+1][b] = rl[a][b]
                d.append([a+1,b])
            else:
                if s[a+1][b] == "#":
                    rl[a+1][b]= min(rl[a+1][b],rl[a][b] + 1)
                else:
                    rl[a+1][b] = min(rl[a+1][b],rl[a][b])
print(rl[h-1][w-1])