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:
h,w = map(int,input().split())
x = [[h+w+1 for _ in range(w)] for _ in range(h)]
y = []
for _ in range(h):
  hi = str(input())
  y.append(hi)
for i in range(h):
  for j in range(w):
    if y[i][j] == "#":
      x[i][j] = 0
def han(i,j):
  if x[i][j] != h+w+1:
    return x[i][j]
  else:
    li = []
    ar.append((i,j))
    for k in [-1,1]:
      if 0 <= i+k and i+k < h and ((i+k,j) not in ar):
        s = x[i+k][j]
        if s != h+w+1:
          li.append(s)
        else:
          li.append(han(i+k,j))
      if 0 <= j+k and j+k < w and ((i,j+k) not in ar):
        s = x[i][j+k]
        if s != h+w+1:
          li.append(s)
        else:
          li.append(han(i,j+k))
    res = min(li) + 1
    x[i][j] = res
    return res
   
for p in range(h):
  for q in range(w):
    ar = []
    x[p][q] = han(p,q)
print(max(x[i][j] for i in range(h) for j in range(w)))

Submission status: Runtime Error

Solution code:


def chng(x,y):
	if(y+1<H and A[x][y+1]=='#'):
		A[x][y] = 'C'
	if(x-1>0 and A[x-1][y]=='#'):
		A[x][y] = 'C'
	if(x+1<W and A[x+1][y]=='#'):
		A[x][y] = 'C'
	if(y-1>0 and A[x][y-1]=='#'):
		A[x][y] = 'C'

def findWhite(A):
	for i in range(H):
		for j in range(W):
			if(A[i][j]=="."):
				return True
	return False

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

A = []
count=0

for i in range(H):
	A.append(list(input()))

while(findWhite(A)):
	count += 1
	for i in range(H):
		for j in range(W):
			if(A[i][j]=="."):
				chng(i,j)
	for i in range(H):
		for j in range(W):
			if(A[i][j]=="C"):
				A[i][j]="#"

print(count)

Submission status: Runtime Error

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

from collections import deque
d = [(1,0),(0,1),(-1,0),(0,-1)]
l = deque()
for i in range(H):
    for j in range(W):
        if a[i][j]=="#":
            l.append((j,i))
            a[i][j] = 0
            while l:
                x,y = l.popleft()
            for o,p in d:
                if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                    continue
                if a[y+p][x+o] == ".":
                    a[y+p][x+o] = a[y][x] + 1
                    l.append((x+i,y+j))
for k in range(3):
    for i in range(H):
        for j in range(W):
            if a[i][j]==k:
                l.append((j,i))
                while l:
                    x,y = l.popleft()
                for o,p in d:
                    if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                        continue
                    if a[y+p][x+o] == ".":
                        a[y+p][x+o] = a[y][x] + 1
                        l.append((x+i,y+j))

aa=[flatten for inner in a for flatten in inner]
print(max(aa))

Submission status: Runtime Error

Solution code:
import sys
import copy

if __name__ == '__main__':
    H, W = map(lambda x:int(x), sys.stdin.readline().split())
    # array = [[0 for x in range(W)] for y in range(H)]
    array = []
    for i in range(H):
        array += [list(sys.stdin.readline().rstrip())]
    # print(array)

    count = 0
    while True:
        # print(array)
        new_array = copy.deepcopy(array)
        flag = True
        for y in range(H):
            for x in range(W):
                if array[x][y] == '#':
                    if x - 1 >= 0:
                        new_array[x-1][y] = '#'
                    if x + 1 < W:
                        new_array[x+1][y] = '#'
                    if y - 1 >= 0:
                        new_array[x][y-1] = '#'
                    if y + 1 < H:
                        new_array[x][y+1] = '#'
                else:
                    if flag:
                        flag = False
                        count += 1
        if flag:
            break
        array = new_array
    print(count)

Submission status: Runtime Error

Solution code:
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

flag = 0
hidari = sc
migi = sc
ue = sr
shita = sr
for i in range(N):
  if S[i] == 'L':
    hidari -= 1
  if S[i] == 'R':
    migi += 1
  if S[i] == 'U':
    ue -= 1
  if S[i] == 'D':
    shita += 1
  if hidari == 0 or migi == W+1 or ue == 0 or shita == H+1 :
    flag = 1
  
  if T[i] == 'R' and hidari != W:
    hidari += 1
  if T[i] == 'L' and migi != 1:
    migi -= 1
  if T[i] == 'D' and ue != 1:
    ue += 1
  if T[i] == 'U' and shita != H:
    shita -= 1

