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



Sample input:
3 3
...
.#.
...


Sample output:
2

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
h, w = map(int, input().split())
a = []
a = [[True] * (w + 2)]
for i in range(h):
    a.append([True] + [x == '#' for x in input()] + [True])
a.append([True] * (w + 2))
q = []


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

count = 0
while q:
    qq = []
    for i, j in q:
        if not a[i-1][j]:
            a[i-1][j] = True
            qq.append((i - 1, j))
        if not a[i+1][j]:
            a[i+1][j] = True
            qq.append((i + 1, j))
        if not a[i][j-1]:
            a[i][j-1] = True
            qq.append((i, j - 1))
        if not a[i][j+1]:
            a[i][j+1] = True
            qq.append((i, j + 1))
    q = qq
    if len(q) <= 0:
        break
    count += 1

print(count)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
h, w = map(int, input().split())
a = []
a = [[True] * (w + 2)]
for i in range(h):
    a.append([True] + [x == '#' for x in input()] + [True])
a.append([True] * (w + 2))
q = []


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

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

print(count)

Submission status: Runtime Error

Solution code:
import sys

R,C = list(map(int,input().split()))
lst = []

whites = []
blacks = []
color_db = {}


for i in range(R):
    st = input()
    tmp = []
    cnt = -1
    for c in st:
        cnt += 1
        set = (i,cnt)
        if c == '.':
            whites.append(set)
            color_db[set]=0
        else:
            blacks.append(set)
            color_db[set] = 1
#
# print(whites)
# print(blacks)
# print(color_db)
#


result = 0
while True:
    new_blacks = []
    result += 1
    for element in blacks:
        # new_blacks.append(element)
        r = element[0]
        c = element[1]

        #1
        if r == 0:
            if c == 0:
                if color_db[(r,c+1)] == 0:
                    color_db[(r,c+1)] = 1
                    whites.remove((r,c+1))
                    new_blacks.append((r,c+1))
                if color_db[(r+1,c)] == 0:
                    color_db[(r+1,c)] = 1
                    whites.remove((r+1,c))
                    new_blacks.append((r+1,c))
         #2
            else:
                if c == C-1:
                    if color_db[(r + 1, c)] == 0:
                        color_db[(r+1,c)] = 1
                        whites.remove((r+1,c))
                        new_blacks.append((r+1,c))
                    if color_db[(r,c-1)] == 0:
                        color_db[(r,c-1)] = 1
                        whites.remove((r,c-1))
                        new_blacks.append((r,c-1))
        #3
                else:
                    if color_db[(r + 1, c)] == 0:
                        color_db[(r+1,c)] = 1
                        whites.remove((r+1,c))
                        new_blacks.append((r+1,c))
                    if color_db[(r,c-1)] == 0:
                        color_db[(r,c-1)] = 1
                        whites.remove((r,c-1))
                        new_blacks.append((r,c-1))
                    if color_db[(r,c+1)] == 0:
                        color_db[(r,c+1)] = 1
                        whites.remove((r,c+1))
                        new_blacks.append((r,c+1))
        else:
            if r == R-1:
                if c == 0:
                    if color_db[(r, c + 1)] == 0:
                        color_db[(r,c+1)] = 1
                        whites.remove((r,c+1))
                        new_blacks.append((r,c+1))
                    if color_db[(r - 1, c)] == 0:
                        color_db[(r - 1, c)] = 1
                        whites.remove((r - 1, c))
                        new_blacks.append((r - 1, c))
                # 2
                else:
                    if c == C - 1:
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                    # 3
                    else:
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                        if color_db[(r, c + 1)] == 0:
                            color_db[(r,c+1)] = 1
                            whites.remove((r,c+1))
                            new_blacks.append((r,c+1))
            else:
                if c == 0:
                    if color_db[(r+1, c)] == 0:
                        color_db[(r+1,c)] = 1
                        whites.remove((r+1,c))
                        new_blacks.append((r+1,c))
                    if color_db[(r, c + 1)] == 0:
                        color_db[(r,c+1)] = 1
                        whites.remove((r,c+1))
                        new_blacks.append((r,c+1))
                    if color_db[(r - 1, c)] == 0:
                        color_db[(r - 1, c)] = 1
                        whites.remove((r - 1, c))
                        new_blacks.append((r - 1, c))
                    # 2
                else:
                    if c == C - 1:
                        if color_db[(r + 1, c)] == 0:
                            color_db[(r+1,c)] = 1
                            whites.remove((r+1,c))
                            new_blacks.append((r+1,c))
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                    # 3
                    else:
                        if color_db[(r + 1, c)] == 0:
                            color_db[(r+1,c)] = 1
                            whites.remove((r+1,c))
                            new_blacks.append((r+1,c))
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                        if color_db[(r, c + 1)] == 0:
                            color_db[(r,c+1)] = 1
                            whites.remove((r,c+1))
                            new_blacks.append((r,c+1))

    blacks = new_blacks
    new_blacks = []
    # print(whites)
    # print(blacks)
    # print(color_db)
    if whites == []:
        break


