Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w = list(map(int,input().split()))
s = [[x for x in input()] for _ in range(h)]
b = deque([])
for i in range(h):
  for j in range(w):
    if s[i][j] == '#':
      s[i][j] = 0
      b.append([i,j])
    if s[i][j] == '.':
      s[i][j] = 1

dh = [1,0,-1,0]
dw = [0,1,0,-1]

def rev(b):
  cnt = 0
  nb = deque([])
  for p in b:
    for i in range(4):
      nh = p[0] + dh[i]
      nw = p[1] + dw[i]
      
      if nh >=0 and nh < h and nw >=0 and nw < w and s[nh][nw]==1:
        s[nh][nw] = 0
        nb.append([nh,nw])
  return nb
cnt = 0
while sum([sum(x) for x in s]) > 0:
  b = rev(b)
  cnt += 1

print(cnt)    


Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w = list(map(int,input().split()))
s = [[x for x in input()] for _ in range(h)]

def find_w():
  for i in range(h):
    for j in range(w):
      if s[i][j] == '.':
        return True

def find_b():
  b = deque([])
  for i in range(h):
    for j in range(w):
      if s[i][j] == '#':
        b.append([i,j])
  return b

dh = [1,0,-1,0]
dw = [0,1,0,-1]

def rev():
  que = find_b()
  for b in que:
    for i in range(4):
      nh = b[0] + dh[i]
      nw = b[1] + dw[i]
      if nh >=0 and nh < h and nw >=0 and nw < w:
        s[nh][nw] = '#'

cnt = 0
while  find_w():
  rev()
  cnt += 1

print(cnt)

Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w = list(map(int,input().split()))
s = [[x for x in input()] for _ in range(h)]

def find_w():
  for i in range(h):
    for j in range(w):
      if s[i][j] == '.':
        return True

def find_b():
  b = deque([])
  for i in range(h):
    for j in range(w):
      if s[i][j] == '#':
        b.append([i,j])
  return b

d = [[float('inf') for _ in range(w)] for _ in range(h)]
def bfs():
  que = deque([])
  que.extend(find_b())
  dh = [1,0,-1,0]
  dw = [0,1,0,-1]
  while que:
    p = que.pop()
    for i in range(4):
      nh = p[0] + dh[i]
      nw = p[1] + dw[i]
      
      if nh >= 0 and nh < h and nw >= 0 and nw < w and \
          s[nh][nw] == '.' and d[nh][nw] == float('inf'):
        s[nh][nw] = '#'

cnt = 0
while find_w():
  bfs()
  cnt += 1

print(cnt)


Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h,w = list(map(int,input().split()))
s = [[x for x in input()] for _ in range(h)]

def find_w():
  for i in range(h):
    for j in range(w):
      if s[i][j] == '.':
        return True

def find_b():
  b = deque([])
  for i in range(h):
    for j in range(w):
      if s[i][j] == '#':
        b.append([i,j])
  return b

d = [[float('inf') for _ in range(w)] for _ in range(h)]
def bfs():
  que = deque([])
  que.extend(find_b())
  dh = [1,0,-1,0]
  dw = [0,1,0,-1]
  while que:
    p = que.popleft()
    for i in range(4):
      nh = p[0] + dh[i]
      nw = p[1] + dw[i]
      
      if nh >= 0 and nh < h and nw >= 0 and nw < w and \
          s[nh][nw] == '.' and d[nh][nw] == float('inf'):
        s[nh][nw] = '#'

cnt = 0
while find_w():
  bfs()
  cnt += 1

print(cnt)


Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

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

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

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

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)

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

h,w = list(map(int,input().split()))
par = [[x for x in input()] for _ in range(h)]
que = deque([])
for i in range(h):
  for j in range(w):
    if par[i][j] == '#':
      que.append([i,j])

m = par.copy()

dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(que):
  next_que = deque([])
  while que:
    cur = que.popleft()
    
    for i in range(4):
      nh = cur[0] + dh[i]
      nw = cur[1] + dw[i]
      
      if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
        m[nh][nw] = '#'
        next_que.append([nh,nw])
        
  return next_que


cnt = 0
while que:
  cnt += 1
  que = bfs(que)

print(cnt-1)

Fixed submission:
from collections import deque

h,w = list(map(int,input().split()))
m = [[x for x in input()] for _ in range(h)]
next_que = deque([])
for i in range(h):
  for j in range(w):
    if m[i][j] == '#':
      next_que.append([i,j])


dh = [1,0,-1,0]
dw = [0,1,0,-1]

def bfs(next_que):
  cnt = -1
  while next_que:
    cnt += 1
    que = list(next_que)
    next_que = deque([])
    for cur in que:
      #cur = que.popleft()
      for i in range(4):
        nh = cur[0] + dh[i]
        nw = cur[1] + dw[i]
        
        if nh >= 0 and nh < h and nw >= 0 and nw < w and m[nh][nw] == '.':
          m[nh][nw] = '#'
          next_que.append([nh,nw])
  
  return cnt



cnt = bfs(next_que) 

print(cnt)