Problem:
 Score : 300 points    Problem Statement You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black. HW characters from A_{11} to A_{HW} represent the colors of the squares. A_{ij} is # if the square at the i -th row from the top and the j -th column from the left is black, and A_{ij} is . if that square is white.  We will repeatedly perform the following operation until all the squares are black:   Every white square that shares a side with a black square, becomes black.   Find the number of operations that will be performed.
The initial grid has at least one black square.      Constraints  1 \leq H,W \leq 1000  A_{ij} is # or . .  The given grid has at least one black square.         Input Input is given from Standard Input in the following format:  H  W  A_{11} A_{12} ... A_{1W}  :  A_{H1} A_{H2} ... A_{HW}       Output Print the number of operations that will be performed.        Sample Input 1 3 3
...
.#.
...      Sample Output 1 2  After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.       Sample Input 2 6 6
..#..#
......
#..#..
......
.#....
....#.      Sample Output 2 3

Solution (Python):
H,W = list(map(int,input().split()))
A = [list(str(input())) for i in range(H)]
 
black = []
 
cnt = 0
 
for i in range(H):
    for j in range(W):
        if A[i][j] == "
            black.append([i,j])
 
length = len(black)
            
while (length < H*W):
    gray = []
    for l in black:
        if l[1] != 0:
            c1 = (l[0],l[1]-1)
            if A[c1[0]][c1[1]] == ".":
                gray.append(c1)
                A[c1[0]][c1[1]] = "
        if l[1] != W-1:
            c2 = (l[0],l[1]+1)
            if  A[c2[0]][c2[1]] == ".":
                gray.append(c2)
                A[c2[0]][c2[1]] = "
        if l[0] != 0:
            c3 = (l[0]-1,l[1])
            if  A[c3[0]][c3[1]] == ".":
                gray.append(c3)
                A[c3[0]][c3[1]] = "
        if l[0] != H-1:
            c4 = (l[0]+1,l[1])
            if  A[c4[0]][c4[1]] == ".":
                gray.append(c4)
                A[c4[0]][c4[1]] = "
    
    gray = list(set(gray))
    black = [l for l in gray]
    
    cnt += 1
    length += len(black)
    
print(cnt)

Solution (Python):
import sys
H, W, *A = sys.stdin.read().split()
H = int(H); W = int(W)

rW = range(W)
rrW = range(W-1, -1, -1)
rH = range(H)

INF = 4000
dist = [[INF]*W for i in rH]
S = []
push = S.append
for di, Ai in zip(dist, A):
    chg = 0
    for j, a in enumerate(Ai):
        if a == '
            di[j] = 0
            chg = 1
    if chg:
        push(di)
for di in S:
    cur = INF
    for j in rW:
        if not di[j]:
            cur = -j
        else:
            di[j] = cur+j
    cur = INF
    for j in rrW:
        if not di[j]:
            cur = j
        elif cur-j < di[j]:
            di[j] = cur-j
for j in rW:
    cur = INF
    for di in dist:
        if cur+1 < di[j]:
            di[j] = cur = cur+1
        else:
            cur = di[j]
    cur = INF
    for di in reversed(dist):
        if cur+1 < di[j]:
            di[j] = cur = cur+1
        else:
            cur = di[j]
sys.stdout.write("%d\n" % max(max(di) for di in dist))

Solution (Python):
from collections import deque

drc = ((0, 1), (0, -1), (1, 0), (-1, 0))

H, W = map(int, input().split())
a = tuple(input() for _ in range(H))
visited = [[False] * W for _ in range(H)]


q = deque()
for r in range(H):
    for c in range(W):
        if a[r][c] == '
            q.append((r, c, 0))
            visited[r][c] = True
            
            
            
            