if flag :
  print("NO")
else :
  print("YES")

Submission status: Runtime Error

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

from collections import deque
d = [(1,0),(0,1),(-1,0),(0,-1)]
l = deque()
for i in range(H):
    for j in range(W):
        if a[i][j]=="#":
            l.append((j,i))
            a[i][j] = 0
            while l:
                x,y = l.popleft()
            for o,p in d:
                if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                    continue
                if a[y+p][x+o] == ".":
                    a[y+p][x+o] = a[y][x] + 1
                    l.append((x+i,y+j))
for k in range(5):
    for i in range(H):
        for j in range(W):
            if a[i][j]==k:
                l.append((j,i))
                while l:
                    x,y = l.popleft()
                for o,p in d:
                    if y+p<0 or y+p>=H or x+o<0 or x+o>=W:
                        continue
                    if a[y+p][x+o] == ".":
                        a[y+p][x+o] = a[y][x] + 1
                        l.append((x+i,y+j))

aa=[flatten for inner in a for flatten in inner]
print(max(aa))

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = list(map(int, input().split()))
env = []
for i in range(H):
    tmp = []
    for j in input():
        if j == '#':
            tmp.append(0)
        else:
            tmp.append(1)
    env.append(tmp)
    tmp = []
env = np.array(env)


ans = 0
while True:
    pos_black = np.where(env == 0)
    
    if len(pos_black[0]) == H*W:
        break
    else:
        ans += 1
    
    if len(pos_black[0]) == 1:
        h, w = pos_black
        h = h.item()
        w = w.item()
        if h-1 >= 0:
            env[h-1][w] = 0
        if h+1 <= H:
            env[h+1][w] = 0
        if w-1 >= 0:
            env[h][w-1] = 0
        if w+1 <= W:
            env[h][w+1] = 0

    else:
        for h, w in zip(pos_black[0], pos_black[1]):
            if h-1 >= 0:
                env[h-1][w] = 0
            if h+1 <= H-1:
                env[h+1][w] = 0
            if w-1 >= 0:
                env[h][w-1] = 0
            if w+1 <= W-1:
                env[h][w+1] = 0
                
print(ans)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(w)]
t = []

for i in range(h):
        for j in range(w):
                if s[i][j] == "#":
                        t.append([i,j])

p = 10 **18
q = []
for i in range(h):
        for j in range(w):
                if s[i][j] == "#":
                        continue
                else:
                        p = 10 **18
                        for k in range(len(t)):
                                if p > abs(i-t[k][0]) + abs(j-t[k][1]):
                                        p = abs(i-t[k][0]) + abs(j-t[k][1])
                        q.append(p)

if q == []:
        print(0)
else:
        print(max(q))

Submission status: Runtime Error

Solution code:
import queue


def darker_and_darker(H: int, W: int, A: list)->int:
    q = queue.PriorityQueue()
    d = [[float('inf')] * H for _ in range(W)]
    count = 0

    for h in range(H):
        for w in range(W):
            if A[h][w] == '#':
                q.put((0, h, w))
                # count += 1

    max_d = 0
    while count < H*W:
        if q.empty():
            raise Exception('empty')

        c, h, w = q.get()

        if d[h][w] <= c:
            continue

        # print(h, w, c)
        d[h][w] = c
        max_d = max(max_d, c)
        count += 1
        # print('count:', count)

        for dh, dw in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nh, nw = h+dh, w+dw

            if nh < 0 or H <= nh:
                continue
            if nw < 0 or W <= nw:
                continue

            q.put((c+1, nh, nw))

    return max_d


if __name__ == "__main__":
    H = 0
    H, W = map(int, input().split())
    A = [input() for _ in range(H)]

    ans = darker_and_darker(H, W, A)
    print(ans)

Submission status: Runtime Error

Solution code:
a=input().split()
n = int(a[0])
m = int(a[1])
white=[]
black=[]
for i in range(n):
    for j in range(m):
        if a[i+2][j] == '.':
            white.append([i,j])
        else:
            black.append([i,j])

