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:
from collections import deque

H, W = map(int, input().split())
# A = [[2000 for _ in range(W)] for _ in range(H)]
B = deque()
check = [[False for _ in range(W)] for _ in range(H)]
for h in range(H):
    a = input()
    if a.count("#") > 0:
        for w, e in enumerate(a):
            if e == "#":
                B.append((h, w))
                check[h][w] = 0
                # A[h][w] = 0

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

while B:
    x, y = B.pop()
    for dx, dy in move:
        nx, ny = x + dx, y + dy
        if -1 < nx < H and -1 < ny < W:
            if check[nx][ny] == False:
                lx, ly = nx, ny
                check[nx][ny] = check[x][y] + 1
                B.appendleft((nx, ny))
print(check[lx][ly])

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
STAGE = [list(input()) for _ in range(H)]

black, white = [], 0
for y, line in enumerate(STAGE):
	for x, cell in enumerate(line):
		if "#" == cell:
			black += [(y, x)]
		else:
			white += 1

stage = STAGE.copy()
ds = ((0, 1), (1, 0), (0, -1), (-1, 0))

def get_next(p, d):
	n = (p[0] + d[0], p[1] + d[1])
	if 0 <= n[0] < H and 0 <= n[1] < W:
		return n
	return None

def set_val(p, v):
	global stage
	stage[p[0]][p[1]] = v

def get_val(p):
	return stage[p[0]][p[1]]

count = 0
while white:
	count += 1
	next_black = []
	for b in black:
		for d in ds:
			n = get_next(b, d)
			if n:
				v = get_val(n)
				if "." == v:
					white -= 1
					set_val(n, "#")
					next_black += [n]
	black = next_black

print(count)

Submission status: Runtime Error

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

a_list = [list(input()) for i in range(H)]

isEnd = False
_a_list = a_list[::][::]
cnt = 0
while not isEnd:
    isEnd = True
    next_a_list = [["." for i in range(H)] for j in range(W)]
    for i in range(H):
        for j in range(W):
            a = _a_list[i][j]
            if a == ".":
                #  白色
                isEnd = False
            else:
                next_a_list[i][j] = "#"
                #  黒色
                if i > 0:
                    next_a_list[i-1][j] = "#"
                if i < H-1:
                    next_a_list[i+1][j] = "#"
                if j > 0:
                    next_a_list[i][j-1] = "#"
                if j < W-1:
                    next_a_list[i][j+1] = "#"
    cnt += 1
    _a_list = next_a_list[::]

print(cnt-1)

Submission status: Runtime Error

Solution code:
import sys
import re

check_list=[]
remove=[]

append=check_list.append
append2=remove.append
for i in range(a[0]):
  b=list(input())
  for j , x in enumerate(b):
    if x == "#":
      append((i+1,j))
      append((i-1,j))
      append((i,j+1))
      append((i,j-1))
      append2((i,j))

for i in range(a[0]):
  append2((i,-1))
  append2((i,a[1]))

for i in range(a[1]):
  append2((-1,i))
  append2((a[0],i))

check_list = set(check_list)-set(remove)
check_list_new=[]
count=1
append3=check_list_new.append
while True:
  if not check_list:
    check_list = set(check_list_new) - set(remove)
    if not check_list:
      break 
    count+=1

  c=check_list.pop()
  append2((c[0],c[1]))

  append3((c[0]+1,c[1]))      
  append3((c[0]-1,c[1]))      
  append3((c[0],c[1]+1))      
  append3((c[0],c[1]-1))   

print(count)

Submission status: Runtime Error

Solution code:

from collections import deque

class Cell:
    x = -1
    y = -1
    distance = -1

    def __init__(self, y, x, distance):
        self.x = x
        self.y = y
        self.distance = distance


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

ans = 0

outside = ["#" for x in range(W+2)]
C_list = [outside]

bfs = deque()

max_distance = 0

checked_list = [[0 for x in range(H+2)] for y in range(W+2)]

for i in range(1, H+1):
    A = input()
    for j in range(W+2):
        if j == 0:
            C_list.append([])
            C_list[i].append("#")
        elif j == W+1:
            C_list[i].append("#")
        else:
            C_list[i].append(A[j-1])
            if A[j-1] == "#":
                bfs.append(Cell(i, j, 0))
                checked_list[i][j] = 1

C_list.append(outside)


