Problem: Consider a grid with H rows and W columns of squares. Let (r, c) denote the square at the r-th row from the top and the c-th column from the left. Each square is painted black or white.  The grid is said to be _good_ if and only if the following condition is satisfied:    * From (1, 1), we can reach (H, W) by moving one square **right or down** repeatedly, while always being on a white square.  Note that (1, 1) and (H, W) must be white if the grid is good.  Your task is to make the grid good by repeating the operation below. Find the minimum number of operations needed to complete the task. It can be proved that you can always complete the task in a finite number of operations.    * Choose four integers r_0, c_0, r_1, c_1(1 \leq r_0 \leq r_1 \leq H, 1 \leq c_0 \leq c_1 \leq W). For each pair r, c (r_0 \leq r \leq r_1, c_0 \leq c \leq c_1), invert the color of (r, c) \- that is, from white to black and vice versa.

Submission attempts (in order):
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])

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])

Verdicts: Runtime Error

Accepted