min_d=[]
max_d=[]
l_white =  len(white)
l_black = len(black)
for i in range(l_white):
    min_d=[]
    for j in range(l_black):
        min_d.append(abs(white[i][0] - black[j][0]) + abs(white[i][1] - black[j][1]))
    max_d.append(min(min_d))

print(max(max_d))

Submission status: Runtime Error

Solution code:
from collections import deque
h,w = map(int, input().split())
a = [list(input()) for _ in range(h)]
d = [(0,1),(1,0),(0,-1),(-1,0)]
q = deque()
cnt = h*w
t = 0
for i in range(h):
  for j in range(w):
    if a[i][j] == "#":
      q.append((i,j))
      a[i][j] = 0
      cnt -= 1
for _ in range(cnt):
  y,x = q.popleft()
  for dx,dy in d:
    if 0 <= x+dx < w and 0 <= y+dy < h and a[y+dy][x+dx] == ".":
      a[y+dy][x+dx] = a[y][x] + 1
      q.append((y+dy,x+dx)) 
for i in range(h):
  m = max(a[i])
  t = max(t,m)
print(t)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
c_list = list()
for i in range(H):
  b_list = list(map(lambda x: x, input()))
  for w in range(W):
    if b_list[w] == '#':
      c_list.append([i, w])

ans_ll = []

for h in range(H):
  for w in range(W):
    if not [h, w] in c_list:
      ans_list = list()
      for c in c_list:
        ans_list.append(abs(h-c[0]) + abs(w-c[1]))
      ans_ll.append(min(ans_list))
print(max(ans_ll))

Submission status: Runtime Error

Solution code:
h, w = list(map(int, input().split()))
s = [list(input()) for i in range(h)]

import numpy as np

black_sum_list = []
white_sum_list = []
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            black_sum_list.append(i + j)
        else:
            white_sum_list.append(i + j)

black_sum_list = np.array(black_sum_list)
white_sum_list = np.array(white_sum_list)

tmp = []
for k in range(len(white_sum_list)):
    tmp.append(np.min(np.abs(black_sum_list - white_sum_list[k])))

print(max(tmp))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
cells = [['.'] * W for _ in range(H)]
dist = [[9999] * W for _ in range(H)]
stack = []

for i in range(H):
    line = input()
    for j in range(W):
        s = line[j]
        if s == '#':
            stack.append((i, j))
            dist[i][j] = 0
            cells[i][j] = s


while len(stack) != 0:
    y, x = stack.pop(0)
    dist_count = dist[x][y] + 1
    if x < W - 1 and dist[x+1][y] > dist_count:
        dist[x+1][y] = dist_count
        stack.append((x+1, y))
    if y < H - 1 and dist[x][y+1] > dist_count:
        dist[x][y+1] = dist_count
        stack.append((x, y+1))
    if 0 < x and dist[x-1][y] > dist_count:
        dist[x-1][y] = dist_count
        stack.append((x-1, y))
    if 0 < y and dist[x][y-1] > dist_count:
        dist[x][y-1] = dist_count
        stack.append((x, y-1))

ans = 0
for i in range(H):
    ans = max(ans, max(dist[i]))
print(ans)

Submission status: Runtime Error

Solution code:
(H, W) = map(int, input().split())
A = []
for i in range(H):
    A.append(input())


def fillDistance(d, h, w, cd):
    if h < 0 or H <= h or w < 0 or W <= w:
        return
    if cd < d[h][w]:
        d[h][w] = cd
        fillDistance(d, h-1, w, cd+1)
        fillDistance(d, h+1, w, cd+1)
        fillDistance(d, h, w-1, cd+1)
        fillDistance(d, h, w+1, cd+1)