while len(bfs) > 0:
    cell = bfs.popleft()
    x = cell.x
    y = cell.y
    distance = cell.distance
    checked_list[y][x] = 1

    max_distance = max(max_distance, distance)

    if checked_list[y][x-1] == 0 and C_list[y][x-1] == ".":
        bfs.append(Cell(y, x-1, distance+1))
        checked_list[y][x - 1] = 2
    if checked_list[y][x+1] == 0 and C_list[y][x+1] == ".":
        bfs.append(Cell(y, x+1, distance+1))
        checked_list[y][x + 1] = 2
    if checked_list[y-1][x] == 0 and C_list[y-1][x] == ".":
        bfs.append(Cell(y-1, x, distance+1))
        checked_list[y-1][x] = 2
    if checked_list[y+1][x] == 0 and C_list[y+1][x] == ".":
        bfs.append(Cell(y+1, x, distance+1))
        checked_list[y+1][x] = 2

print(max_distance)

Submission status: Runtime Error

Solution code:

from collections import deque

class Cell:
    x = -1
    y = -1
    distance = -1

    def __init__(self, y, x, distance):
        self.x = x
        self.y = y
        self.distance = distance


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

ans = 0

outside = ["#" for x in range(W+2)]
C_list = [outside]

bfs = deque()

max_distance = 0

for i in range(1, H+1):
    A = input()
    for j in range(W+2):
        if j == 0:
            C_list.append([])
            C_list[i].append("#")
        elif j == W+1:
            C_list[i].append("#")
        else:
            C_list[i].append(A[j-1])
            if A[j-1] == "#":
                bfs.append(Cell(i, j, 0))

C_list.append(outside)

checked_list = [[0 for x in range(H+2)] for y in range(W+2)]

while len(bfs) > 0:
    cell = bfs.popleft()
    x = cell.x
    y = cell.y
    distance = cell.distance
    checked_list[y][x] = 1

    max_distance = max(max_distance, distance)

    if checked_list[y][x-1] == 0 and C_list[y][x-1] == ".":
        bfs.append(Cell(y, x-1, distance+1))
        checked_list[y][x - 1] = 2
    if checked_list[y][x+1] == 0 and C_list[y][x+1] == ".":
        bfs.append(Cell(y, x+1, distance+1))
        checked_list[y][x + 1] = 2
    if checked_list[y-1][x] == 0 and C_list[y-1][x] == ".":
        bfs.append(Cell(y-1, x, distance+1))
        checked_list[y-1][x] = 2
    if checked_list[y+1][x] == 0 and C_list[y+1][x] == ".":
        bfs.append(Cell(y+1, x, distance+1))
        checked_list[y+1][x] = 2

print(max_distance)

Submission status: Runtime Error

Solution code:

from collections import deque

class Cell:
    x = -1
    y = -1
    distance = -1

    def __init__(self, y, x, distance):
        self.x = x
        self.y = y
        self.distance = distance


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

ans = 0

outside = ["#" for x in range(W+2)]
C_list = [outside]

bfs = deque()

max_distance = 0

for i in range(1, H+1):
    A = input()
    for j in range(W+2):
        if j == 0:
            C_list.append([])
            C_list[i].append("#")
        elif j == W+1:
            C_list[i].append("#")
        else:
            C_list[i].append(A[j-1])
            if A[j-1] == "#":
                bfs.append(Cell(i, j, 0))

C_list.append(outside)

checked_list = [[0 for x in range(H+2)] for y in range(W+2)]

while len(bfs) > 0:
    cell = bfs.popleft()
    x = cell.x
    y = cell.y
    distance = cell.distance
    checked_list[y][x] = 1

    max_distance = max(max_distance, distance)

    if checked_list[y][x-1] == 0 and C_list[y][x-1] == ".":
        bfs.append
from collections import deque

class Cell:
    x = -1
    y = -1
    distance = -1

    def __init__(self, y, x, distance):
        self.x = x
        self.y = y
        self.distance = distance


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

ans = 0

outside = ["#" for x in range(W+2)]
C_list = [outside]

bfs = deque()

max_distance = 0

for i in range(1, H+1):
    A = input()
    for j in range(W+2):
        if j == 0:
            C_list.append([])
            C_list[i].append("#")
        elif j == W+1:
            C_list[i].append("#")
        else:
            C_list[i].append(A[j-1])
            if A[j-1] == "#":
                bfs.append(Cell(i, j, 0))

C_list.append(outside)

checked_list = [[0 for x in range(H+2)] for y in range(W+2)]

