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):
import sys
import heapq

H,W = map(int,input().split())
maze = [list(input()) for i in range(H)]

def removeWalls(wallPointH,wallPointW):
  if maze[wallPointH][wallPointW]=="#":
    changeTo = "."
  elif maze[wallPointH][wallPointW]=="1":
    changeTo = "0"
  if maze[wallPointH][wallPointW]=="#" or maze[wallPointH][wallPointW]=="1":
    maze[wallPointH][wallPointW]=changeTo
    if wallPointH+1 <= H-1:
      removeWalls(wallPointH+1,wallPointW)
    if wallPointW+1 <= W-1:
      removeWalls(wallPointH,wallPointW+1)

que = [[0,0,0]]
heapq.heapify(que)
count = 0
      
while que:
  qu = heapq.heappop(que)
  if qu[0]==1:
    heapq.heappush(que, qu)
    count += 1
    for q in que:
      q[0]=0
      removeWalls(q[1],q[2])
  else:
    nowH = qu[1]
    nowW = qu[2]
    if nowH==H-1 and nowW==W-1:
      print(count)
      sys.exit()
    else:
      
      if nowH+1 <= H-1:
        if maze[nowH+1][nowW]=="
          maze[nowH+1][nowW]=="1"
          heapq.heappush(que, [1,nowH+1,nowW])
        elif maze[nowH+1][nowW]==".":
          maze[nowH+1][nowW]=="0"
          heapq.heappush(que, [0,nowH+1,nowW])
      if nowW+1 <= W-1:
        if maze[nowH][nowW+1]=="
          maze[nowH][nowW+1]=="1"
          heapq.heappush(que, [1,nowH,nowW+1])
        elif maze[nowH][nowW+1]==".":
          maze[nowH][nowW+1]=="0"
          heapq.heappush(que, [0,nowH,nowW+1])
        



import sys

H,W = map(int,input().split())
maze = [list(input()) for i in range(H)]



def removeWalls(wallPointH,wallPointW):
  if maze[wallPointH][wallPointW]=="#":
    maze[wallPointH][wallPointW]="."
    if wallPointH+1 <= H-1:
      removeWalls(wallPointH+1,wallPointW)
    if wallPointW+1 <= W-1:
      removeWalls(wallPointH,wallPointW+1)

que = [[0,0,0]]
count = 0
      
while que:
  #print(que)
  if que[0][0]==1:
    que.sort()
  if que[0][0]==1:
    count += 1
    for q in que:
      q[0]=0
      removeWalls(q[1],q[2])
  else:
    now = que.pop(0)
    nowH = now[1]
    nowW = now[2]
    if maze[nowH][nowW]=="
      que.append([1,nowH,nowW])
      continue
    elif nowH==H-1 and nowW==W-1:
      print(count)
      sys.exit()
    else:
      
      if nowH+1 <= H-1:
        que.append([0,nowH+1,nowW])
      if nowW+1 <= W-1:
        que.append([0,nowH,nowW+1])



import sys
import heapq

H,W = map(int,input().split())
maze = [list(input()) for i in range(H)]

def removeWalls(wallPointH,wallPointW):
  if maze[wallPointH][wallPointW]=="#":
    maze[wallPointH][wallPointW]="."
    if wallPointH+1 <= H-1:
      removeWalls(wallPointH+1,wallPointW)
    if wallPointW+1 <= W-1:
      removeWalls(wallPointH,wallPointW+1)

que = [[0,0,0]]
heapq.heapify(que)
count = 0
      
while que:
  qu = heapq.heappop(que)
  #print(que)
  if qu[0]==1:
    heapq.heappush(que, qu)
    count += 1
    for q in que:
      q[0]=0
      removeWalls(q[1],q[2])
  else:
    now = qu
    nowH = now[1]
    nowW = now[2]
    if maze[nowH][nowW]=="
      heapq.heappush(que, [1,nowH,nowW])
      continue
    elif nowH==H-1 and nowW==W-1:
      print(count)
      sys.exit()
    else:
      
      if nowH+1 <= H-1:
        heapq.heappush(que, [0,nowH+1,nowW])
      if nowW+1 <= W-1:
        heapq.heappush(que, [0,nowH,nowW+1])
        



import sys

H,W = map(int,input().split())
maze = [list(input()) for i in range(H)]

def removeWalls(wallPointH,wallPointW):
  if maze[wallPointH][wallPointW]=="#":
    maze[wallPointH][wallPointW]="."
    if wallPointH+1 <= H-1:
      removeWalls(wallPointH+1,wallPointW)
    if wallPointW+1 <= W-1:
      removeWalls(wallPointH,wallPointW+1)

que = [[0,0,0]]
count = 0
      
while que:
  #print(que)
  if que[0][0]==1:
    que.sort()
  if que[0][0]==1:
    count += 1
    for q in que:
      q[0]=0
      removeWalls(q[1],q[2])
  else:
    now = que.pop(0)
    nowH = now[1]
    nowW = now[2]
    if maze[nowH][nowW]=="
      que.append([1,nowH,nowW])
      continue
    elif nowH==H-1 and nowW==W-1:
      print(count)
      sys.exit()
    else:
      
      if nowH+1 <= H-1:
        que.append([0,nowH+1,nowW])
      if nowW+1 <= W-1:
        que.append([0,nowH,nowW+1])



import sys
import heapq

H,W = map(int,input().split())
maze = [list(input()) for i in range(H)]

def removeWalls(wallPointH,wallPointW):
  if maze[wallPointH][wallPointW]=="#":
    changeTo = "."
  elif maze[wallPointH][wallPointW]=="1":
    changeTo = "0"
  if maze[wallPointH][wallPointW]=="#" or maze[wallPointH][wallPointW]=="1":
    maze[wallPointH][wallPointW]=changeTo
    if wallPointH+1 <= H-1:
      removeWalls(wallPointH+1,wallPointW)
    if wallPointW+1 <= W-1:
      removeWalls(wallPointH,wallPointW+1)

if maze[0][0]=="#":
  flag=1
else:
  flag=0
      
que = [[flag,0,0]]
heapq.heapify(que)
count = 0
      
while que:
  qu = heapq.heappop(que)
  if qu[0]==1:
    heapq.heappush(que, qu)
    count += 1
    for q in que:
      q[0]=0
      removeWalls(q[1],q[2])
  else:
    nowH = qu[1]
    nowW = qu[2]
    if nowH==H-1 and nowW==W-1:
      print(count)
      sys.exit()
    else:
      
      if nowH+1 <= H-1:
        if maze[nowH+1][nowW]=="#":
          maze[nowH+1][nowW]="1"
          heapq.heappush(que, [1,nowH+1,nowW])
        elif maze[nowH+1][nowW]==".":
          maze[nowH+1][nowW]="0"
          heapq.heappush(que, [0,nowH+1,nowW])
      if nowW+1 <= W-1:
        if maze[nowH][nowW+1]=="#":
          maze[nowH][nowW+1]="1"
          heapq.heappush(que, [1,nowH,nowW+1])
        elif maze[nowH][nowW+1]==".":
          maze[nowH][nowW+1]="0"
          heapq.heappush(que, [0,nowH,nowW+1])
        



Verdicts: Wrong Answer

Time Limit Exceeded

Time Limit Exceeded

Time Limit Exceeded

Accepted