Problem p03053 (python)

Fixed submission:
import sys
import copy
import  math
import bisect
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h, w = na()
maze = [list(input()) for _ in range(h)]
que = deque()
d = [[-1] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        if maze[i][j] == "#":
            d[i][j] = 0
            que.append((i, j))
l = 0

while que:
    x, y = que.popleft()
    l = d[x][y]
    for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        nx = x + dx
        ny = y + dy
        if not 0 <= nx < h or not 0 <= ny < w or d[nx][ny] != -1:
            continue
        d[nx][ny] = l + 1
        que.append((nx, ny))
print(l)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
pli = []
lens = [[2001 for i in range(w)] for j in range(h)]
for x in range(w):
    for y in range(h):
        if a[x][y] == '#':
            pli.append([x, y])
            lens[x][y] = 0
for y in range(h):
    for x in range(w):
        if [x, y] not in pli:
            for p in pli:
                tmp = abs(x - p[0]) + abs(y - p[1])
                if lens[x][y] > tmp:
                    lens[x][y] = tmp
print(np.max(lens))

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
pli = []
lens = [[2001 for i in range(w)] for j in range(h)]
for x in range(w):
    for y in range(h):
        if a[x][y] == '#':
            pli.append([x, y])
            lens[x][y] = 0
for y in range(h):
    for x in range(w):
        if [x, y] not in pli:
            for p in pli:
                tmp = abs(x - p[0]) + abs(y - p[1])
                if lens[x][y] > tmp:
                    lens[x][y] = tmp
print(np.max(lens))

Buggy submission (verdict: Runtime Error):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def change(a, x, y, h, w):
    ct = 0
    if x > 0:
        if a[x - 1][y] == '.':
            a[x - 1][y] = '#'
            ct += 1
    if y > 0:
        if a[x][y - 1] == '.':
            a[x][y - 1] = '#'
            ct += 1
    if x < w - 1:
        if a[x + 1][y] == '.':
            a[x + 1][y] = '#'
            ct += 1
    if y < h - 1:
        if a[x][y + 1] == '.':
            a[x][y + 1] = '#'
            ct += 1
    return ct


h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
for y in range(h):
    for x in range(w):
        if a[x][y] == '#':
            ct += 1
#print(ct)
while ct != ds:
    li = []
    for y in range(h):
        for x in range(w):
            if a[x][y] == '#':
                li.append([x, y])
    for p in li:
        ct += change(a, p[0], p[1], h, w)
    #print(ct)
    trial += 1

print(trial)

Buggy submission (verdict: Runtime Error):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def change(a, x, y, h, w):
    ct = 0
    if x > 0:
        if a[x - 1][y] == '.':
            a[x - 1][y] = '#'
            ct += 1
    if y > 0:
        if a[x][y - 1] == '.':
            a[x][y - 1] = '#'
            ct += 1
    if x < w - 1:
        if a[x + 1][y] == '.':
            a[x + 1][y] = '#'
            ct += 1
    if y < h - 1:
        if a[x][y + 1] == '.':
            a[x][y + 1] = '#'
            ct += 1
    return ct


h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
for y in range(h):
    for x in range(w):
        if a[x][y] == '#':
            ct += 1

while ct != ds:
    li = []
    for y in range(h):
        for x in range(w):
            if a[x][y] == '#':
                li.append([x, y])
    for p in li:
        ct += change(a, p[0], p[1], h, w)
    trial += 1

print(trial)

Buggy submission (verdict: Runtime Error):
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

def change(a, x, y, h, w):
    ct = 0
    if x > 0:
        if a[x - 1][y] == '.':
            a[x - 1][y] = '#'
            ct += 1
    if y > 0:
        if a[x][y - 1] == '.':
            a[x][y - 1] = '#'
            ct += 1
    if x < w - 1:
        if a[x + 1][y] == '.':
            a[x + 1][y] = '#'
            ct += 1
    if y < h - 1:
        if a[x][y + 1] == '.':
            a[x][y + 1] = '#'
            ct += 1
    return ct


h, w = na()
a = [list(input()) for i in range(h)]
ct = 0
ds = h * w
trial = 0
for y in range(h):
    for x in range(w):
        if a[x][y] == '#':
            ct += 1

while ct != ds:
    li = []
    for y in range(h):
        for x in range(w):
            if a[x][y] == '#':
                li.append([x, y])
    for p in li:
        ct += change(a, p[0], p[1], h, w)
    trial += 1

print(trial)