Problem p03053 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, w = map(int, readline().split())
a = [input() for i in range(h)]
q = []
dist = [[False] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            q.append((i, j))
            dist[i][j] = True
test = [(0, 1), (0, -1), (1, 0), (-1, 0)]
cnt = -1
while q:
    cnt += 1
    qq = []
    for y, x in q:
        for dy, dx in test:
            yy, xx = y + dy, x + dx
            if xx < 0 or yy < 0 or xx > w - 1 or yy > h - 1 or dist[yy][xx]:
                continue
            else:
                dist[yy][xx] = True
                qq.append((yy, xx))
    q = qq
print(cnt)

Buggy submission (verdict: Runtime Error):
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, w = map(int, readline().split())
a = [input() for i in range(h)]
q = []
dist = [[False] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            q.append((i, j))
            dist[i][j] = True
test = [(0, 1), (0, -1), (1, 0), (-1, 0)]
cnt = -1
while q:
    cnt += 1
    qq = []
    for y, x in q:
        for dy, dx in test:
            yy, xx = y + dy, x + dx
            if xx < 0 or yy < 0 or xx > w - 1 or yy > h - 1 or dist[yy][xx]:
                continue
            else:
                dist[yy][xx] = True
                qq.append((yy, xx))
    q = qq
print(cnt)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, w = map(int, readline().split())
a = [input() for i in range(h)]
q = []
dist = [[False] * h for _ in range(w)]
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            q.append((i, j))
            dist[i][j] = True
test = [(0, 1), (0, -1), (1, 0), (-1, 0)]
cnt = -1
while q:
    cnt += 1
    qq = []
    for y, x in q:
        for dy, dx in test:
            yy, xx = y + dy, x + dx
            if xx < 0 or yy < 0 or xx > h - 1 or yy > w - 1 or dist[yy][xx]:
                continue
            else:
                dist[yy][xx] = True
                qq.append((yy, xx))
    q = qq
print(cnt)

Buggy submission (verdict: Runtime Error):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

h, w = map(int, readline().split())
a = [input() for i in range(h)]
q = []
dist = [[False] * h for _ in range(w)]
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            q.append((i, j))
            dist[i][j] = True
test = [(0, 1), (0, -1), (1, 0), (-1, 0)]
cnt = -1
while q:
    cnt += 1
    qq = []
    for y, x in q:
        for dy, dx in test:
            yy, xx = y + dy, x + dx
            if xx < 0 or yy < 0 or xx > w - 1 or yy > h - 1 or dist[yy][xx]:
                continue
            else:
                dist[yy][xx] = True
                qq.append((yy, xx))
    q = qq
print(cnt)