while q:
    r, c, d = q.popleft()
    for dr, dc in drc:
        nr = r + dr
        if not (0 <= nr < H): continue
        nc = c + dc
        if not (0 <= nc < W): continue
        
        if visited[nr][nc]: continue
        
        
        q.append((nr, nc, d + 1))
        visited[nr][nc] = True
        

print(d)

Solution (Python):

import sys
import numpy as np


def solve(H: int, W: int, A: "List[str]"):
    dist = np.full((H, W), np.inf)
    for h in range(H):
        for w in range(W):           
            if A[h][w] == '
                dist[h][w] = 0

    for h in range(H-1):
        dist[h+1, :] = np.minimum(dist[h, :] + 1, dist[h+1, :])
    for w in range(W-1):
        dist[:, w+1] = np.minimum(dist[:, w] + 1, dist[:, w+1])
    for h in range(H-1, 0, -1):
        dist[h-1, :] = np.minimum(dist[h, :] + 1, dist[h-1, :])
    for w in range(W-1, 0, -1):
        dist[:, w-1] = np.minimum(dist[:, w] + 1, dist[:, w-1])

    print(int(np.max(dist)))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    A = [ next(tokens) for _ in range(H) ]  
    solve(H, W, A)

if __name__ == '__main__':
    main()

Solution (Python):
from scipy.ndimage import*
print(maximum(morphology.distance_transform_cdt([[t>'

Solution (Python):
h, w = map(int, input().split())

grid = [[True] * (w + 2)]
for _ in range(h):
    grid.append([True] + [x == '
grid.append([True] * (w + 2))

q = []

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if grid[i][j] is True:
            q.append((i, j))

cnt = 0
while q:
    qq = []
    for i, j in q:
        if not grid[i + 1][j]:
            grid[i + 1][j] = True
            qq.append((i + 1, j))
        if not grid[i - 1][j]:
            grid[i - 1][j] = True
            qq.append((i - 1, j))
        if not grid[i][j + 1]:
            grid[i][j + 1] = True
            qq.append((i, j + 1))
        if not grid[i][j - 1]:
            grid[i][j - 1] = True
            qq.append((i, j - 1))

    cnt += 1
    q = qq

print(cnt - 1)

Solution (Python):


import numpy

H, W = map(int, input().split())
values = [None] * H

for y in range(H):
    values[y] = [0 if v == "
values = numpy.array(values)

for x in range(W - 1):
    values[:, x + 1] = numpy.minimum(values[:, x] + 1, values[:, x + 1])

for x in range(W - 1, 0, -1):
    values[:, x - 1] = numpy.minimum(values[:, x] + 1, values[:, x - 1])

for y in range(H - 1):
    values[y + 1, :] = numpy.minimum(values[y, :] + 1, values[y + 1, :])

for y in range(H - 1, 0, -1):
    values[y - 1, :] = numpy.minimum(values[y, :] + 1, values[y - 1, :])

print(numpy.max(values))

Solution (Python):
H,W = map(int, input().split())
B = [list(input()) for i in range(H)]

l = []

for i in range(H):
    for j in range(W):
        if B[i][j] == '
            l.append((i,j))

def a(l):
    n = []
    d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    for y, x in l:
        for dy, dx in d:
            ny = y + dy
            nx = x + dx
            if 0 <= ny < H and 0 <= nx < W and B[ny][nx] == '.':
                B[ny][nx] = '
                n.append((ny, nx))
    return n

ans = 0
while len(l) != 0:
    l = a(l)
    ans += 1
print(ans-1)

Solution (Python):
from collections import deque

H, W = map(int,input().split())
coords = []
blacks = deque([])
for i in range(H):
    rows = list(input())
    coords.append(rows)
    for j,row in enumerate(rows):
        if row == "
            blacks.append((i,j,0))

ans = 0
while len(blacks) > 0: 
    h,w,dist = blacks.popleft()
    ans = max(dist,ans)
    for di, dj in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        ni = h + di
        nj = w + dj
        if 0 <= ni < H and 0 <= nj < W:
            if coords[ni][nj] == ".":
                blacks.append((ni,nj,dist+1))
                coords[ni][nj] = "

print(ans)

Solution (Python):
from time import time
from collections import deque





def append(d, i, j):
    if not 0 <= i < H or not 0 <= j < W or dists[i][j] != -1:
        return
    dists[i][j] = d
    q.append((d, i, j))


q = deque()
H, W = map(int, input().split())
dists = [[-1] * W for _ in range(H)]
for i in range(H):
    for j, v in enumerate(input()):
        if v == "
            append(0, i, j)

while len(q):
    d, i, j = q.popleft()
    append(d+1, i-1, j)
    append(d+1, i+1, j)
    append(d+1, i, j-1)
    append(d+1, i, j+1)


ans = max(map(max, dists))



print(ans)

Solution (Python):


import sys
from collections import deque

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()

que = deque()

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

grid = list2d(H+2, W+2, '*')
for i in range(1,H+1):
    row = list(input())
    for j in range(1, W+1):
        grid[i][j] = row[j-1]
        if grid[i][j] == '
            que.append((i, j, 0))


memo=[[-1]*(W+2) for i in range(H+2)]
while len(que):
    cur=que.popleft()
    if memo[cur[0]][cur[1]]!=-1:
        continue
    memo[cur[0]][cur[1]]=cur[2]
    
    for direction in directions:
        x=cur[0]+direction[0]
        y=cur[1]+direction[1]
        
        if grid[x][y]=='*' or grid[x][y]=='
            continue
        
        if grid[x][y]=='.':
            grid[x][y]='
            que.append((x, y, cur[2]+1))
ans=0
for i in range(1, H+1):
        ans=max(ans, max(memo[i]))
print(ans)

Solution (Python):

import sys

inf = float('inf')

def solve(H: int, W: int, A: "List[str]"):
    dist = [[inf] * W for h in range(H)]
    for h in range(H):
        for w in range(W):
            hh = max(h-1, 0)
            ww = max(w-1, 0)            
            if A[h][w] == '
                dist[h][w] = 0
            else:
                dist[h][w] = min(dist[hh][w], dist[h][ww]) + 1
    
    dmax = 0
    for h in range(H-1, -1, -1):
        for w in range(W-1, -1, -1):
            hh = min(h+1, H-1)
            ww = min(w+1, W-1)
            dist[h][w] = min(dist[h][w], dist[h][ww] + 1, dist[hh][w] + 1)
            dmax = max(dist[h][w], dmax)
    print(dmax) 
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    A = [ next(tokens) for _ in range(H) ]  
    solve(H, W, A)

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq as hq
import itertools as its
import operator as op



inf = float('inf')
H, W = 0, 0
A = []


def set_inputs():
    global H, W, A
    H, W = get_li()
    A = [get_str() for _ in range(H)]
    return


def main():
    set_inputs()
    
    stage = [[0 for _ in range(W)] for _ in range(H)]
    remaining = deque()
    for i, ai_ in enumerate(A):
        for j, a in enumerate(ai_):
            if a == ".":
                continue
            stage[i][j] = 1
            remaining.append((i, j))
    ans = 0
    while remaining:
        _next = deque()
        for (i, j) in remaining:
            stage[i][j] = 2
            if i > 0 and stage[i-1][j] == 0:
                stage[i-1][j] = 1
                _next.append((i-1, j))
            if i < H - 1 and stage[i+1][j] == 0:
                stage[i+1][j] = 1
                _next.append((i+1, j))
            if j > 0 and stage[i][j-1] == 0:
                stage[i][j-1] = 1
                _next.append((i, j-1))
            if j < W - 1 and stage[i][j+1] == 0:
                stage[i][j+1] = 1
                _next.append((i, j+1))
        remaining = _next
        ans += 1
    if ans == 0:
        print(0)
    else:
        print(ans - 1)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
H, W = map(int, input().split())
hw = [list(input().strip()) for _ in range(H)]
b = deque()
for h in range(H):
    for w in range(W):
        if hw[h][w] == '
            b.append([h, w, 0])

ans = 0
while len(b) > 0:
    h, w, d = b.popleft()
    d += 1
    for y, x in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
        nh, nw = h + y, w + x
        if nh >= 0 and nh < H and nw >= 0 and nw < W and hw[nh][nw] == '.':
            b.append([nh, nw, d])
            if ans < d:
                ans = d
            hw[nh][nw] = "
print(ans)

Solution (Python):
inputs = raw_input().replace("\n", "").split(" ")
H, W = map(int, inputs)

m = [ [-1 for x in range(W)] for y in range(H)]

count = 0
zero_coords = []
for y in range(H):
    
    line = list(raw_input().replace("\n", ""))
    for x, point in enumerate(line):
        if point == "
            m[y][x] = 0
            zero_coords.append([y, x])
            count += 1

tmp_coords = zero_coords

mawari_soutais = [[-1, 0], [0, -1], [0, 1], [1, 0]]

val = 0
while count < H*W:
    val += 1
    new_tmp_coords = []
    for center_coord in tmp_coords:
        for mawari_soutai in mawari_soutais:
            mawari_coord = [mawari_soutai[i] + center_coord[i] for i in range(2)]
            y, x = mawari_coord
            if y < 0 or H <= y or x < 0 or W <= x or m[y][x] != -1:
                continue
            else:
                m[y][x] = val
                count += 1
                new_tmp_coords.append([y, x])
    tmp_coords = new_tmp_coords
    

out = val
print(out)

Solution (Python):
from collections import deque

H,W = map(int,input().split()) 
A = [[0]*W for _ in range(H)]
M = 0
queue = deque()
for i in range(H):
  s = input()
  for j in range(W):
    if s[j]== '
      queue.append([i,j,0])
      A[i][j] = '
while queue:
  i,j,cost = queue.popleft()
  M=max(M,cost)
  if i+1 < H and A[i+1][j] != '
    queue.append([i+1,j,cost+1])
    A[i+1][j] = '
  if i-1 >= 0 and A[i-1][j] != '
    queue.append([i-1,j,cost+1])
    A[i-1][j] = '
  if j+1 < W and A[i][j+1] != '
    queue.append([i,j+1,cost+1])
    A[i][j+1] = '
  if j-1 >= 0 and A[i][j-1] != '
    queue.append([i,j-1,cost+1])
    A[i][j-1] = '
print(M)

Solution (Python):
from collections import deque
from itertools import chain
H, W = map(int, input().split())
G = chain.from_iterable([[s == '
dist = [0]*(H*W)
B = deque([i for i, g in enumerate(G) if g])
visited = set(B)
while B:
    vij = B.pop()
    for dij in (W, -W, 1, -1):
        nij = vij + dij
        if (dij == 1 and not nij % W) or (dij == -1 and not (nij+1) % W) or not (0 <= nij < H*W) or nij in visited:
            continue
        visited.add(nij)
        dist[nij] = 1 + dist[vij]
        B.appendleft(nij)
print(max(dist))

Solution (Python):
from collections import deque
h, w = map(int, input().split())
a = []
for i in range(h):
    a.append(str(input()))
que = deque()
dist = [[-1]*w for i in range(h)]

for i in range(h):
    for j in range(w):
        if a[i][j] == "
            dist[i][j] = 0
            que.append((i, j))


dd = ((0, -1), (-1, 0), (0, 1), (1, 0))
ans = 0
d = 0
while que:
    y, x = que.popleft() 
    d = dist[y][x]
    for dx, dy in dd:
        nx = x + dx; ny = y + dy
        if 0 <= nx < w and 0 <= ny < h and dist[ny][nx] == -1:
            dist[ny][nx] = 0
            que.append((ny, nx))
            dist[ny][nx] = d + 1
print(d)

Solution (Python):

import sys

inf = float('inf')

def solve(H: int, W: int, A: "List[str]"):
    dist = [[inf] * W for h in range(H)]
    for h in range(1, H):
        for w in range(1, W):
            if A[h][w] == '
                dist[h][w] = 0
            else:
                dist[h][w] = min(dist[h-1][w], dist[h][w-1]) + 1
    
    dmax = 0
    for h in range(H-1, -1, -1):
        for w in range(W-1, -1, -1):
            if A[h][w] == '
                dist[h][w] = 0
            elif h+1 <= H-1 and w+1 <= W-1:
                dist[h][w] = min(dist[h][w], min(dist[h+1][w], dist[h][w+1]) + 1)
            elif w+1 == W and h+1 <= H-1:
                dist[h][w] = min(dist[h][w], dist[h+1][w] + 1)
            elif h+1 == H and w+1 <= W-1:
                dist[h][w] = min(dist[h][w], dist[h][w+1] + 1)
            dmax = max(dist[h][w], dmax)
    print(dmax) 
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  
    W = int(next(tokens))  
    A = [ next(tokens) for _ in range(H) ]  
    solve(H, W, A)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
blacks = deque([])
for h in range(H):
  for w in range(W):
    if A[h][w] == "
      blacks.append((h, w))
count = 0
while len(blacks) > 0:
  newblacks = deque([])
  while len(blacks) > 0:
    b = blacks.popleft()
    for y, x in zip((b[0]-1, b[0], b[0], b[0]+1), (b[1], b[1]-1, b[1]+1, b[1])):
      if y < 0 or y >= H or x < 0 or x >= W:
        continue
      if A[y][x] == ".":
        newblacks.append((y, x))
      A[y][x] = "
  blacks = newblacks
  count += 1
print(count-1)

Solution (Python):
from collections import deque

H, W = [int(i) for i in input().split()]
A = [list(input()) for _ in range(H)]

Q = deque()
for i in range(H):
    for j in range(W):
        if A[i][j] == "
            Q.append((i, j, 0))

ma = 0
while Q:
    i, j, dist = Q.popleft()
    ma = max(ma, dist)
    for ni, nj in [(i + 1, j), (i, j + 1), (i - 1, j), (i, j - 1)]:
        if 0 <= ni < H and 0 <= nj < W and A[ni][nj] == ".":
            A[ni][nj] = "
            Q.append((ni, nj, dist + 1))

print(ma)

Solution (Python):
import sys

input = sys.stdin.readline

from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

Solution (Python):
from collections import deque

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

bl = [[False] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '
            bl[r][c] = True
            dq.append((r, c, 0))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

while dq:
    r, c, d = dq.popleft()
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if bl[nr][nc]: continue
        bl[nr][nc] = True
        dq.append((nr, nc, d + 1))

print(d)

Solution (Python):


import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

import datetime

def main():
    H, W = i2nn()
    HW = H * W
    A = ii2ss(H)
    A = [[0 if s == '
    for i in range(H):
        for j in range(1, W):
            if A[i][j-1]+1 < A[i][j]:
                A[i][j] = A[i][j-1]+1
        for j in range(W-2, -1, -1):
            if A[i][j+1]+1 < A[i][j]:
                A[i][j] = A[i][j+1]+1
    for j in range(W):
        for i in range(1, H):
            if A[i-1][j]+1 < A[i][j]:
                A[i][j] = A[i-1][j]+1
        for i in range(H-2, -1, -1):
            if A[i+1][j]+1 < A[i][j]:
                A[i][j] = A[i+1][j]+1
    ans = 0
    for i in range(H):
        for j in range(W):
            if A[i][j] > ans:
                ans = A[i][j]
    print(ans)

main()

Solution (Python):


import sys
import numpy as np

def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    INF = H * W
    dp = [[INF if c == '.' else 0 for c in input().strip()] for _ in range(H)]
    dp = np.array(dp)

    for i in range(1, H):
        dp[i, :] = np.minimum(dp[i, :], dp[i-1, :] + 1)

    for i in range(H-2, -1, -1):
        dp[i, :] = np.minimum(dp[i, :], dp[i+1, :] + 1)

    for i in range(1, W):
        dp[:, i] = np.minimum(dp[:, i], dp[:, i-1] + 1)

    for i in range(W-2, -1, -1):
        dp[:, i] = np.minimum(dp[:, i], dp[:, i+1] + 1)

    return np.max(dp)

if __name__ == '__main__':
    print(main())

Solution (Python):
def main():
    high, width = map(int, input().split())
    data = [list(input()) for i in range(high)]

    now_list = []
    next_list = []

    for i in range(high):
        for j in range(width):
            if data[i][j] == '
                now_list.append([i, j])

    count = 0

    while 1:
        for now_data in now_list:
            i, j = now_data

            kari_i1, kari_i2 = i - 1, i + 1
            kari_j1, kari_j2 = j - 1, j + 1

            if kari_i1 >= 0:
                a, b = kari_i1, j
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '

            if kari_i2 <= high - 1:
                a, b = kari_i2, j
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '

            if kari_j1 >= 0:
                a, b = i, kari_j1
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '

            if kari_j2 <= width - 1:
                a, b = i, kari_j2
                if data[a][b] == '.':
                    next_list.append([a, b])
                    data[a][b] = '

        now_list = next_list
        next_list = []


        if len(now_list) == 0:
            break
        else:
            count += 1


    print(count)



if __name__ == '__main__':
    main()

Solution (Python):
H, W = map(int, input().split())
S = [''] * H
M = [[0 for w in range(W)] for h in range(H)]
for i in range(H):
  S[i] = input()
  n = H * W
  for j in range(W):
    if S[i][j] == '
      M[i][j] = 0
      n = 0
    else:
      n += 1
      M[i][j] = n
  n = H * W
  for j in range(W - 1, -1, -1):
    if S[i][j] == '
      n = 0
    else:
      n = min(n + 1, M[i][j])
      M[i][j] = n
for j in range(W):
  n = H * W
  for i in range(H):
    if S[i][j] == '
      n = 0
    else:
      n = min(n + 1, M[i][j])
      M[i][j] = n
  n = H * W
  for i in range(H - 1, -1, -1):
    if S[i][j] == '
      n = 0
    else:
      n = min(n + 1, M[i][j])
      M[i][j] = n
print(max([max(m) for m in M]))

Solution (Python):
inpl = lambda: list(map(int,input().split()))
H, W = inpl()
A = []
new_black = []
Nb = 0
for h in range(H):
    line = input()
    Aline = [0] * W
    for w in range(W):
        if line[w] == '
            Aline[w] = 1
            new_black.append((h,w))
            Nb += 1
    A.append(Aline)
Nw = H*W - Nb
t = 0
while Nw > 0:
    t += 1
    prev_black = new_black
    new_black = []
    for h, w in prev_black:
        for nh, nw in [(h+1,w), (h-1,w), (h,w+1), (h,w-1)]:
            if 0<=nh<H and 0<=nw<W and A[nh][nw] == 0:
                Nw -= 1
                A[nh][nw] = 1
                new_black.append((nh,nw))
print(t)

Solution (Python):
h,w= map(int, input().split())
s_list = []
b_list = []
for i in range(h):
    li = input()
    aa = []
    for j in range(w):
        aa.append(li[j])
        if aa[j] == '
            b_list.append((i, j))
    s_list.append(aa)
ans = 0
while True:
    bb_list = []
    for p in b_list:
        pp = [(p[0]-1, p[1]), (p[0]+1, p[1]), (p[0], p[1]-1), (p[0], p[1]+1)]
        for ppp in pp:
            if (0 <= ppp[0] < h) and (0 <= ppp[1] < w):
                if s_list[ppp[0]][ppp[1]] == '.':
                    s_list[ppp[0]][ppp[1]] = '
                    bb_list.append((ppp[0], ppp[1]))
    
    b_list = bb_list
    if len(b_list) == 0:
        break
    ans += 1
    
print(ans)

Solution (Python):
from collections import deque

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

dist = [[-1] * W for _ in range(H)]
dq = deque()

for r in range(H):
    for c in range(W):
        if a[r][c] == '
            dist[r][c] = 0
            dq.append((r, c))


def in_range(r, c):
    return 0 <= r < H and 0 <= c < W


dr_s = (0, 0, -1, 1)
dc_s = (-1, 1, 0, 0)

d = 0
while dq:
    r, c = dq.popleft()
    d = dist[r][c]
    for dr, dc in zip(dr_s, dc_s):
        nr = r + dr
        nc = c + dc
        if not in_range(nr, nc): continue
        if dist[nr][nc] != -1: continue
        dist[nr][nc] = d + 1
        dq.append((nr, nc))

print(d)

Solution (Python):
import sys
from collections import deque


fin = sys.stdin.readline
H, W = [int(elem) for elem in fin().split()]
A_list = [fin().rstrip("\n") for _ in range(H)]
assert len(A_list[0]) == W

visited = [[False] * W for _ in range(H)]
que = deque((i, j, 0) for i in range(H) for j in range(W) if A_list[i][j] == "
max_dist = 0
while que:
    i, j, dist = que.popleft()
    for next_i, next_j in ((i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)):
        if not (0 <= next_i < H and 0 <= next_j < W):
            continue
        elif visited[next_i][next_j]:
            continue
        elif A_list[next_i][next_j] == "
            continue
        else:
            que.append((next_i, next_j, dist + 1))
            visited[next_i][next_j] = True
            max_dist = max(max_dist, dist + 1)
print(max_dist)

Solution (Python):
from collections import deque
H,W=map(int,input().split())
A=[list(input()) for i in range(H)]
q=deque([])
dist=[[-1 for j in range(W)] for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="
            q.append((i,j))
            dist[i][j]=0
dx=[-1,1,0,0]
dy=[0,0,1,-1]
while(len(q)>0):
    x,y=q.popleft()
    for k in range(4):
        nx=x+dx[k]
        ny=y+dy[k]
        if not(0<=nx<H and 0<=ny<W):
            continue
        if dist[nx][ny]==-1:
            dist[nx][ny]=dist[x][y]+1
            q.append((nx,ny))
print(max([max(i) for i in dist]))

Solution (Python):
from collections import deque

H, W = map(int, input().split())


black_cells = []

for h in range(H):
    line = list(input())
    for w in range(W):
        if line[w] == "
            black_cells.append(h * W + w)



def r(p):
    q_list = []
    h = p // W
    w = p % W
    if h != 0:
        q_list.append(p - W)
    if h != H-1:
        q_list.append(p + W)
    if w != 0:
        q_list.append(p - 1)
    if w != W-1:
        q_list.append(p + 1)
    return q_list


queue = deque(black_cells)
d = [10000] * (H * W)
for i in black_cells:
    d[i] = 0


while len(queue) > 0:
    p = queue.popleft()
    for q in r(p):
        if d[q] > d[p] + 1:
            d[q] = d[p] + 1
            queue.append(q)

print(max(d))

Solution (Python):
from collections import deque

h, w = map(int,input().split())
alist = [input().strip() for i in range(h)]
dxy = [(0,1),(1,0),(0,-1),(-1,0)]

INF = 99999
dp = [[INF]*w for i in range(h)]
q = deque()
for y, row in enumerate(alist):
  for x, c in enumerate(row):
    if c=='
      dp[y][x] = 0
      q.append((x,y))

while q:
  x, y = q.popleft()
  d = dp[y][x]
  for dx, dy in dxy:
    nx, ny = x+dx, y+dy
    if not 0 <= nx < w:
      continue
    if not 0 <= ny < h:
      continue
    if dp[ny][nx] <= d+1:
      continue
    dp[ny][nx] = d+1
    q.append((nx,ny))

print(max(max(row) for row in dp))

Solution (Python):
import numpy as np

H, W = map(int, input().split())

status = np.full((H, W), H*W, dtype=int)
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == "
            status[i,j] = 0

for h in range(1, H):
    status[h, :] = np.minimum(status[h, :], status[h-1, :] + 1)
for h in range(0, H-1)[::-1]:
    status[h, :] = np.minimum(status[h, :], status[h+1, :] + 1)
for w in range(1, W):
    status[:, w] = np.minimum(status[:, w], status[:, w-1] + 1)
for w in range(0, W-1)[::-1]:
    status[:, w] = np.minimum(status[:, w], status[:, w+1] + 1)

print(np.max(status))

Solution (Python):
from collections import deque

def inside(x, l, r):
    return l <= x < r

if __name__ == '__main__':
    h, w = map(int, input().split())

    a = []
    for i in range(h):
        a.append(list(input()))

    qu = deque()
    
    nokori = h*w
    for i in range(h):
        for j in range(w):
            if a[i][j] == '
                nokori -= 1
                qu.append((i, j))

    ans = 0
    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]
    while True:
        if nokori == 0:
            break
        ans += 1
        qu2 = deque()
        while len(qu) > 0:
            i0, j0 = qu.popleft()
            for k in range(4):
                ni = i0 + dx[k]
                nj = j0 + dy[k]
                if not (inside(ni, 0, h) and inside(nj, 0, w)):
                    continue
                if a[ni][nj] == '.':
                    a[ni][nj] = '
                    nokori -= 1
                    qu2.append((ni, nj))
        qu = qu2
    print(ans)

Solution (Python):
from collections import deque
import sys
def input():
	return sys.stdin.readline()[:-1]

H, W = map(int, input().split())
m = ["
q = deque()
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

step = [[float("inf") for _ in range(W+2)] for _ in range(H+2)]

for h in range(1, H+1):
	for w in range(1, W+1):
		if m[h][w] == "
			q.appendleft([h, w])
			step[h][w] = 0

while q:
	label = q.pop()

	for i in range(4):
		h, w = label[0] + dx[i], label[1] + dy[i]
		if m[h][w] != "
			q.appendleft([h, w])
			step[h][w] = step[label[0]][label[1]] + 1

ans = 0
for h in range(1, H+1):
	for w in range(1, W+1):
		ans = max(ans, step[h][w])
print(ans)

Solution (Python):
import collections

H,W = map(int, input().split())
grid = [0]*(H*W)
black = collections.deque()
for i in range(H):
  A_i = input()
  for j in range(W):
    grid[W*i + j] = 0 if A_i[j] == '
    if A_i[j] == '
      black.append(W*i + j)

ans = 0
while True:
  black_new = collections.deque()
  while len(black) > 0:
    i = black.pop()
    if i - W >= 0:
      if grid[i - W] == -1:
        grid[i - W] = ans + 1
        black_new.append(i-W)
    if i - 1 >= 0 and i%W != 0:
      if grid[i - 1] == -1:
        grid[i - 1] = ans + 1
        black_new.append(i-1)
    if i + 1 <= H*W-1 and i%W != W-1:
      if grid[i + 1] == -1:
        grid[i + 1] = ans + 1
        black_new.append(i+1)
    if i + W <= H*W-1:
      if grid[i + W] == -1:
        grid[i + W] = ans + 1
        black_new.append(i+W)
  black = black_new
  if len(black) == 0:
    print(ans)
    break
  ans += 1