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

b_y = deque()
b_x = deque()
for i in range(h):
  for j in range(w):
    if a[i][j] == "#":
      b_y.append(i)
      b_x.append(j)
      a[i][j] = 0

d = [[1,0], [-1, 0], [0,-1], [0,1]]
while b_y:
  for y,x in d:
    if 0 <= b_y[0]+y <= h-1 and 0 <= b_x[0]+x <= w-1 and a[b_y[0]+y][b_x[0]+x] == ".":
      a[b_y[0]+y][b_x[0]+x] = a[b_y[0]][b_x[0]]+1
      b_y.append(b_y[0]+y)
      b_x.append(b_x[0]+x)
  b_y.popleft()
  b_x.popleft()

ans = 0
for v in a:
  if max(v) > ans: ans = max(v)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

H,W = map(int,input().split())
INF = 10**18
q = []

visited = [[True]*(W+2)]
visited += [[True] + [x == '#' for x in input().rstrip()] + [True] for _ in range(H)]
visited.append([True]*(W+2))
for i in range(1,H+1):
  for j in range(1,W+1):
    if visited[i][j]:
      q.append((i,j))

moves = [(0,1),(0,-1),(1,0),(-1,0)] #全方向調べる

ans = -1
while q:
  qq = []
  ans += 1
  for x,y in q:
    if not visited[x-1][y]:
      visited[x-1][y] = True
      qq.append((x-1,y))
    if not visited[x+1][y]:
      visited[x+1][y] = True
      qq.append((x+1,y))
    if not visited[x][y-1]:
      visited[x][y-1] = True
      qq.append((x,y-1))
    if not visited[x][y+1]:
      visited[x][y+1] = True
      qq.append((x,y+1))
  q = qq
  
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)]

b_y = deque()
b_x = deque()
for i in range(h):
  for j in range(w):
    if a[i][j] == "#":
      b_y.append(i)
      b_x.append(j)
      a[i][j] = 0

d = [[1,0], [-1, 0], [0,-1], [0,1]]
while b_y:
  for y,x in d:
    if 0 <= b_y[0]+y <= h-1 and 0 <= b_x[0]+x <= w-1 and a[b_y[0]+y][b_x[0]+x] == ".":
      a[b_y[0]+y][b_x[0]+x] = a[b_y[0]][b_x[0]]+1
      b_y.append(b_y[0]+y)
      b_x.append(b_x[0]+x)
  b_y.popleft()
  b_x.popleft()

ans = 0
for v in a:
  if max(v) > ans: ans = max(v)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

H,W = map(int,input().split())
INF = 10**18
q = []

visited = [[True]*(W+2)]
visited += [[True] + [x == '#' for x in input().rstrip()] + [True] for _ in range(H)]
visited.append([True]*(W+2))
for i in range(1,H+1):
  for j in range(1,W+1):
    if visited[i][j]:
      q.append((i,j))

moves = [(0,1),(0,-1),(1,0),(-1,0)] #全方向調べる

ans = -1
while q:
  qq = []
  ans += 1
  for x,y in q:
    if not visited[x-1][y]:
      visited[x-1][y] = True
      qq.append((x-1,y))
    if not visited[x+1][y]:
      visited[x+1][y] = True
      qq.append((x+1,y))
    if not visited[x][y-1]:
      visited[x][y-1] = True
      qq.append((x,y-1))
    if not visited[x][y+1]:
      visited[x][y+1] = True
      qq.append((x,y+1))
  q = qq
  
print(ans)

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

H, W = map(int, input().split())
grid = [input() for i in range(H)]

# 初期の黒('#')の位置からの距離
dist = [[-1]*W for _ in range(H)]

# '#'の位置をキューに追加（＝今回はこれが複数あるので、複数スタートとなる）
black_cells = deque()
for h in range(H):
    for w in range(W):
        if grid[h][w] == '#':
            black_cells.append((h, w))
            dist[h][w] = 0

def bfs(black_cells, dist):
    """
    BFSを用いて、全マス目を黒くする回数を求める

    Args:
        black_cells: 「開始時の黒マス」の座標が入ったキュー（複数座標あり）
        dist: 「開始時の黒マス」からの距離の入ったリスト  [[-1, -1, 0], ...]
        　　　（白マスの初期値は0）

    Returns:
        g: 操作を繰り返した数
    """
    d = 0
    while black_cells:
        # キューとして使うのでpop()ではなくpopleft()
        h, w = black_cells.popleft()
        d = dist[h][w]
        for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            new_h = h + dy
            new_w = w + dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:  # セルが白('.')であるのと同義
                dist[new_h][new_w] = d+1
                black_cells.append((new_h, new_w))
    return d

