Buggy python submission (verdict: Time Limit Exceeded):
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[True] * (w + 2)]
    for _ in range(h):
        t += [[True] + [(c == "#") for c in input()] + [True]]
    t += [[True] * (w + 2)]
    kuro = []
    for i, ti in enumerate(t[1:-1]):
        for j, tij in enumerate(ti[1:-1]):
            if tij:
                kuro+=[(i + 1, j + 1)]
    cnt = -1
    while kuro:
        cnt += 1
        nxt = []
        for i, j in kuro:
            t[i][j] = True
            for di, dj in dt:
                ni, nj = i + di, j + dj
                if t[ni][nj] == False:
                    nxt+=[(ni, nj)]
        kuro = nxt
    print(cnt)


h, w = map(int, input().split())
f(h, w)


Fixed submission:
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[True] * (w + 2)]
    for _ in range(h):
        t += [[True] + [(c == "#") for c in input()] + [True]]
    t += [[True] * (w + 2)]
    kuro = []
    for i, ti in enumerate(t[1:-1]):
        for j, tij in enumerate(ti[1:-1]):
            if tij:
                kuro+=[(i + 1, j + 1)]
    cnt = -1
    while kuro:
        cnt += 1
        nxt = []
        for i, j in kuro:
            ti=t[i]
            if not ti[j+1]:
                ti[j + 1]=True
                nxt += [(i, j+1)]
            if not ti[j-1]:
                ti[j - 1]=True
                nxt += [(i, j-1)]
            if not t[i - 1][j]:
                t[i - 1][j]=True
                nxt += [(i - 1, j)]
            if not t[i + 1][j]:
                t[i + 1][j]=True
                nxt += [(i + 1, j)]
        kuro = nxt
    print(cnt)


h, w = map(int, input().split())
f(h, w)

Buggy python submission (verdict: Time Limit Exceeded):
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[9] * (w + 2)]
    for _ in range(h):
        t += [[9] + [(c == "#") * 1 for c in input()] + [9]]
    t += [[9] * (w + 2)]
    kuro = set()
    for i, ti in enumerate(t):
        for j, tij in enumerate(ti):
            if tij == 1:
                kuro.add((i, j))
    cnt = -1
    while kuro:
        cnt += 1
        nxt = set()
        for i, j in kuro:
            t[i][j] = 1
            for di, dj in dt:
                ni, nj = i + di, j + dj
                if t[ni][nj] == 0:
                    nxt.add((ni, nj))
        kuro = nxt - kuro
    print(cnt)


h, w = map(int, input().split())
f(h, w)


Fixed submission:
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[True] * (w + 2)]
    for _ in range(h):
        t += [[True] + [(c == "#") for c in input()] + [True]]
    t += [[True] * (w + 2)]
    kuro = []
    for i, ti in enumerate(t[1:-1]):
        for j, tij in enumerate(ti[1:-1]):
            if tij:
                kuro+=[(i + 1, j + 1)]
    cnt = -1
    while kuro:
        cnt += 1
        nxt = []
        for i, j in kuro:
            ti=t[i]
            if not ti[j+1]:
                ti[j + 1]=True
                nxt += [(i, j+1)]
            if not ti[j-1]:
                ti[j - 1]=True
                nxt += [(i, j-1)]
            if not t[i - 1][j]:
                t[i - 1][j]=True
                nxt += [(i - 1, j)]
            if not t[i + 1][j]:
                t[i + 1][j]=True
                nxt += [(i + 1, j)]
        kuro = nxt
    print(cnt)


h, w = map(int, input().split())
f(h, w)

Buggy python submission (verdict: Time Limit Exceeded):
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[9] * (w + 2)]
    for _ in range(h):
        t += [[9] + [(c == "#") * 1 for c in input()] + [9]]
    t += [[9] * (w + 2)]
    kuro = set()
    for i, ti in enumerate(t):
        for j, tij in enumerate(ti):
            if tij == 1:
                kuro.add((i, j))
    cnt = -1
    while kuro:
        cnt += 1
        nxt = set()
        for i, j in kuro:
            t[i][j] = 1
            for di, dj in dt:
                ni, nj = i + di, j + dj
                if t[ni][nj] == 0:
                    nxt.add((ni, nj))
        kuro = nxt - kuro
    print(cnt)


h, w = map(int, input().split())
f(h, w)


Fixed submission:
def f(h, w):
    dt = [[-1, 0], [0, -1], [0, 1], [1, 0]]
    t = [[True] * (w + 2)]
    for _ in range(h):
        t += [[True] + [(c == "#") for c in input()] + [True]]
    t += [[True] * (w + 2)]
    kuro = []
    for i, ti in enumerate(t[1:-1]):
        for j, tij in enumerate(ti[1:-1]):
            if tij:
                kuro+=[(i + 1, j + 1)]
    cnt = -1
    while kuro:
        cnt += 1
        nxt = []
        for i, j in kuro:
            ti=t[i]
            if not ti[j+1]:
                ti[j + 1]=True
                nxt += [(i, j+1)]
            if not ti[j-1]:
                ti[j - 1]=True
                nxt += [(i, j-1)]
            if not t[i - 1][j]:
                t[i - 1][j]=True
                nxt += [(i - 1, j)]
            if not t[i + 1][j]:
                t[i + 1][j]=True
                nxt += [(i + 1, j)]
        kuro = nxt
    print(cnt)


h, w = map(int, input().split())
f(h, w)