Problem p03053 (python)

Fixed submission:
H, W = tuple(map(int, input().split()))
func = (lambda x: 0 if x == "#" else -1)
func_in = (lambda h, w: 0 <= h < H and 0 <= w < W)
P = [[func(i) for i in input()] for i in range(H)]

queue_set = set()
zeros = set()
for h in range(H):
    for w in range(W):
        if P[h][w] == 0:
            zeros.add((h)*W+w)
            for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                if func_in(h+dh, w+dw) and P[h+dh][w+dw] == -1:
                    queue_set.add((h+dh)*W+w+dw)
m = 0

queue = [[q // W, q % W, 0] for q in queue_set]
queue_set |= zeros
idx = 0
cnt = 0
while len(queue) > idx:
    h, w, cnt = queue[idx]
    idx += 1
    cnt += 1
    P[h][w] = cnt
    a = 0
    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if func_in(h+dh, w+dw) and (h+dh)*W+w+dw not in queue_set:
            queue += [[h + dh, w + dw, cnt]]
            queue_set.add((h+dh)*W+w+dw)
print(cnt)

Buggy submission (verdict: Runtime Error):
H, W = tuple(map(int, input().split()))
func = (lambda x: 0 if x == "#" else -1)
func_in = (lambda h, w: 0 <= h < H and 0 <= w < W)
P = [[func(i) for i in input()] for i in range(H)]

queue_set = set()
zeros = set()
for h in range(H):
    for w in range(W):
        if P[h][w] == 0:
            zeros.add((h)*W+w)
            for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                if func_in(h+dh, w+dw) and P[h+dh][w+dw] == -1:
                    queue_set.add((h+dh)*W+w+dw)
m = 0

queue = [[q // W, q % W, 0] for q in queue_set]
queue_set |= zeros
idx = 0
while len(queue) > idx:
    h, w, cnt = queue[idx]
    idx += 1
    cnt += 1
    P[h][w] = cnt
    a = 0
    for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if func_in(h+dh, w+dw) and (h+dh)*W+w+dw not in queue_set:
            queue += [[h + dh, w + dw, cnt]]
            queue_set.add((h+dh)*W+w+dw)
print(cnt)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
H, W = tuple(map(int, input().split()))
P = [[-int(i) for i in input().rstrip().replace("#", "0").replace(".", "1")]
     for i in range(H)]

queue_set = set()
for h in range(H):
    for w in range(W):
        if P[h][w] == 0:
            for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                if 0 <= h+dh < H and 0 <= w+dw < W and P[h+dh][w+dw] == -1:
                    queue_set.add((h+dh)*W+w+dw)
        elif h == 0 or h == H-1 or w == 0 or w == W-1:
            queue_set.add(h*W+w)
m = 0

queue = [[q//W, q % W, 0] for q in queue_set]
idx = 0
while len(queue) != idx+1:
    h, w, cnt = queue[idx]
    idx += 1
    if P[h][w] == -1:
        P[h][w] = cnt+1
        a = 0
        for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            if 0 <= h+dh < H and 0 <= w+dw < W:
                a = P[h+dh][w+dw] if P[h+dh][w+dw] > a else a
                if P[h+dh][w+dw] == -1:
                    queue += [[h+dh, w+dw, cnt+1]]

        m = m if cnt+1 + a < m else cnt+1 + a

print(m)

Buggy submission (verdict: Runtime Error):
H, W = tuple(map(int, input().split()))

P = [[-int(i) for i in input().replace("#", "0").replace(".", "1")]
     for i in range(H)]

queue_set = set()
for h in range(H):
    for w in range(W):
        if P[h][w] == 0:
            for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                if 0 <= h+dh < H and 0 <= w+dw < W and P[h+dh][w+dw] == -1:
                    queue_set.add((h+dh)*W+w+dw)
        elif h == 0 or h == H-1 or w == 0 or w == W-1:
            queue_set.add(h*W+w)
m = 0

queue = [[q//W, q % W, 0] for q in queue_set]
idx = 0
while len(queue) != idx+1:
    h, w, cnt = queue[idx]
    idx += 1
    if P[h][w] == -1:
        P[h][w] = cnt+1
        a = 0
        for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            if 0 <= h+dh < H and 0 <= w+dw < W:
                a = max(P[h+dh][w+dw], a)
                if P[h+dh][w+dw] == -1:
                    queue += [[h+dh, w+dw, cnt+1]]
        m = max(m, cnt+1 + a)

print(m)

Buggy submission (verdict: Runtime Error):
H,W = tuple(map(int,input().split()))

P = [[-int(i) for i in input().replace("#","0").replace(".","1")] for i in range(H)]

queue_set = set()
for h in range(H):
    for w in range(W):
        if  P[h][w]==0:
            for dh,dw in [(-1,0),(1,0),(0,-1),(0,1)]:
                if 0<=h+dh<H and 0<= w+dw <W:
                    queue_set.add((h+dh)*W+w+dw )
        elif h == 0 or h==H-1 or w==0 or w==W-1:
            queue_set.add(h*W+w)
m = 0

queue = [[q//W,q%W,0] for q in  queue_set]


while len(queue) != 1:
    h,w,cnt = queue.pop(0)
    if P[h][w] == -1:
        P[h][w] = cnt+1
        a = 0
        for dh,dw in [(-1,0),(1,0),(0,-1),(0,1)]:
            if 0<=h+dh<H and 0<= w+dw <W:
                a = max(P[h+dh][w+dw],a)
                if P[h+dh][w+dw]==-1:
                    queue+=[[h+dh,w+dw,cnt+1]]       
        m = max(m, cnt+1 + a)

print(m)