Problem p03053 (python)

Fixed submission:
from heapq import*
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#")for x in range(h)]+[["#"]*(w+2)]

stack=[]
for y in range(1,h+1):
    for x in range(1,w+1):
        if grid[y][x]=="#":
            grid[y][x]=0
            stack.append((y,x))

for i in range(h*w):
    tmp=[]
    for my,mx in stack:
        for y,x in ([0,1],[1,0],[-1,0],[0,-1]):
            if grid[my+y][mx+x]==".":
                grid[my+y][mx+x]=i+1
                tmp.append([my+y,mx+x])
    stack=tmp
    if len(tmp)==0:
        print(i)
        break

Buggy submission (verdict: Runtime Error):
from heapq import*
import sys;sys.setrecursionlimit(200000)
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#") for x in range(h)]+[["#"]*(w+2)]

g=0
x=1
y=1
lock=[]
for x in range(1,1+h):
    for y in range(1,1+w):
        if grid[y][x]=="#":
            heappush(lock,(y,x))
#print(lock)


while 1:
    locked=[]
    for y,x in lock:
        if grid[y][x]=='#':
            for i,j in([0,1],[1,0],[0,-1],[-1,0]):
                if grid[y+i][x+j]!="#":
                    if (y+i,x+j)in locked:
                        continue
                    else:
                        grid[y+i][x+j]="#"
                        heappush(locked,(y+i,x+j))
    g+=1
    lock=locked
#    print(lock)
#    for i in grid:
#        print(*i)
#    print()
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Buggy submission (verdict: Runtime Error):
from heapq import*
import sys;sys.setrecursionlimit(200000)
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#") for x in range(h)]+[["#"]*(w+2)]

g=0
x=1
y=1
lock=[]
while 1:
    for x in range(1,h+1):
        for y in range(1,w+1):
            if (y,x) in lock:
                continue
            if grid[y][x]=='#':
                for i,j in([0,1],[1,0],[0,-1],[-1,0]):
                    if grid[y+i][x+j]!="#":
                        grid[y+i][x+j]="#"
                        heappush(lock,(y+i,x+j))
    g+=1
    lock=[]
#    for i in grid:
#        print(*i)
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Buggy submission (verdict: Runtime Error):
from heapq import*
import sys;input=sys.stdin.readline
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#") for x in range(h)]+[["#"]*(w+2)]

g=0
x=1
y=1
lock=[]
while 1:
    for x in range(1,h+1):
        for y in range(1,w+1):
            if (y,x) in lock:
                continue
            if grid[y][x]=='#':
                for i,j in([0,1],[1,0],[0,-1],[-1,0]):
                    if grid[y+i][x+j]!="#":
                        grid[y+i][x+j]="#"
                        heappush(lock,(y+i,x+j))
    g+=1
    lock=[]
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#") for x in range(h)]+[["#"]*(w+2)]

g=0
x=1
y=1
lock=[]
while 1:
    for x in range(1,h+1):
        for y in range(1,w+1):
            if (y,x) in lock:
                continue
            if grid[y][x]=='#':
                for i,j in([0,1],[1,0],[0,-1],[-1,0]):
                    if grid[y+i][x+j]=="#":
                        continue
                    grid[y+i][x+j]="#"
                    lock.append((y+i,x+j))
    g+=1
    lock=[]
                    
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()