print(result)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import copy

H, W = map(int, input().split())
rest = H * W
lst = [[0 for i in range(H)] for j in range(W)]
answer = 0

black_mass = set()
new_black_mass = set()

for h in range(H):
    line=input()
    for w in range(W):
        if line[w] == '#':
            rest -= 1
            lst[h][w] = 1
            black_mass.add((h,w))

while(rest > 0):
    for xy in black_mass:
        if xy[0] > 0:
            if lst[xy[0]-1][xy[1]] == 0:
                lst[xy[0]-1][xy[1]] = 1
                new_black_mass.add((xy[0]-1, xy[1]))
                rest -= 1
        if xy[0] < W - 1:
            if lst[xy[0]+1][xy[1]] == 0:
                lst[xy[0]+1][xy[1]] = 1
                new_black_mass.add((xy[0]+1, xy[1]))
                rest -= 1
        if xy[1] > 0:
            if lst[xy[0]][xy[1]-1] == 0:
                lst[xy[0]][xy[1]-1] = 1
                new_black_mass.add((xy[0], xy[1]-1))
                rest -= 1
        if xy[1] < H - 1:
            if lst[xy[0]][xy[1]+1] == 0:
                lst[xy[0]][xy[1]+1] = 1
                new_black_mass.add((xy[0], xy[1]+1))
                rest -= 1
    black_mass.clear()
    black_mass = copy.deepcopy(new_black_mass)
    answer += 1

print(answer)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import copy

H, W = map(int, input().split())
rest = H * W
lst = [[0 for i in range(H)] for j in range(W)]
answer = 0

black_mass = set()
new_black_mass = set()

for h in range(H):
    line=input()
    for w in range(W):
        if line[w] == '#':
            rest -= 1
            lst[h][w] = 1
            black_mass.add((h,w))

while(rest > 0):
    for xy in black_mass:
        if xy[0] > 0:
            if lst[xy[0]-1][xy[1]] == 0:
                lst[xy[0]-1][xy[1]] = 1
                new_black_mass.add((xy[0]-1, xy[1]))
                rest -= 1
        if xy[0] < W - 1:
            if lst[xy[0]+1][xy[1]] == 0:
                lst[xy[0]+1][xy[1]] = 1
                new_black_mass.add((xy[0]+1, xy[1]))
                rest -= 1
        if xy[1] > 0:
            if lst[xy[0]][xy[1]-1] == 0:
                lst[xy[0]][xy[1]-1] = 1
                new_black_mass.add((xy[0], xy[1]-1))
                rest -= 1
        if xy[1] < H - 1:
            if lst[xy[0]][xy[1]+1] == 0:
                lst[xy[0]][xy[1]+1] = 1
                new_black_mass.add((xy[0], xy[1]+1))
                rest -= 1
    black_mass.clear()
    black_mass = copy.copy(new_black_mass)
    answer += 1

print(answer)

Submission status: Runtime Error

Solution code:
import sys

R,C = list(map(int,input().split()))
lst = []

whites = []
blacks = []
color_db = {}


for i in range(R):
    st = input()
    tmp = []
    cnt = -1
    for c in st:
        cnt += 1
        set = (i,cnt)
        if c == '.':
            whites.append(set)
            color_db[set]=0
        else:
            blacks.append(set)
            color_db[set] = 1
#
# print(whites)
# print(blacks)
# print(color_db)
#