d = [[10000 for i in range(W)] for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            fillDistance(d, h, w, 0)
ans = -1
for h in range(H):
    for w in range(W):
        if d[h][w] > ans:
            ans = d[h][w]
print(ans)

Submission status: Runtime Error

Solution code:
def main():
  dep, wid = map(int, input().split())
  a[][]
  for i in range(dep):
    s = input()
    a.append([])
    for in j in range(s):
      a[i].append(s[j])
  
  result = 0
  while True:
    flg = 0
    for i in range(dep):
      for j in range(wid):
        if i > 0 and a[i-1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif i < dep-1 and a[i+1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j > 0 and a[i][j-1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j < wid-1 and a[i][j+1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
    
    if(flg == 1):
      result += 1
    else:
      break
      
  print(result)
  
main()

Submission status: Runtime Error

Solution code:
H, W = [int(n) for n in input().split()]
F = H*W

A = []
B = 0
for _ in range(H):
    A_ = list(input())
    A.append(A_)
    B += A_.count('#')

S = [[False for _ in range(W)] for _ in range(H)]
will = [[False for _ in range(W)] for _ in range(H)]

def search(i, j):
    global B
    S[i][j] = True
    if A[i][j] == '.':
        will[i][j] = True
        B += 1
        return
    if i > 0 and not S[i - 1][j]:
        search(i - 1, j)
    if i < H - 1 and not S[i + 1][j]:
        search(i + 1, j)
    if j > 0 and not S[i][j - 1]:
        search(i, j - 1)
    if j < W - 1 and not S[i][j + 1]:
        search(i, j + 1)

def swap():
    for i in range(H):
        for j in range(W):
            if will[i][j]:
                A[i][j] = '#'
                will[i][j] = False
                S[i][j] = False

C = 0

while B < F:
    C += 1
    for i in range(H):
        for j in range(W):
            if A[i][j] == '#' and not S[i][j]:
                search(i, j)
    swap()

print(C)

Submission status: Runtime Error

Solution code:
import sys

H, W = map(int, sys.stdin.readline().split())

A = []
for i in range(H):
    A.append(sys.stdin.readline().rstrip())

black_i = []
black_j = []

for i in range(W):
    for j in range(H):
        if A[i][j] == '#':
            black_i.append(i)
            black_j.append(j)

min_distance = [[H+W] * W for i in range(H)]

for i in range(len(black_i)):
    temp_i = black_i[i]
    temp_j = black_j[i]
    for j in range(H):
        for k in range(W):
            length = abs(j - temp_i) + abs(k - temp_j)
            if min_distance[j][k] > length:
                min_distance[j][k] = length

max = 0
for i in range(H):
    for j in range(W):
        if max < min_distance[i][j]:
            max = min_distance[i][j]
print(max)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

import numpy as np

sys.setrecursionlimit(10000)
INF = float('inf')

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

flags = np.zeros((H, W))


# 一番近い # の場所までの数
@lru_cache(maxsize=None)
def count(h, w):
    if not (0 <= h < H and 0 <= w < W and flags[h][w] == 0):
        return INF
    if A[h][w] == '#':
        return 0

    flags[h][w] = 1
    ret = min(
        count(h - 1, w) + 1,
        count(h + 1, w) + 1,
        count(h, w + 1) + 1,
        count(h, w - 1) + 1,
    )
    flags[h][w] = 0
    return ret


ans = -INF
for h in range(H):
    for w in range(W):
        ans = max(ans, count(h, w))
print(ans)

Submission status: Runtime Error

Solution code:
from heapq import*
import sys;sys.setrecursionlimit(200000)
h,w=map(int,input().split())
grid=[["#"]*(w+2)]+[list("#"+input()+"#") for x in range(h)]+[["#"]*(w+2)]

g=0
x=1
y=1
lock=[]
while 1:
    for x in range(1,h+1):
        for y in range(1,w+1):
            if (y,x) in lock:
                continue
            if grid[y][x]=='#':
                for i,j in([0,1],[1,0],[0,-1],[-1,0]):
                    if grid[y+i][x+j]!="#":
                        grid[y+i][x+j]="#"
                        heappush(lock,(y+i,x+j))
    g+=1
    lock=[]
#    for i in grid:
#        print(*i)
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Submission status: Runtime Error

Solution code:
from fractions import gcd
N = int(input())
A = [int(i) for i in input().split()]
L = [A[0]] * N
R = [A[-1]] * N
for i, a in enumerate(A[1:]):
    L[i+1] = gcd(a, L[i])
for i, a in enumerate(A[::-1][1:]):
    R[i+1] = gcd(a, R[i])
L = [0] + L
R = R[::-1] + [0]
ans = 0
for i in range(N):
    ans = max(ans, gcd(L[i], R[i+1]))
print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
e = [[input() for i in range(W)]for c in range(H)]
a = []
b = []
for i in range(W):
  for c in range(H):
    if c =="#":
      a.append(int(c+i))
for x in range(a):
  for y in range(H*W):
    b.append(abs(a[x]-y))
print(max(b))

Submission status: Runtime Error

Solution code:
import copy
H, W = [int(i) for i in input().split()]
M = []
for i in range(H):
  M.append(input())
int_M = [[0 for i in range(H)] for j in range(W)]
for i in range(H):
  for j in range(W):
    if M[i][j]=='#':
      int_M[i][j]=1
    else:
      int_M[i][j]=0
M = int_M

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

NM = copy.deepcopy(M)
s = 0
ans = 0

while (sum(map(sum, NM))!=H*W):
  for i in range(H):
    for j in range(W):
      if M[i][j]==1:
        for di,dj in move:
          ni = i+di
          ny = j+dj
          if (ni>=0 and ni < H and ny>=0 and ny<W):
            NM[ni][ny]=1
  ans += 1
  M=copy.deepcopy(NM)
print (ans)

Submission status: Runtime Error

Solution code:
def main():
  dep, wid = map(int, input().split())
  a = [[""] * wid] * dep
  for i in range(dep):
    s = input()
    for in j in range(s):
      a[i][j] = s[j]
  
  result = 0
  while True:
    flg = 0
    for i in range(dep):
      for j in range(wid):
        if i > 0 and a[i-1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif i < dep-1 and a[i+1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j > 0 and a[i][j-1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j < wid-1 and a[i][j+1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
    
    if(flg == 1):
      result += 1
    else:
      break
      
  print(result)
  
main()

Submission status: Runtime Error

Solution code:
import numpy as np
H,W =map(int ,raw_input().split())
l =[]
for i in range(H):
    l.append(map(str ,raw_input().split()))
    
def distance(i,j,l):
    if l[i][j] == '#':
        return 1
    elif l[i][j] == '.':
        return 1000
    else:
        return l[i][j]+1
flag =1
Max = 0
while flag == 1:
    flag = 0
    for i in range(H):
        for j in range(W):
            if l[i][j] == '.':
                s = 1000
                if i>0:
                    k = distance(i-1,j,l)
                    if k < s:
                        s =k
                if i<H-1:
                    k = distance(i+1,j,l)
                    if k < s:
                        s =k
                if j>0:
                    k = distance(i,j-1,l)
                    if k < s:
                        s =k
                if j<W-1:
                    k = distance(i,j+1,l)
                    if k < s:
                        s =k
                if s == 1000:
                    flag =1
                else:
                    l[i][j] = s
                    if Max < s:
                        Max = s
print str(s)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = map(int, input().split())
b = []
for _ in range(W):
    b.append(list(map(int, input().replace('.', '0').replace('#', '1'))))
a = np.array(b)
num = 0

while (a == 0).any():
    ne = a.copy()
    ne[1:, :] += a[:-1, :]
    ne[:-1, :] += a[1:, :]
    ne[:, 1:] += a[:, :-1]
    ne[:, :-1] += a[:, 1:]
    num += 1
    a = ne

print(num)

Submission status: Runtime Error

Solution code:
h, w = list(map(int, input().split()))
a = []
for _ in range(h):
    a.append(input())

blacks = []
whites = []
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            blacks.append((i, j))
        else:
            whites.append((i, j))
removes = []
for (x, y) in blacks:
    if (x-1, y) in blacks and (x+1, y) in blacks and (x, y-1) in blacks and (x, y+1) in blacks:
        removes.append((x, y))
blacks = list(set(blacks) - set(removes))

dists = []
for (x, y) in whites:
    dists.append(min([abs(x-i) + abs(y-j) for (i, j) in blacks]))
print(max(dists))

Submission status: Runtime Error

Solution code:
def main():
  dep, wid = map(int, input().split())
  a = [[0] * wid] * dep
  for i in range(dep):
    s = input()
    for in j in range(wid):
      a[i][j] = s[j]
  
  result = 0
  while True:
    flg = 0
    for i in range(dep):
      for j in range(wid):
        if i > 0 and a[i-1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif i < dep-1 and a[i+1][j] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j > 0 and a[i][j-1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
        elif j < wid-1 and a[i][j+1] == "#" and a[i][j] == ".":
          a[i][j] = "#"
          flg = 1
    
    if(flg == 1):
      result += 1
    else:
      break
      
  print(result)
  
main()

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
A = [input() for _ in range(H)]
B = [[-1]*W for _ in range(H)]
bp = set()
for i, a in enumerate(A):
    for j, e in enumerate(a):
        if e == '#':
            bp.add((i, j))

ans = 0
for i, a in enumerate(A):
    for j, e in enumerate(a):
        if e == '.':
            d = 1000*1000
            for p in bp:
                d = min(d, abs(i-p[0])+abs(j-p[1]))
        ans = max(ans, d)
print(ans)

Submission status: Runtime Error

Solution code:
#https://atcoder.jp/contests/agc033
# -*- coding: utf-8 -*-
from copy import deepcopy

h,w = map(int,input().split())
a = []
for i in range(h):
        a.append( list(input()) )

flag = 1
counter = 0
while(flag is 1):
        counter += 1
        flag = 0
        newa = deepcopy(a)
        for i,v in enumerate(a):
                for j,x in enumerate(v):
                        if x is ".":
                                if i is not 0:
                                        if a[i-1][j] is "#":
                                                newa[i][j] = "#"
                                                flag = 1
                                if i is not h-1:
                                        if a[i+1][j] is "#":
                                                newa[i][j] = "#"
                                                flag = 1
                                if j is not 0:
                                        if a[i][j-1] is "#":
                                                newa[i][j] = "#"
                                                flag = 1
                                if j is not w-1:
                                        if a[i][j+1] is "#":
                                                newa[i][j] = "#"
                                                flag = 1
        a = deepcopy(newa)
print(counter-1)

Submission status: Runtime Error

Solution code:
H, W = list(map(int, input().split()))
A = []
for i in range(H):
    A.append(list(input().split()[0]))

import numpy as np
A = np.array(A)

cnt = 0
flag = False

while not flag:
    indexes = np.where(A == '#')
    for i in range(len(indexes[0])):
        index = [indexes[0][i], indexes[1][i]]
        if index[0] == 0:
            if index[1] == 0:
                A[index[0] + 1, index[1]] = '#'
                A[index[0], index[1] + 1] = '#'
            elif index[1] == W - 1:
                A[index[0] + 1, index[1]] = '#'
                A[index[0], index[1] - 1] = '#'
            else:
                A[index[0] + 1, index[1]] = '#'
                A[index[0], index[1] - 1] = '#'
                A[index[0], index[1] + 1] = '#'
        elif index[1] == 0:
            if index[0] == H - 1:
                A[index[0] - 1, index[1]] = '#'
                A[index[0], index[1] + 1] = '#'
            else:
                A[index[0] - 1, index[1]] = '#'
                A[index[0], index[1] + 1] = '#'
                A[index[0] + 1, index[1]] = '#'
        elif index[1] == W - 1:
            if index[0] == H - 1:
                A[index[0] - 1, index[1]] = '#'
                A[index[0], index[1] - 1] = '#'
            else:
                A[index[0] - 1, index[1]] = '#'
                A[index[0], index[1] - 1] = '#'
                A[index[0] + 1, index[1]] = '#'
        elif index[0] == H - 1:
            A[index[0], index[1] - 1] = '#'
            A[index[0], index[1] + 1] = '#'
            A[index[0] - 1, index[1]] = '#'
        else:
            A[index[0], index[1] - 1] = '#'
            A[index[0], index[1] + 1] = '#'
            A[index[0] - 1, index[1]] = '#'
            A[index[0] + 1, index[1]] = '#'
    cnt += 1
    flag = all([all(A[i] == '#') for i in range(A.shape[0])])
print(cnt)

Submission status: Runtime Error

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

l=[]
for i in range(H):
  l.append(list(input()))

c=0
while True:
  c+=1
  for i in range(H):
    for j in range(W):
      x=l[i][j]
      if x=='#':
        if i>0 and i<H-1 and j>0 and j<W-1:
          if l[i-1][j]=='.':
            l[i-1][j]='a'
          if l[i+1][j]=='.':
            l[i+1][j]='a'
          if l[i][j-1]=='.':
            l[i][j-1]='a'
          if l[i][j+1]=='.':
            l[i][j+1]='a'
  
        if i==0 and j==0:
          if l[i][j+1]=='.':
            l[i][j+1]='a'
          if l[i+1][j]=='.':
            l[i+1][j]='a'
  
        if i==0 and j>0 and j<W-1:
          if l[i+1][j]=='.':
            l[i+1][j]='a'
          if l[i][j-1]=='.':
            l[i][j-1]='a'
          if l[i][j+1]=='.':
            l[i][j+1]='a'
  
        if i==0 and j==W-1:
          if l[i][j-1]=='.':
            l[i][j-1]='a'
          if l[i+1][j]=='.':
            l[i+1][j]='a'
  
        if i>0 and i<H-1 and j==0:
          if l[i-1][j]=='.':
            l[i-1][j]='a'
          if l[i][j+1]=='.':
            l[i][j+1]='a'
          if l[i+1][j]=='.':
            l[i+1][j]='a'
  
        if i>0 and i<H-1 and j==W-1:
          if l[i-1][j]=='.':
            l[i-1][j]='a'
          if l[i][j-1]=='.':
            l[i][j-1]='a'
          if l[i+1][j]=='.':
            l[i+1][j]='a'
  
        if i==H-1 and j==0:
          if l[i-1][j]=='.':
            l[i-1][j]='a'
          if l[i][j+1]=='.':
            l[i][j+1]='a'
  
        if i==H-1 and j>0 and j<W-1:
          if l[i-1][j]=='.':
            l[i-1][j]='a'
          if l[i][j-1]=='.':
            l[i][j-1]='a'
          if l[i][j+1]=='.':
            l[i][j+1]='a'
  
        if i==H-1 and j==W-1:
          if l[i-1][j]=='.':
            l[i-1][j]='a'
          if l[i][j-1]=='.':
            l[i][j-1]='a'
  
  f=False
  for i in range(H):
    if 'a' not in l[i]:
      f=True
    else:
      l[i]=list(''.join(l[i]).replace('a','#'))

  if f:
    break

print(c)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = list(map(int, input().split()))
env = []
for i in range(H):
    tmp = []
    for j in input():
        if j == '#':
            tmp.append(0)
        else:
            tmp.append(1)
    env.append(tmp)
    tmp = []
env = np.array(env)

ans = 0
while True:
    
    if len(np.where(env == 0)[0]) == H*W:
        break
    else:
        ans += 1
    
    if len(np.where(env == 0)[0]) == 1:
        h, w = np.where(env == 0)
        h = h.item()
        w = w.item()
        if h-1 >= 0:
            env[h-1][w] = 0
        if h+1 <= H:
            env[h+1][w] = 0
        if w-1 >= 0:
            env[h][w-1] = 0
        if w+1 <= W:
            env[h][w+1] = 0

    else:
        for h, w in zip(np.where(env == 0)[0], np.where(env == 0)[1]):
            if h-1 >= 0:
                env[h-1][w] = 0
            if h+1 <= H-1:
                env[h+1][w] = 0
            if w-1 >= 0:
                env[h][w-1] = 0
            if w+1 <= W-1:
                env[h][w+1] = 0
                
print(ans)

Submission status: Runtime Error

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

i = 0
checked = set()
from collections import deque
q = deque()

def is_valid(h, w):
    return 0 <= h < H and 0 <= w < W

def step(h, w):
    for nh, nw in ((h-1,w), (h+1,w), (h,w-1), (h,w+1)):
        if is_valid(nh, nw) and (nh, nw) not in checked and A[nh][nw]=='.':
            checked.add((nh, nw))
            q.append((nh, nw))

for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            checked.add((h, w))
            q.append((h, w))

ans = -1
while len(q) > 0:
    ans += 1
    for _ in range(len(q)):
        step(*q.popleft())

print(ans)

Submission status: Runtime Error

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

bl = []
wh = []
for i in range(h):
    s = input()
    for j in range(w):
        if s[j] == '#':
            bl.append([i,j])
        else:
            wh.append([i,j])

mx = []
for i in wh:
    mn = [abs(j[0]-i[0])+abs(j[1]-i[1]) for j in bl]
    mx.append(min(mn))
print(max(mx))

Submission status: Runtime Error

Solution code:
import sys

H,W = map(int,input().split())    
A = [0]*H
for i in range(H):
  A[i] = input()

def L1(x1,y1,x2,y2):
  return abs(x1-x2)+abs(y1-y2)

b_ij = []
for i in range(H):
  for j in range(W):
    if A[i][j] == '#':
      b_ij.append({'x': i, 'y': j})
l = len(b_ij)
m_list = []
for i in range(H):
  for j in range(W):
    if A[i][j] == '.':
      m = sys.maxsize
      for k in range(l):
        m = min(m,L1(b_ij[k].get('x'),b_ij[k].get('y'),i,j))
      m_list.append(m)
print(max(m_list))