Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
HW = [[st for st in input()] for _ in range(H)]
inf = float('inf')
memo = [[inf]*W for _ in range(H)]

def bfs(HW,memo):
    black = []
    for ii in range(H):
        for jj in range(W):
            if HW[ii][jj] == "#":
                black.append((ii,jj))
                memo[ii][jj] = 0
    ans = 0
    while len(black) > 0:
            next = black.pop(0)
            i,j = next
            if 0 <= i - 1:
                if HW[i-1][j] == ".":
                    black.append((i-1,j))
                    HW[i-1][j] = "#"
                    memo[i-1][j] = min(memo[i-1][j],memo[i][j] + 1)
                    if memo[i-1][j] > ans:
                        ans = memo[i-1][j]
            if 0 <= i + 1 < H:
                if HW[i+1][j] == ".":
                    black.append((i+1,j))
                    HW[i+1][j] = "#"
                    memo[i+1][j] = min(memo[i+1][j],memo[i][j] + 1)
                    if memo[i+1][j] > ans:
                        ans = memo[i+1][j]
            if 0 <= j - 1:
                if HW[i][j-1] == ".":
                    black.append((i,j-1))
                    HW[i][j-1] = "#"
                    memo[i][j-1] = min(memo[i][j-1],memo[i][j] + 1)
                    if memo[i][j-1] > ans:
                        ans = memo[i][j-1]
            if j + 1 < W:
                if HW[i][j+1] == ".":
                    black.append((i,j+1))
                    HW[i][j+1] = "#"
                    memo[i][j+1] = min(memo[i][j+1],memo[i][j] + 1)
                    if memo[i][j+1] > ans:
                        ans = memo[i][j+1]
    return ans
print(bfs(HW,memo))






Fixed submission:
h, w = map(int, input().split())
A = [["#"]*(w+2)] + [["#"] + [s for s in input()] + ["#"]
                     for _ in range(h)] + [["#"]*(w+2)]


def main():
    step = 0
    Q = set([])
    for hh in range(1, h + 1):
        for ww in range(1, w + 1):
            if A[hh][ww] == "#":
                if A[hh - 1][ww] == ".":
                    Q.add((hh - 1, ww))
                if A[hh][ww - 1] == ".":
                    Q.add((hh, ww - 1))
                if A[hh + 1][ww] == ".":
                    Q.add((hh + 1, ww))
                if A[hh][ww + 1] == ".":
                    Q.add((hh, ww + 1))
    while len(Q) > 0:
        step += 1
        new = set([])
        for hh, ww in Q:
            A[hh][ww] = "#"
        while len(Q) > 0:
            hh, ww = Q.pop()
            if A[hh - 1][ww] == ".":
                A[hh-1][ww] = "#"
                new.add((hh - 1, ww))
            if A[hh][ww - 1] == ".":
                A[hh][ww-1] = "#"
                new.add((hh, ww - 1))
            if A[hh + 1][ww] == ".":
                A[hh+1][ww] = "#"
                new.add((hh + 1, ww))
            if A[hh][ww + 1] == ".":
                A[hh][ww + 1] = "#"
                new.add((hh, ww + 1))
        Q = new.copy()
    print(step)
    return


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
HW = [[st for st in input()] for _ in range(H)]
inf = float('inf')
memo = [[inf]*W for _ in range(H)]

def bfs(HW,memo):
    black = []
    for ii in range(H):
        for jj in range(W):
            if HW[ii][jj] == "#":
                black.append((ii,jj))
                memo[ii][jj] = 0
    ans = 0
    while len(black) > 0:
            next = black.pop(0)
            i,j = next
            if 0 <= i - 1:
                if HW[i-1][j] == ".":
                    black.append((i-1,j))
                    HW[i-1][j] = "#"
                    memo[i-1][j] = min(memo[i-1][j],memo[i][j] + 1)
                    if memo[i-1][j] > ans:
                        ans = memo[i-1][j]
            if 0 <= i + 1 < H:
                if HW[i+1][j] == ".":
                    black.append((i+1,j))
                    HW[i+1][j] = "#"
                    memo[i+1][j] = min(memo[i+1][j],memo[i][j] + 1)
                    if memo[i+1][j] > ans:
                        ans = memo[i+1][j]
            if 0 <= j - 1:
                if HW[i][j-1] == ".":
                    black.append((i,j-1))
                    HW[i][j-1] = "#"
                    memo[i][j-1] = min(memo[i][j-1],memo[i][j] + 1)
                    if memo[i][j-1] > ans:
                        ans = memo[i][j-1]
            if j + 1 < W:
                if HW[i][j+1] == ".":
                    black.append((i,j+1))
                    HW[i][j+1] = "#"
                    memo[i][j+1] = min(memo[i][j+1],memo[i][j] + 1)
                    if memo[i][j+1] > ans:
                        ans = memo[i][j+1]
    return ans
print(bfs(HW,memo))

Fixed submission:
h, w = map(int, input().split())
A = [["#"]*(w+2)] + [["#"] + [s for s in input()] + ["#"]
                     for _ in range(h)] + [["#"]*(w+2)]


def main():
    step = 0
    Q = set([])
    for hh in range(1, h + 1):
        for ww in range(1, w + 1):
            if A[hh][ww] == "#":
                if A[hh - 1][ww] == ".":
                    Q.add((hh - 1, ww))
                if A[hh][ww - 1] == ".":
                    Q.add((hh, ww - 1))
                if A[hh + 1][ww] == ".":
                    Q.add((hh + 1, ww))
                if A[hh][ww + 1] == ".":
                    Q.add((hh, ww + 1))
    while len(Q) > 0:
        step += 1
        new = set([])
        for hh, ww in Q:
            A[hh][ww] = "#"
        while len(Q) > 0:
            hh, ww = Q.pop()
            if A[hh - 1][ww] == ".":
                A[hh-1][ww] = "#"
                new.add((hh - 1, ww))
            if A[hh][ww - 1] == ".":
                A[hh][ww-1] = "#"
                new.add((hh, ww - 1))
            if A[hh + 1][ww] == ".":
                A[hh+1][ww] = "#"
                new.add((hh + 1, ww))
            if A[hh][ww + 1] == ".":
                A[hh][ww + 1] = "#"
                new.add((hh, ww + 1))
        Q = new.copy()
    print(step)
    return


if __name__ == "__main__":
    main()