Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from sys import stdin

    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = stdin.readline()
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append((h, w))
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append((nh, nw))
                    cnt += 1
        q = nextq
    print(ans)
main()


Fixed submission:
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from sys import stdin

    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = stdin.readline()
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()


Fixed submission:
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
g = []
q = []
cnt = 0
for h in range(H):
    A = str(input())
    row = []
    for w in range(W):
        row.append(A[w])
        if A[w] == '#':
            cnt += 1
            q.append([h, w])
    g.append(row)
ans = 0
dh = [1, 0, 0, -1]
dw = [0, -1, 1, 0]
while cnt < H * W:
    ans += 1
    nextq = []
    while q:
        h, w = q.pop()
        for i in range(4):
            nh = h + dh[i]
            nw = w + dw[i]
            if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                g[nh][nw] = '#'
                nextq.append([nh, nw])
                cnt += 1
    q = nextq
print(ans)


Fixed submission:
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import copy

    H, W = map(int, input().split())
    g = []
    cnt = 0
    for _ in range(H):
        A = str(input())
        row = []
        for i in range(W):
            row.append(A[i])
            if A[i] == '#':
                cnt += 1
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        newg = copy.deepcopy(g)
        for h in range(H):
            for w in range(W):
                if g[h][w] == '#':
                    for i in range(4):
                        nh = h + dh[i]
                        nw = w + dw[i]
                        if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.' and newg[nh][nw] != '#':
                            newg[nh][nw] = '#'
                            cnt += 1
        g = newg
    print(ans)
main()


Fixed submission:
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from collections import deque

    H, W = map(int, input().split())
    g = [str(input()) for _ in range(H)]
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    for h in range(H):
        for w in range(W):
            if g[h][w] == '.':
                d = 0
                done = [[False] * W for _ in range(H)]
                done[h][w] = True
                q = deque([[h, w]])
                while q:
                    h, w = q.popleft()
                    d += 1
                    for i in range(4):
                        nh = h + dh[i]
                        nw = w + dw[i]
                        if 0 <= nh < H and 0 <= nw < W and done[nh][nw] == False:
                            if g[nh][nw] == '.':
                                done[nh][nw] = True
                                q.append([nh, nw])
                            else:
                                q.clear()
                                break
                ans = max(ans, d)
    print(ans)
main()

Fixed submission:
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

H, W = map(int, input().split())
g = [str(input()) for _ in range(H)]
ans = 0
dh = [1, 0, 0, -1]
dw = [0, -1, 1, 0]
for h in range(H):
    for w in range(W):
        if g[h][w] == '.':
            d = 0
            done = [[False] * W for _ in range(H)]
            done[h][w] = True
            q = deque([[h, w]])
            while q:
                h, w = q.popleft()
                d += 1
                for i in range(4):
                    nh = h + dh[i]
                    nw = w + dw[i]
                    if 0 <= nh < H and 0 <= nw < W and done[nh][nw] == False:
                        if g[nh][nw] == '.':
                            done[nh][nw] = True
                            q.append([nh, nw])
                        else:
                            q.clear()
                            break
            ans = max(ans, d)
print(ans)


Fixed submission:
def main():
    H, W = map(int, input().split())
    g = []
    q = []
    cnt = 0
    for h in range(H):
        A = str(input())
        row = []
        for w in range(W):
            row.append(A[w])
            if A[w] == '#':
                cnt += 1
                q.append([h, w])
        g.append(row)
    ans = 0
    dh = [1, 0, 0, -1]
    dw = [0, -1, 1, 0]
    while cnt < H * W:
        ans += 1
        nextq = []
        while q:
            h, w = q.pop()
            for i in range(4):
                nh = h + dh[i]
                nw = w + dw[i]
                if 0 <= nh < H and 0 <= nw < W and g[nh][nw] == '.':
                    g[nh][nw] = '#'
                    nextq.append([nh, nw])
                    cnt += 1
        q = nextq
    print(ans)
main()