while len(bfs) > 0:
    cell = bfs.popleft()
    x = cell.x
    y = cell.y
    distance = cell.distance
    checked_list[y][x] = 1

    max_distance = max(max_distance, distance)

    if checked_list[y][x-1] == 0 and C_list[y][x-1] == ".":
        bfs.append(Cell(y, x-1, distance+1))
        checked_list[y][x - 1] = 2
    if checked_list[y][x+1] == 0 and C_list[y][x+1] == ".":
        bfs.append(Cell(y, x+1, distance+1))
        checked_list[y][x + 1] = 2
    if checked_list[y-1][x] == 0 and C_list[y-1][x] == ".":
        bfs.append(Cell(y-1, x, distance+1))
        checked_list[y-1][x] = 2
    if checked_list[y+1][x] == 0 and C_list[y+1][x] == ".":
        bfs.append(Cell(y+1, x, distance+1))
        checked_list[y+1][x] = 2

print(max_distance)(Cell(y, x-1, distance+1))
        checked_list[y][x - 1] = 2
    if checked_list[y][x+1] == 0 and C_list[y][x+1] == ".":
        bfs.append(Cell(y, x+1, distance+1))
        checked_list[y][x + 1] = 2
    if checked_list[y-1][x] == 0 and C_list[y-1][x] == ".":
        bfs.append(Cell(y-1, x, distance+1))
        checked_list[y-1][x] = 2
    if checked_list[y+1][x] == 0 and C_list[y+1][x] == ".":
        bfs.append(Cell(y+1, x, distance+1))
        checked_list[y+1][x] = 2

print(max_distance)

Submission status: Runtime Error

Solution code:
h, w=map(int, input().split())
a=[]
visited=[[True for i in range(w)] for j in range(h)]
need_visited=[[0 for i in range(w)] for j in range(h)]
stack=[]
for i in range(h):
    a.append(list(input()))

for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            stack.append([i, j])
            visited[i][j]=False



dx=[1, -1, 0, 0]
dy=[0, 0, 1, -1]


while len(stack)!=0:
    now_point=stack.pop(0)
    now_y=now_point[0]
    now_x=now_point[1]
    for i in range(4):
        if 0<=now_x+dx[i]<=w-1 and 0<=now_y+dy[i]<=h-1:
            if a[now_x+dx[i]][now_y+dy[i]]==".":
                if visited[now_x+dx[i]][now_y+dy[i]]:
                    need_visited[now_x+dx[i]][now_y+dy[i]]=need_visited[now_x][now_y]+1
                    visited[now_x+dx[i]][now_y+dy[i]]=False

                    stack.append([now_y+dy[i], now_x+dx[i]])
max_list=[]
for i in range(h):
    max_list.append(max(need_visited[i]))
print(max(max_list))

Submission status: Runtime Error

Solution code:
h, w=map(int, input().split())
a=[]
visited=[[True for i in range(w)] for j in range(h)]
need_visited=[[0 for i in range(w)] for j in range(h)]
stack=[]
for i in range(h):
    a.append(list(input()))

for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            stack.append([i, j])
            visited[i][j]=False



dx=[1, -1, 0, 0]
dy=[0, 0, 1, -1]


while len(stack)!=0:
    now_point=stack.pop(0)
    now_y=now_point[0]
    now_x=now_point[1]
    for i in range(4):
        if 0<=now_x+dx[i]<=w-1 and 0<=now_y+dy[i]<=h-1:
            if a[now_x+dx[i]][now_y+dy[i]]==".":
                if visited[now_x+dx[i]][now_y+dy[i]]:
                    need_visited[now_x+dx[i]][now_y+dy[i]]=need_visited[now_x][now_y]+1
                    visited[now_x+dx[i]][now_y+dy[i]]=False

                    stack.append([now_x+dx[i], now_y+dy[i]])
max_list=[]
for i in range(h):
    max_list.append(max(need_visited[i]))
print(max(max_list))

Submission status: Runtime Error

Solution code:
h, w=map(int, input().split())
a=[]
visited=[[True for i in range(w)] for j in range(h)]
need_visited=[[h+w for i in range(w)] for j in range(h)]
stack=[]
for i in range(h):
    a.append(list(input()))

for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            stack.append([i, j])
            need_visited[i][j]=0
            visited[i][j]=False



dx=[1, -1, 0, 0]
dy=[0, 0, 1, -1]