result = 0
while True:
    new_blacks = []
    result += 1
    for element in blacks:
        new_blacks.append(element)
        r = element[0]
        c = element[1]

        #1
        if r == 0:
            if c == 0:
                if color_db[(r,c+1)] == 0:
                    color_db[(r,c+1)] = 1
                    whites.remove((r,c+1))
                    new_blacks.append((r,c+1))
                if color_db[(r+1,c)] == 0:
                    color_db[(r+1,c)] = 1
                    whites.remove((r+1,c))
                    new_blacks.append((r+1,c))
         #2
            else:
                if c == C-1:
                    if color_db[(r + 1, c)] == 0:
                        color_db[(r+1,c)] = 1
                        whites.remove((r+1,c))
                        new_blacks.append((r+1,c))
                    if color_db[(r,c-1)] == 0:
                        color_db[(r,c-1)] = 1
                        whites.remove((r,c-1))
                        new_blacks.append((r,c-1))
        #3
                else:
                    if color_db[(r + 1, c)] == 0:
                        color_db[(r+1,c)] = 1
                        whites.remove((r+1,c))
                        new_blacks.append((r+1,c))
                    if color_db[(r,c-1)] == 0:
                        color_db[(r,c-1)] = 1
                        whites.remove((r,c-1))
                        new_blacks.append((r,c-1))
                    if color_db[(r,c+1)] == 0:
                        color_db[(r,c+1)] = 1
                        whites.remove((r,c+1))
                        new_blacks.append((r,c+1))
        else:
            if r == R-1:
                if c == 0:
                    if color_db[(r, c + 1)] == 0:
                        color_db[(r,c+1)] = 1
                        whites.remove((r,c+1))
                        new_blacks.append((r,c+1))
                    if color_db[(r - 1, c)] == 0:
                        color_db[(r - 1, c)] = 1
                        whites.remove((r - 1, c))
                        new_blacks.append((r - 1, c))
                # 2
                else:
                    if c == C - 1:
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                    # 3
                    else:
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                        if color_db[(r, c + 1)] == 0:
                            color_db[(r,c+1)] = 1
                            whites.remove((r,c+1))
                            new_blacks.append((r,c+1))
            else:
                if c == 0:
                    if color_db[(r+1, c)] == 0:
                        color_db[(r+1,c)] = 1
                        whites.remove((r+1,c))
                        new_blacks.append((r+1,c))
                    if color_db[(r, c + 1)] == 0:
                        color_db[(r,c+1)] = 1
                        whites.remove((r,c+1))
                        new_blacks.append((r,c+1))
                    if color_db[(r - 1, c)] == 0:
                        color_db[(r - 1, c)] = 1
                        whites.remove((r - 1, c))
                        new_blacks.append((r - 1, c))
                    # 2
                else:
                    if c == C - 1:
                        if color_db[(r + 1, c)] == 0:
                            color_db[(r+1,c)] = 1
                            whites.remove((r+1,c))
                            new_blacks.append((r+1,c))
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                    # 3
                    else:
                        if color_db[(r + 1, c)] == 0:
                            color_db[(r+1,c)] = 1
                            whites.remove((r+1,c))
                            new_blacks.append((r+1,c))
                        if color_db[(r - 1, c)] == 0:
                            color_db[(r - 1, c)] = 1
                            whites.remove((r - 1, c))
                            new_blacks.append((r - 1, c))
                        if color_db[(r, c - 1)] == 0:
                            color_db[(r,c-1)] = 1
                            whites.remove((r,c-1))
                            new_blacks.append((r,c-1))
                        if color_db[(r, c + 1)] == 0:
                            color_db[(r,c+1)] = 1
                            whites.remove((r,c+1))
                            new_blacks.append((r,c+1))

    blacks = new_blacks
    new_blacks = []
    # print(whites)
    # print(blacks)
    # print(color_db)
    if whites == []:
        break


print(result)

Submission status: Runtime Error

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

grid = [[v for v in input()[:-1]] for _ in range(H)]
dist = [[float('inf')]*W for _ in range(H)]

