Buggy python submission (verdict: Runtime Error):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
cnt = [[float('inf')]*w for _ in range(h)]
moves = ((0, 1), (0, -1), (1, 0), (-1, 0))
q = deque([])
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0 
 
def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and a[ny][nx] == '.' and used[ny][nx] == 0:
        used[ny][nx] = 1
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1)
        q.append([ny, nx])
 
used = [[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      used[i][j] = 1
      q.append([i, j])
 
bfs()
 
ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])
print(ans)
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
cnt = [[float('inf')]*w for _ in range(h)]
moves = ((0, 1), (0, -1), (1, 0), (-1, 0))
q = deque([])
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0 

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and a[ny][nx] == '.' and used[ny][nx] == 0:
        used[ny][nx] = 1
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1)
        q.append([ny, nx])

used = [[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      used[i][j] = 1
      q.append([i, j])

bfs()

ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])
print(ans)


Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dist = [[float('inf')]*w for _ in range(h)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0))
visited = [[0]*w for _ in range(h)]

def bfs(li):
  q = deque(li)
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy 
      nx = x + dx 
      if 0<=ny<h and 0<=nx<w and a[ny][nx]=='.' and visited[ny][nx]==0 and dist[ny][nx]>dist[y][x]+1:
        dist[ny][nx] = dist[y][x] + 1
        visited[ny][nx] = 1
        q += [[ny, nx]]

li = []
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      li += [[i, j]]
      dist[i][j] = 0
      visited[i][j] = 1

bfs(li)

ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, dist[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dist = [[float('inf')]*w for _ in range(h)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0))

def bfs(li):
  q = deque(li)
  visited = [[0]*w for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy 
      nx = x + dx 
      if 0<=ny<h and 0<=nx<w and a[ny][nx]=='.' and visited[ny][nx]==0 and dist[ny][nx]>dist[y][x]+1:
        dist[ny][nx] = dist[y][x] + 1
        visited[ny][nx] = 1
        q += [[ny, nx]]

li = []
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      li += [[i, j]]
      dist[i][j] = 0

bfs(li)

ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, dist[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dist = [[float('inf')]*w for _ in range(h)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0))

def bfs(sy, sx):
  q = deque([[sy, sx]])
  dist[sy][sx] = 0 
  visited = [[0]*w for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy 
      nx = x + dx 
      if 0<=ny<h and 0<=nx<w and a[ny][nx]=='.' and visited[ny][nx]==0 and dist[ny][nx]>dist[y][x]+1:
        dist[ny][nx] = dist[y][x] + 1
        visited[ny][nx] = 1
        q += [[ny, nx]]


for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      bfs(i,j)

ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, dist[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
dist = [[float('inf')]*w for _ in range(h)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0))

def bfs(sy, sx):
  q = deque([[sy, sx]])
  dist[sy][sx] = 0 
  visited = [[0]*w for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy 
      nx = x + dx 
      if 0<=ny<h and 0<=nx<w and a[ny][nx]=='.' and visited[ny][nx]==0 and dist[ny][nx]>dist[y][x]+1:
        dist[ny][nx] = dist[y][x] + 1
        visited[ny][nx] = 1
        q += [[ny, nx]]


for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      bfs(i,j)

ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, dist[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
cnt = [[float('inf')]*w for _ in range(h)]
moves = ((0, 1), (0, -1), (1, 0), (-1, 0))

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0 

def bfs(sy, sx):
  q = deque([[sy, sx]])
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and a[ny][nx] == '.' and used[ny][nx] == 0:
        used[ny][nx] = 1
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1)
        q.append([ny, nx])


for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      used = [[0]*w for _ in range(h)]
      bfs(i, j)

ans = 0
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])
print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
sys.setrecursionlimit(1000000)
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
cnt = [[float('inf')]*w for _ in range(h)]
moves = ((0, 1), (0, -1), (1, 0), (-1, 0))

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0 

def bfs(sy, sx):
  q = deque([[sy, sx]])
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and a[ny][nx] == '.' and used[ny][nx] == 0:
        used[ny][nx] = 1
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1)
        q.append([ny, nx])

import numpy as np
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      used = [[0]*w for _ in range(h)]
      bfs(i, j)

print(np.max(np.array(cnt)))

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
cnt = [[float('inf')]*w for _ in range(h)]
moves = ((0, 1), (0, -1), (1, 0), (-1, 0))

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0 

def bfs(sy, sx):
  q = deque([[sy, sx]])
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      ny = y + dy
      nx = x + dx 
      if 0 <= ny < h and 0 <= nx < w and a[ny][nx] == '.' and used[ny][nx] == 0:
        used[ny][nx] = 1
        cnt[ny][nx] = min(cnt[ny][nx], cnt[y][x]+1)
        q.append([ny, nx])

import numpy as np
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      used = [[0]*w for _ in range(h)]
      bfs(i, j)

print(np.max(np.array(cnt)))

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
start = []
q = deque([])