while len(stack)!=0:
    now_point=stack.pop(0)
    now_y=now_point[0]
    now_x=now_point[1]
    for i in range(4):
        if 0<=now_x+dx[i]<=w-1 and 0<=now_y+dy[i]<=h-1:
            if a[now_x+dx[i]][now_y+dy[i]]==".":
                if visited[now_x+dx[i]][now_y+dy[i]]:
                    need_visited[now_x+dx[i]][now_y+dy[i]]=need_visited[now_x][now_y]+1
                    visited[now_x+dx[i]][now_y+dy[i]]=False

                    stack.append([now_x+dx[i], now_y+dy[i]])
max_list=[]
for i in range(h):
    max_list.append(max(need_visited[i]))
print(max(max_list))

Submission status: Runtime Error

Solution code:
h, w=map(int, input().split())
a=[]
visited=[[True for i in range(w)] for j in range(h)]
need_visited=[[h+w for i in range(w)] for j in range(h)]
stack=[]
for i in range(h):
    a.append(list(input()))

for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            stack.append([i, j])
            need_visited[i][j]=0
            visited=False



dx=[1, -1, 0, 0]
dy=[0, 0, 1, -1]


while len(stack)!=0:
    now_point=stack.pop(0)
    now_y=now_point[0]
    now_x=now_point[1]
    for i in range(4):
        if 0<=now_x+dx[i]<=w-1 and 0<=now_y+dy[i]<=h-1:
            if a[now_x+dx[i]][now_y+dy[i]]==".":
                if visited:
                    need_visited[now_x+dx[i]][now_y+dy[i]]=need_visited[now_x][now_y]+1
                    stack.append([now_x+dx[i], now_y+dy[i]])
max_list=[]
for i in range(h):
    max_list.append(max(need_visited[i]))
print(max(max_list))

Submission status: Runtime Error

Solution code:
h, w=map(int, input().split())
a=[]
need_visited=[[h+w for i in range(w)] for j in range(h)]
stack=[]
for i in range(h):
    a.append(list(input()))

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



dx=[1, -1, 0, 0]
dy=[0, 0, 1, -1]


while len(stack)!=0:
    now_point=stack.pop(0)
    now_y=now_point[0]
    now_x=now_point[1]
    for i in range(4):
        if 0<=now_x+dx[i]<=w-1 and 0<=now_y+dy[i]<=h-1:
            if a[now_x+dx[i]][now_y+dy[i]]==".":
                if need_visited[now_x+dx[i]][now_y+dy[i]]>need_visited[now_x][now_y]+1:
                    need_visited[now_x+dx[i]][now_y+dy[i]]=need_visited[now_x][now_y]+1
                    stack.append([now_x+dx[i], now_y+dy[i]])
max_list=[]
for i in range(h):
    max_list.append(max(need_visited[i]))
print(max(max_list))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import copy
import collections


def solve(H: int, W: int, A: "List[str]"):

    blist = []
    nextblist = collections.deque()
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                blist.append((i,j))
                nextblist.append((i,j))

    res = 0
    while len(blist) < H*W:
        cnt = len(nextblist)
        res += 1
        for _ in range(cnt):
            y,x = nextblist.pop()
            for dy,dx in [(1,0),(-1,0),(0,1),(0,-1)]:
                if 0 <= y+dy < H and 0 <= x+dx < W and (y+dy,x+dx) not in blist:
                    nextblist.append((y+dy,x+dx))
                    blist.append((y+dy,x+dx))

    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [ list(next(tokens)) for _ in range(W) ]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import copy
import collections


def solve(H: int, W: int, A: "List[str]"):

    blist = []
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                blist.append((i,j))

    nextblist = collections.deque(blist)
    res = 0
    while len(blist) < H*W:
        cnt = len(nextblist)
        res += 1
        for _ in range(cnt):
            y,x = nextblist.pop()
            for dy,dx in [(1,0),(-1,0),(0,1),(0,-1)]:
                if 0 <= y+dy < H and 0 <= x+dx < W and (y+dy,x+dx) not in blist:
                    nextblist.append((y+dy,x+dx))
                    blist.append((y+dy,x+dx))

    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [ list(next(tokens)) for _ in range(W) ]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import copy
import collections


def solve(H: int, W: int, A: "List[str]"):

    blist = []
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                blist.append((i,j))

    nextblist = collections.deque(blist)
    res = 0
    while len(blist) < H*W:
        cnt = len(nextblist)
        res += 1
        for i in range(cnt):
            x,y = nextblist.pop()
            for dy,dx in [(1,0),(-1,0),(0,1),(0,-1)]:
                if 0 <= y+dy < H and 0 <= x+dx < W and (y+dy,x+dx) not in blist:
                    nextblist.append((y+dy,x+dx))
                    blist.append((y+dy,x+dx))

    print(res)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [ list(next(tokens)) for _ in range(W) ]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import copy