Q = []
cnt = 0
for row in range(H):
    for col in range(W):
        if grid[row][col] == "#":
            dist[row][col] = 0
            Q.append([row, col])
            cnt += 1

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
current_dist = 0
while cnt < H*W and Q:
    current_dist += 1
    tmp = []
    for sx, sy in Q:
        for x, y in zip(dx, dy):
            new_x = sx+x
            new_y = sy+y
            if 0 <= new_x < H and 0 <= new_y < W:
                if dist[new_x][new_y] == float('inf'):
                    dist[new_x][new_y] = current_dist
                    tmp.append([new_x, new_y])
                    cnt += 1
    Q = tmp
print(d)

Submission status: Runtime Error

Solution code:
import copy
from collections import deque

H,W=map(int,input().split())
L=list(list(input()) for _ in range(H))
q=deque([])
for i,I in enumerate(L):
    for j in range(W):
        if I[j]=='#':
            q.append([i,j])
ans=[]
for y,x in q:
    L[y][x]=0
while len(q)>0:
    y,x=q.popleft()
    for dy,dx in [[1,0],[-1,0],[0,1],[0,-1]]:
        if 0<=y+dy<H and 0<=x+dx<W and L[y+dy][x+dx]=='.':
            L[y+dy][x+dx]=L[y][x]+1
            ans.append(L[y][x]+1)
            q.append([y+dy,x+dx])
print(max(ans))

Submission status: Runtime Error

Solution code:
H,W = list(map(int,input().split()))

grid = [[0]*W for i in range(H)]
step = 0
tar = 0
temp = []
for i in range(H):
    string = input()
    for j in range(W):
        if string[j] == '#':
            grid[i][j] = 1
            tar += 1
            temp.append((i,j))


while tar < H*W:
    step += 1
    temp1 = []
    for x in temp:
        i,j = x[0],x[1]
        for y in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
            if  grid[y[0]][y[1]] == 0:
                if y[0]>=0 and y[0]<= H-1 and y[1]>=0 and y[1]<= W-1:
                    grid[y[0]][y[1]] = 1
                    tar += 1
                    temp1.append((y[0],y[1]))

    temp = temp1

print(step)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-


def main():
    from collections import deque

    h, w = map(int, input().split())
    a = [list(input()) for _ in range(h)]
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    dist = [[float('inf') for __ in range(w)] for _ in range(h)]
    blacks = deque()

    # See:
    # http://drken1215.hatenablog.com/entry/2019/05/05/223200?_ga=2.177845805.987014750.1556545680-1855989383.1556545680
    # https://blog.kichi2004.jp/2019/05/agc033/
    for i in range(h):
        for j in range(w):
            if a[i][j] == '#':
                dist[i][j] = 0
                blacks.append((i, j))

    while blacks:
        current = blacks.popleft()
        cur_x = current[0]
        cur_y = current[1]

        for i in range(len(dx)):
            next_x = cur_x + dx[i]
            next_y = cur_y + dy[i]

            if next_x < 0 or next_x >= w or next_y < 0 or next_y >= h:
                continue

            if dist[next_x][next_y] == float('inf'):
                dist[next_x][next_y] = dist[cur_x][cur_y] + 1
                blacks.append((next_x, next_y))

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from queue import Queue

H, W = map(int, input().split())
matrix = [[-1 for i in range(H)] for j in range(W)]
q = Queue()


for i in range(H):
    S = input()
    for j in range(len(S)):
        if(S[j]=="#"):
            matrix[i][j] = 0
            q.put((i, j))
ans = 0
while(not q.empty()):
    i, j = q.get()
    for di, dj in [(0, -1), (1, 0), (0, 1), (-1, 0)]:
        ni, nj = i+di, j+dj
        if(ni < 0 or ni >= W or nj < 0 or nj >= H):
            continue
        if(matrix[ni][nj]!=-1):
            continue
        matrix[ni][nj] = matrix[i][j]+1
        ans=max(ans, matrix[ni][nj])
        q.put((ni, nj))

print(ans)

Submission status: Runtime Error

Solution code:
import copy
import collections

H, W = map(int, input().split())
X = [list(map(str, list(input()))) for i in range(H)]
visit = copy.deepcopy(X)

queue = collections.deque()
tmpqueue = collections.deque()
ans = -1

for y in range(H):
    for x in range(W):
        visit[y][x] = 0
        if X[y][x] == "#":
            queue.append((y, x))
            visit[y][x] = 1