def bfs():
  searched = [[0 for _ in range(w)] for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      if 0 <= y+dy < h and 0 <= x+dx < w and searched[y+dy][x+dx] == 0 and a[y+dy][x+dx] == '.' and cnt[y+dy][x+dx] > cnt[y][x]+1:
        searched[y+dy][x+dx] = 1
        q.append([y+dy, x+dx])
        cnt[y+dy][x+dx] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      start.append([i, j])
      cnt[i][j] = 0
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
start = []
q = deque([])

def bfs():
  #q = deque([(y, x)])
  searched = [[0 for _ in range(w)] for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      if 0 <= y+dy < h and 0 <= x+dx < w and searched[y+dy][x+dx] == 0 and a[y+dy][x+dx] == '.' and cnt[y+dy][x+dx] > cnt[y][x]+1:
        searched[y+dy][x+dx] = 1
        q.append([y+dy, x+dx])
        cnt[y+dy][x+dx] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      start.append([i, j])
      cnt[i][j] = 0
      q.append([i, j])

#for i, j in start:
  #bfs(i, j)
bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
start = []

def bfs(y, x):
  q = deque([(y, x)])
  searched = [[0 for _ in range(w)] for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      if 0 <= y+dy < h and 0 <= x+dx < w and searched[y+dy][x+dx] == 0 and a[y+dy][x+dx] == '.' and cnt[y+dy][x+dx] > cnt[y][x]+1:
        searched[y+dy][x+dx] = 1
        q.append([y+dy, x+dx])
        cnt[y+dy][x+dx] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      start.append([i, j])
      cnt[i][j] = 0
      
for i, j in start:
  bfs(i, j)

for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
start = []

def bfs(y, x):
  q = deque([(y, x)])
  searched = [[0 for _ in range(w)] for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      if 0 <= y+dy < h and 0 <= x+dx < w and searched[y+dy][x+dx] == 0 and a[y+dy][x+dx] == '.' and cnt[y+dy][x+dx] > cnt[y][x]+1:
        searched[y+dy][x+dx] = 1
        q.append([y+dy, x+dx])
        cnt[y+dy][x+dx] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      start.append([i, j])
      cnt[i][j] = 0
      
for i, j in start:
  bfs(i, j)

for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
start = []

def bfs(y, x):
  q = deque([(y, x)])
  searched = [[0 for _ in range(w)] for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      if 0 <= y+dy < h and 0 <= x+dx < w and searched[y+dy][x+dx] == 0 and a[y+dy][x+dx] == '.':
        searched[y+dy][x+dx] = 1
        q.append([y+dy, x+dx])
        cnt[y+dy][x+dx] = min(cnt[y+dy][x+dx], cnt[y][x]+1)

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      start.append([i, j])
      cnt[i][j] = 0
      
for i, j in start:
  bfs(i, j)

for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])


print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0

def bfs(y, x):
  q = deque([(y, x)])
  searched = [[0 for _ in range(w)] for _ in range(h)]
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      if 0 <= y+dy < h and 0 <= x+dx < w and searched[y+dy][x+dx] == 0 and a[y+dy][x+dx] == '.':
        #a[y+dy][x+dx] = '#'
        searched[y+dy][x+dx] = 1
        q.append([y+dy, x+dx])
        cnt[y+dy][x+dx] = min(cnt[y+dy][x+dx], cnt[y][x]+1)

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      
for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      bfs(i, j)

for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]

black_list = []
used = []

for i in range(h):
  for j in range(w):
    if a[i][j] == ".":
      a[i][j] = 1
    else:
      a[i][j] = 0
      black_list.append([i, j])
a = np.array(a)

def search(a, h, w):
  a = np.array(a)
  b = np.array(a[:]).T
  for i in range(w):
    for j in range(1, h-1):
      if a[i][j] == 0:
        a[i][j-1] = 0
        a[i][j+1] = 0

  for i in range(h):
    for j in range(1, w-1):
      if b[i][j] == 0:
        b[i][j-1] = 0
        b[i][j+1] = 0
  return a*b.T

count = 0
while np.sum(a) > 0:
  a = search(a, h, w)
  count += 1

print(count)





Fixed submission:
from collections import deque
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
cnt = [[float('inf') for _ in range(w)] for _ in range(h)]
ans = 0
searched = [[0 for _ in range(w)] for _ in range(h)]
q = deque([])

def bfs():
  while q:
    y, x = q.popleft()
    for dy, dx in moves:
      next_y = y + dy
      next_x = x + dx 
      if 0 <= next_y < h and 0 <= next_x < w and searched[next_y][next_x] == 0 and a[next_y][next_x] == '.' and cnt[next_y][next_x] > cnt[y][x]+1:
        searched[next_y][next_x] = 1
        q.append([next_y, next_x])
        cnt[next_y][next_x] = cnt[y][x]+1

for i in range(h):
  for j in range(w):
    if a[i][j] == '#':
      cnt[i][j] = 0
      searched[i][j] = 1
      q.append([i, j])

bfs()
for i in range(h):
  for j in range(w):
    ans = max(ans, cnt[i][j])

print(ans)