Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            fin += 1
        else:
            cnt[h][w] = -1

            
#print(cnt,fin)
dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    for h in range(1,H+1):
        for w in range(1,W+1):
            if cnt[h][w] == c:
                for dx,dy in dxy:
                    if cnt[h+dx][w+dy] == -1:
                        cnt[h+dx][w+dy] = c+1
                        #print(cnt)
                        fin += 1
    c += 1

print(c)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
st = deque()
sts = deque()
push, pop = st.append, st.pop
pushs, pops = sts.append, sts.pop
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            push([h,w])
            fin += 1
        else:
            cnt[h][w] = -1


dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    while st:
        [h,w] = pop()
        for dx,dy in dxy:
            if cnt[h+dx][w+dy] == -1:
                #print(c,fin)
                cnt[h+dx][w+dy] = c+1
                pushs([h+dx,w+dy])
                fin += 1
    c += 1
    while sts:
        [h,w] = pops()
        push([h,w])


print(c)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            fin += 1
        else:
            cnt[h][w] = -1

            
#print(cnt,fin)
dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    for h in range(1,H+1):
        for w in range(1,W+1):
            if cnt[h][w] == c:
                for dx,dy in dxy:
                    if cnt[h+dx][w+dy] == -1:
                        cnt[h+dx][w+dy] = c+1
                        #print(cnt)
                        fin += 1
    c += 1

ans = []
for h in range(1,H+1):
    ans.append(max(cnt[h]))

print(max(ans))

Fixed submission:
from collections import deque
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
st = deque()
sts = deque()
push, pop = st.append, st.pop
pushs, pops = sts.append, sts.pop
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            push([h,w])
            fin += 1
        else:
            cnt[h][w] = -1


dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    while st:
        [h,w] = pop()
        for dx,dy in dxy:
            if cnt[h+dx][w+dy] == -1:
                #print(c,fin)
                cnt[h+dx][w+dy] = c+1
                pushs([h+dx,w+dy])
                fin += 1
    c += 1
    while sts:
        [h,w] = pops()
        push([h,w])


print(c)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            fin += 1
        else:
            cnt[h][w] = -1

            
#print(cnt,fin)
dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    for h in range(1,H+1):
        for w in range(1,W+1):
            if cnt[h][w] == c:
                for dx,dy in dxy:
                    if cnt[h+dx][w+dy] == -1:
                        cnt[h+dx][w+dy] = c+1
                        #print(cnt)
                        fin += 1
    c += 1

ans = []
for h in range(1,H+1):
    ans.append(max(cnt[h]))

print(max(ans))

Fixed submission:
from collections import deque
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
st = deque()
sts = deque()
push, pop = st.append, st.pop
pushs, pops = sts.append, sts.pop
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            push([h,w])
            fin += 1
        else:
            cnt[h][w] = -1


dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    while st:
        [h,w] = pop()
        for dx,dy in dxy:
            if cnt[h+dx][w+dy] == -1:
                #print(c,fin)
                cnt[h+dx][w+dy] = c+1
                pushs([h+dx,w+dy])
                fin += 1
    c += 1
    while sts:
        [h,w] = pops()
        push([h,w])


print(c)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
a = [['#'] * (W+2) for i in range(H+2)]

for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        a[h][w] = ai[w-1]


cnt = 0
for h in range(1,H+1):
    for w in range(1,W+1):
        if a[h][w] == '#':
            cnt += 1


dxy = ([1,0],[0,1],[-1,0],[0,-1])
ans = 0
while cnt < H*W:
    ans +=1
    A = [['#'] * (W+2) for i in range(H+2)]
    for h in range(1,H+1):
        for w in range(1,W+1):
            A[h][w] = a[h][w]
    #print(a)
    for h in range(1,H+1):
        for w in range(1,W+1):
            if a[h][w] == '#':
                for dx,dy in dxy:
                    if A[h+dx][w+dy] == '.':
                        A[h+dx][w+dy] = '#'
                        #print(cnt,A)
                        cnt += 1
    for h in range(1,H+1):
        for w in range(1,W+1):
            a[h][w] = A[h][w]

print(ans)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
#a = [['#'] * (W+2) for i in range(H+2)]
cnt = [[-5] * (W+2) for i in range(H+2)]
c = 0
fin = 0
st = deque()
sts = deque()
push, pop = st.append, st.pop
pushs, pops = sts.append, sts.pop
for h in range(1,H+1):
    ai = input()
    for w in range(1,W+1):
        #a[h][w] = ai[w-1]
        if ai[w-1] == '#':
            cnt[h][w] = c
            push([h,w])
            fin += 1
        else:
            cnt[h][w] = -1


dxy = ([1,0],[0,1],[-1,0],[0,-1])

while fin < H*W:
    while st:
        [h,w] = pop()
        for dx,dy in dxy:
            if cnt[h+dx][w+dy] == -1:
                #print(c,fin)
                cnt[h+dx][w+dy] = c+1
                pushs([h+dx,w+dy])
                fin += 1
    c += 1
    while sts:
        [h,w] = pops()
        push([h,w])


print(c)