while queue:
    while queue:
        y, x = queue.pop()
        if x - 1 >= 0 and visit[y][x - 1] == 0 and X[y][x - 1] == ".":
            X[y][x - 1] = "#"
            tmpqueue.append((y, x - 1))
            visit[y][x - 1] = 1
        if x + 1 < W and visit[y][x + 1] == 0 and X[y][x + 1] == ".":
            X[y][x + 1] = "#"
            tmpqueue.append((y, x + 1))
            visit[y][x + 1] = 1
        if y - 1 >= 0 and visit[y - 1][x] == 0 and X[y - 1][x] == ".":
            X[y - 1][x] = "#"
            tmpqueue.append((y - 1, x))
            visit[y - 1][x] = 1
        if y + 1 < H and visit[y + 1][x] == 0 and X[y + 1][x] == ".":
            X[y + 1][x] = "#"
            tmpqueue.append((y + 1, x))
            visit[y + 1][x] = 1
    queue = tmpqueue.copy()
    tmpqueue = collections.deque()
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().strip().split())

dist = []
queue = []

for i in range(H):
    dist.append([[] for i in range(W)])
    line = input().strip()
    for j in range(W):
        if line[j] == '#':
            dist[i][j] = 0
            queue.append([i, j])
        else:
            dist[i][j] = -1

moves = [
    [-1, 0],
    [1, 0],
    [0, -1],
    [0, 1],
]

while len(queue) > 0:
    x, y = queue.pop(0)

    for dx, dy in moves:
        next_x = x + dx
        next_y = y + dy

        if next_x < 0 or next_x >= W or next_y < 0 or next_y >= H:
            continue

        if dist[next_x][next_y] == -1:
            dist[next_x][next_y] = dist[x][y] + 1
            queue.append([next_x, next_y])
ans = 0
for i in dist:
    ans = max(ans, max(i))

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np


def func(x):
    if x == '.':
        return 2000
    else:
        return 0


H, W = map(int, input().split())
b = []
for _ in range(W):
    b.append(list(map(func, input())))
a = np.array(b)

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

print(np.max(a))

Submission status: Runtime Error

Solution code:
import numpy as np


def func(x):
    if x == '.':
        return 2000
    else:
        return 0


H, W = map(int, input().split())
b = []
for _ in range(W):
    b.append(list(map(func, input())))
a = np.array(b)

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

print(max(a))

Submission status: Runtime Error

Solution code:
import numpy as np


def func(x):
    if x == '.':
        return 2000
    else:
        return 0


H, W = map(int, input().split())
b = []
for _ in range(W):
    b.append(list(map(func, input())))
a = np.array(b)

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

print(np.max(a))

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
H, W = map(int, input().split())

data = "".join(input() + "#" for _ in range(H))
data = data.replace(".", "1").replace("#", "0")

""" intの進数を利用
> int("01", 2)
1
> int("11", 2)
3
> int("10", 2)
2
"""
bits = int(data, 2)

W1 = W + 1
H1 = H + 1
# bitsをW1分左シフト
bits <<= W1

# 1をW1分左シフト
frame = (1 << W1) - 1
#
frame |= frame <<  H1 ** W1
#
frame |= sum(1 << b for b in range(W1, H1 * W1, W1))
 
count = 0
while bits:
    count += 1
    mask = bits | frame
    #       up             down           left          right
    bits &= (mask << W1) & (mask >> W1) & (mask << 1) & (mask >> 1)
print(count)

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
H, W = map(int, input().split())

data = "".join(input() + "#" for _ in range(H))

data = data.replace(".", "1").replace("#", "0")

""" intの進数を利用
> int("01", 2)
1
> int("11", 2)
3
> int("10", 2)
2
"""
bits = int(data, 2)

W1 = W + 1
H1 = H + 1

# 1をW1分左シフト
frame = (1 << W1) - 1
#
frame |= frame <<  H1 ** W1
#
frame |= sum(1 << b for b in range(W1, H1 * W1, W1))
 
count = 0
while bits:
    count += 1
    mask = bits | frame
    #       up             down           left          right
    bits &= (mask << W1) & (mask >> W1) & (mask << 1) & (mask >> 1)
print(count)

Submission status: Runtime Error

Solution code:
# A - Darker and Darker
H, W = map(int, input().split())

data = "".join(input() for _ in range(H))