d = bfs(black_cells, dist)
print(d)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

H,W = map(int,input().split())
INF = 10**18
q = []

visited = [[True]*(W+2)]
visited += [[True] + [x == '#' for x in input().rstrip()] + [True] for _ in range(H)]
visited.append([True]*(W+2))
for i in range(1,H+1):
  for j in range(1,W+1):
    if visited[i][j]:
      q.append((i,j))

moves = [(0,1),(0,-1),(1,0),(-1,0)] #全方向調べる

ans = -1
while q:
  qq = []
  ans += 1
  for x,y in q:
    if not visited[x-1][y]:
      visited[x-1][y] = True
      qq.append((x-1,y))
    if not visited[x+1][y]:
      visited[x+1][y] = True
      qq.append((x+1,y))
    if not visited[x][y-1]:
      visited[x][y-1] = True
      qq.append((x,y-1))
    if not visited[x][y+1]:
      visited[x][y+1] = True
      qq.append((x,y+1))
  q = qq
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
a = [input() for _ in range(H)]

#初期の黒("#")の位置からの距離
dist = [[-1] * W for _ in range(H)]

# "#"の位置をキューに追加
black = deque()
for h in range(H):
    for w in range(W):
        if a[h][w] == "#":
            black.append((h,w))
            dist[h][w] = 0
def bfs(black, dist):
    d = 0
    while black: #blackに中身が入っている間
        #キューとして使うから左から出すpopleft()
        h, w = black.popleft()
        d = dist[h][w]
        for dy,dx in((1,0), (0,1), (-1, 0), (0,-1)):
            new_h = h + dy
            new_w = w + dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w] = d+1
                black.append((new_h, new_w))
    return d

d = bfs(black, dist)
print(d)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

H,W = map(int,input().split())
INF = 10**18
q = []

visited = [[True]*(W+2)]
visited += [[True] + [x == '#' for x in input().rstrip()] + [True] for _ in range(H)]
visited.append([True]*(W+2))
for i in range(1,H+1):
  for j in range(1,W+1):
    if visited[i][j]:
      q.append((i,j))

moves = [(0,1),(0,-1),(1,0),(-1,0)] #全方向調べる

ans = -1
while q:
  qq = []
  ans += 1
  for x,y in q:
    if not visited[x-1][y]:
      visited[x-1][y] = True
      qq.append((x-1,y))
    if not visited[x+1][y]:
      visited[x+1][y] = True
      qq.append((x+1,y))
    if not visited[x][y-1]:
      visited[x][y-1] = True
      qq.append((x,y-1))
    if not visited[x][y+1]:
      visited[x][y+1] = True
      qq.append((x,y+1))
  q = qq
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
a = [input() for _ in range(H)]

#初期の黒("#")の位置からの距離
dist = [[-1] * W for _ in range(H)]

# "#"の位置をキューに追加
black = deque()
for h in range(H):
    for w in range(W):
        if a[h][w] == "#":
            black.appendleft((h,w))
            dist[h][w] = 0
def bfs(black, dist):
    d = 0
    while black: #blackに中身が入っている間
        h, w = black.popleft()
        d = dist[h][w]
        for dy,dx in((1,0), (0,1), (-1, 0), (0,-1)):
            new_h = h + dy
            new_w = w + dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w] = d+1
                black.append((new_h, new_w))
    return d

d = bfs(black, dist)
print(d)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

H,W = map(int,input().split())
INF = 10**18
q = []

visited = [[True]*(W+2)]
visited += [[True] + [x == '#' for x in input().rstrip()] + [True] for _ in range(H)]
visited.append([True]*(W+2))
for i in range(1,H+1):
  for j in range(1,W+1):
    if visited[i][j]:
      q.append((i,j))

moves = [(0,1),(0,-1),(1,0),(-1,0)] #全方向調べる

ans = -1
while q:
  qq = []
  ans += 1
  for x,y in q:
    if not visited[x-1][y]:
      visited[x-1][y] = True
      qq.append((x-1,y))
    if not visited[x+1][y]:
      visited[x+1][y] = True
      qq.append((x+1,y))
    if not visited[x][y-1]:
      visited[x][y-1] = True
      qq.append((x,y-1))
    if not visited[x][y+1]:
      visited[x][y+1] = True
      qq.append((x,y+1))
  q = qq
  
print(ans)