def solve(H: int, W: int, A: "List[str]"):

    bNewBlack = True
    res = 0
    while bNewBlack:
        bNewBlack = False
        newDic = {}
        for i in range(H):
            for j in range(W):
                if A[i][j] == "#" and (i,j) not in newDic:
                    for dx,dy in [(1,0),(-1,0),(0,1),(0,-1)]:
                        if 0 <= i+dy < H and 0 <= j+dx < W:
                            if A[i+dy][j+dx] != "#":
                                A[i+dy][j+dx] = "#"
                                newDic[i+dy,j+dx] = True
                                bNewBlack = True
        if bNewBlack:
            res += 1

    print(res)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [ list(next(tokens)) for _ in range(W) ]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import copy


def solve(H: int, W: int, A: "List[str]"):

    bNewBlack = True
    res = 0
    while bNewBlack:
        bNewBlack = False
        Atemp = copy.deepcopy(A)
        for i in range(H):
            for j in range(W):
                if A[i][j] == "#":
                    for dx,dy in [(1,0),(-1,0),(0,1),(0,-1)]:
                        if 0 <= i+dy < H and 0 <= j+dx < W:
                            if A[i+dy][j+dx] != "#":
                                Atemp[i+dy][j+dx] = "#"
                                bNewBlack = True
        A = Atemp
        if bNewBlack:
            res += 1

    print(res)

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [ list(next(tokens)) for _ in range(W) ]  # type: "List[str]"
    solve(H, W, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np
from scipy.spatial.distance import cityblock
B = []

H,W = map(int,input().split())
for row in range(H):
    Inputs = input().split()
    for column in [i for i, x in enumerate(Inputs) if x == '#']:
        B.append((column,row))
OO = [cityblock((0,0),b) for b in B]
OI = [cityblock((0,W-1),b) for b in B]
IO = [cityblock((H-1,0),b) for b in B]
II = [cityblock((H-1,W-1),b) for b in B]
print(min(min(OO),min(OI),min(IO),min(II)))

Submission status: Runtime Error

Solution code:
import numpy as np
from scipy.spatial.distance import cityblock
H,W = map(int,input().split())
for row in range(H):
    Inputs = input().split(',')
    for column in [i for i, x in enumerate(Inputs) if x == '#']:
        B.append((column,row))
for b in B:
    OO.apppend(cityblock((0,0),b))
    OI.apppend(cityblock((H-1,0),b))
    IO.apppend(cityblock((0,W-1),b))
    II.append(cityblock(H-1,W-1,b))
print(min(min(OO),min(OI),min(IO),min(II)))

Submission status: Runtime Error

Solution code:
def pixel_input(H,W,border="."):
    pixel = [[border]*(W+2)] + \
    [[border] + list(input()) + [border] for _ in range(H)] + \
    [[border]*(W+2)]
    return pixel
  
H,W=map(int,input().split())
A=pixel_input(H,W)

cnt=0
q,r=[],[]
for i in range(1,H+1):
    for j in range(1,W+1):
        if A[i][j]=="#":
            q.append((i,j))
if len(q)==H*W:
    print(0)
    sys.exit()
while(q):
    while(q):
        i,j=q.pop()
        if A[i-1][j]==".":
            if i-1!=0:
                A[i-1][j]="#"
                r.append((i-1,j))
        if A[i+1][j]==".":
            if i+1!=H+1:
                A[i+1][j]="#"
                r.append((i+1,j))
        if A[i][j-1]==".":
            if j-1!=0:
                A[i][j-1]="#"
                r.append((i,j-1))
        if A[i][j+1]==".":
            if j+1!=W+1:
                A[i][j+1]="#"
                r.append((i,j+1))
    q=[i for i in r]
    r=[]
    cnt+=1
print(cnt-1)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

h, w = map(int, input().split())
map = [input() for _ in range(h)]
mapB = np.zeros((h, w))
for i, ii in enumerate(map):
  for j, jj in enumerate(ii):
    if jj == '#':
      mapB[i, j] = 1
map = copy.deepcopy(mapB)
      
count = 0
done = False

while not done:
  count +=1
  for i in range(w):
    for j in range(h):
      if map[i, j] >= 1:
          mapB[max(0, i-1), j] = 1 
          mapB[i, max(0, j-1)] = 1 
          mapB[min(h-1, i+1), j] = 1 
          mapB[i, min(w-1, j+1)] = 1 

  if mapB.all():
    done = True
    break
  
  map = map + mapB
  
print(count)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
import numpy as np
s = sys.stdin.readlines()
N, W = map(int, s[0].split())
matrix = np.zeros((N,N),dtype=np.int)
for idx, iterator in enumerate(s[1:]):
    W_list = iterator
    for w_idx, w_iterator in enumerate(W_list):
        if w_iterator == "#":
            matrix[idx][w_idx] = 1
ch_matrix = matrix.copy()
w_index_list = np.where(matrix == 0)
b_index_list = np.where(matrix == 1)
max_of_min_dist = 0
for i in range(len(w_index_list[0])):
    min_dist = N * N
    for j in range(len(b_index_list[0])):
        dist = abs(w_index_list[0][i] - b_index_list[0][j]) + abs(w_index_list[1][i] - b_index_list[1][j])
        if min_dist > dist:
            min_dist = dist
    if max_of_min_dist < min_dist:
        max_of_min_dist = min_dist
print(max_of_min_dist)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
import numpy as np
s = sys.stdin.readlines()
N, W = map(int, s[0].split())
matrix = np.zeros((N,N),dtype=np.int)
for idx, iterator in enumerate(s[1:]):
    W_list = iterator
    for w_idx, w_iterator in enumerate(W_list):
        if w_iterator == "#":
            matrix[idx][w_idx] = 1
ch_matrix = matrix.copy()
for i in range(N * 2 + 1):
    zero_index_list = np.where(matrix == 0)
    if len(zero_index_list[0]) == 0:
        print(i)
        break
    for i in range(len(zero_index_list[0])):
        row = zero_index_list[0][i]
        col = zero_index_list[1][i]
        bw_checker = 0
        if row != 0:
            bw_checker += matrix[row - 1][col]
        if row != N - 1:
            bw_checker += matrix[row + 1][col]
        if col != 0:
            bw_checker += matrix[row][col - 1]
        if col != W - 1:
            bw_checker += matrix[row][col + 1]
        if bw_checker > 0:
            ch_matrix[row][col] = 1
    matrix = ch_matrix.copy()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
import numpy as np
s = sys.stdin.readlines()
N, W = map(int, s[0].split())
matrix = np.zeros((N,N),dtype=np.int)
for idx, iterator in enumerate(s[1:]):
    W_list = iterator
    for w_idx, w_iterator in enumerate(W_list):
        if w_iterator == "#":
            matrix[idx][w_idx] = 1
ch_matrix = matrix.copy()
for i in range(N * 2):
    zero_index_list = np.where(matrix == 0)
    if len(zero_index_list[0]) == 0:
        print(i)
        break
    for i in range(len(zero_index_list[0])):
        row = zero_index_list[0][i]
        col = zero_index_list[1][i]
        bw_checker = 0
        if row != 0:
            bw_checker += matrix[row - 1][col]
        if row != N - 1:
            bw_checker += matrix[row + 1][col]
        if col != 0:
            bw_checker += matrix[row][col - 1]
        if col != W - 1:
            bw_checker += matrix[row][col + 1]
        if bw_checker > 0:
            ch_matrix[row][col] = 1
    matrix = ch_matrix.copy()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
import sys
import numpy as np
s = sys.stdin.readlines()
N, W = map(int, s[0].split())
matrix = np.zeros((N,N),dtype=np.int)
for idx, iterator in enumerate(s[1:]):
    W_list = [x for x in iterator.split()]
    for w_idx in range(W):
        if W == "#":
            matrix[idx, w_idx] = 1
ch_matrix = matrix.copy()
for i in range(N):
    zero_index_list = np.where(matrix == 0)
    if len(zero_index_list[0]) == 0:
        print(i + 1)
        break
    for i in range(len(zero_index_list[0])):
        row = zero_index_list[0][i]
        col = zero_index_list[1][i]
        bw_checker = 0
        if row != 0:
            bw_checker += matrix[row - 1][col]
        if row != N - 1:
            bw_checker += matrix[row + 1][col]
        if col != 0:
            bw_checker += matrix[row][col - 1]
        if col != W - 1:
            bw_checker += matrix[row][col + 1]
        if bw_checker == 1:
            ch_matrix[x][y] = 1
    matrix = ch_matrix.copy()

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.extend([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 True:
    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 + 2):
    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 True:
    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 True:
    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)