data = data.replace(".", "1").replace("#", "0")

""" intの進数を利用
> int("01", 2)
1
> int("11", 2)
3
> int("10", 2)
2
"""
bits = int(data, 2)

W1 = W + 1
H1 = H + 1

# 1をW1分左シフト
frame = (1 << W1) - 1
#
frame |= frame <<  H1 ** W1
#
frame |= sum(1 << b for b in range(W1, H1 * W1, W1))
 
count = 0
while bits:
    count += 1
    mask = bits | frame
    #       up             down           left          right
    bits &= (mask << W1) & (mask >> W1) & (mask << 1) & (mask >> 1)
print(count)

Submission status: Runtime Error

Solution code:
from queue import Queue
h, w = map(int, input().split())
m = [list(input()) for _ in range(h)]


def put(i, j, x, q, v):
    if j + 1 < w and not v[i][j + 1]:
        q.put((i, j + 1, x + 1))
        v[i][j + 1] = True
    if j - 1 >= 0 and not v[i][j - 1]:
        q.put((i, j - 1, x + 1))
        v[i][j - 1] = True
    if i + 1 < w and not v[i + 1][j]:
        q.put((i + 1, j, x + 1))
        v[i + 1][j] = True
    if i - 1 >= 0 and not v[i - 1][j]:
        q.put((i - 1, j, x + 1))
        v[i - 1][j] = True


for i in range(len(m)):
    for j in range(len(m[i])):
        if m[i][j] == '#':
            v = [[False for _ in range(w)] for _ in range(h)]
            q = Queue()
            v[i][j] = True
            put(i, j, 0, q, v)
            while not q.empty():
                k, l, x = q.get()
                if m[k][l] == '.' or ord(m[k][l]) - ord('0') > x:
                    m[k][l] = chr(x + ord('0'))
                    put(k, l, x, q, v)
c = None
for i in m:
    for j in i:
        if j != '#' and (c is None or c < j):
            c = j
print(c)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque


dx_list = [1, 0, -1, 0]
dy_list = [0, -1, 0, 1]
def bfs(h_len, w_len):
    max_cnt = 0
    while len(bfs_q)>0:
        u_list = bfs_q.popleft()
        h, w, cnt = u_list[0], u_list[1], u_list[2]
        max_cnt = max(max_cnt, cnt)
        for dx, dy in zip(dx_list, dy_list):
            x = w + dx
            y = h + dy
            if 0 <= x < w_len and 0 <= y < h_len and m[y][x] == 0:
                m[y][x] = 1
                bfs_q.append([y, x, cnt+1])
    return max_cnt


if __name__ == "__main__":
    input = sys.stdin.readline
    h_len, w_len = map(int, input().split())
    m = [[0]*h_len for _ in range(w_len)]
    a_list = []
    for _ in range(h_len):
        s = input()[:-1]
        a_list.append(s)
    bfs_q = deque([])
    for h, w_list in enumerate(a_list):
        for w, a in enumerate(w_list):
    	    if a == '#':
    		    m[h][w] = 1
    		    bfs_q.append([h, w, 0])
    ans = bfs(h_len, w_len)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

dx_list = [1, 0, -1, 0]
dy_list = [0, -1, 0, 1]
def bfs(h_len, w_len):
    cnt = 0
    while len(bfs_q)>0:
        u_list = bfs_q.popleft()
        h, w = u_list[0], u_list[1]
        tmp = m[h][w]
        for dx, dy in zip(dx_list, dy_list):
            x = w + dx
            y = h + dy
            if 0 <= x < w_len and 0 <= y < h_len and m[y][x] == -1:
                m[y][x] = tmp + 1
                cnt = max(cnt, tmp + 1)
                bfs_q.append([y, x])
    return cnt


if __name__ == "__main__":
    input = sys.stdin.readline
    h_len, w_len = map(int, input().split())
    m = [[-1]*h_len for _ in range(w_len)]
    a_list = []
    for _ in range(h_len):
        s = input()[:-1]
        a_list.append(s)
    bfs_q = deque([])
    for h, w_list in enumerate(a_list):
        for w, a in enumerate(w_list):
    	    if a == '#':
    		    m[h][w] = 0
    		    bfs_q.append([h, w])
    ans = bfs(h_len, w_len)
    print(ans)