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

from collections import deque

h, w = [int(x) for x in input().split()]

i_mat = []

queue = deque()


for i in range(h):
    x = input()
    i_mat.append([0 if y == "#" else 99999 for y in list(x)])

    for (j, t) in enumerate(i_mat[-1]):
        if t == 0:
            queue.append((i, j))


while queue:
    index = queue.popleft()
    curr = i_mat[index[0]][index[1]]

    # ←
    if index[0]-1 >= 0 and i_mat[index[0]-1][index[1]] > curr + 1:
        i_mat[index[0]-1][index[1]] = curr + 1
        queue.append((index[0]-1, index[1]))

    # ↑
    if index[1]-1 >= 0 and i_mat[index[0]][index[1]-1] > curr + 1:
        i_mat[index[0]][index[1]-1] = curr + 1
        queue.append((index[0], index[1]-1))

    # →
    if index[0]+1 < w and i_mat[index[0]+1][index[1]] > curr + 1:
        i_mat[index[0]+1][index[1]] = curr + 1
        queue.append((index[0]+1, index[1]))
    # ↓
    if index[1]+1 < h and i_mat[index[0]][index[1]+1] > curr + 1:
        i_mat[index[0]][index[1]+1] = curr + 1
        queue.append((index[0], index[1]+1))

result = 0

for data in i_mat:
    if result < max(data):
        result = max(data)

print(result)

Submission status: Runtime Error

Solution code:
for m in masu:
	if '.' in m:
		for i in range(H):
			if __name__ == "__main__":
				index_num = [n for n, v in enumerate(masu[i]) if v == '#']
				list_index_num.append(index_num)

			def index_Multi(List,liter):
              index_L = []
              dor val in range(0,len(List)):
                if liter == List[val]:
                  index_L.append(val)
              return inde
            
            
                
                
                
                print(list_index_num)
		for x in list_index_num:
			for y in x:
				if a>=1 and y-1>=0:
					masu[a][y-1] = '#'
				if a>=1:
					masu[a][y] = '#'
				if a>=1 and y+1<W:
					masu[a][y+1] = '#'
				if a>0:
					masu[a+1][y-1] = '#'
				if y+1<W:
					masu[a+1][y+1] = '#'
				if a+1<H and y-1>=0:
					masu[a+2][y-1] = '#'
				if a+1<H:
					masu[a+2][y] = '#'
				if a+1<H and y+1<W:
					masu[a+2][y+1] = '#'
		a+=1  
		list_index_num = []
print(a)

Submission status: Runtime Error

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




S_power = [[0, 0, 0, 0] for i in range(N+1)] #L, R, U, D
for i in range(N):
    if S[i] == "L":
        S_power[i][0] += 1
    elif S[i] == "R":
        S_power[i][1] += 1
    elif S[i] == "U":
        S_power[i][2] += 1
    else:
        S_power[i][3] += 1

T_power = [[0, 0, 0, 0] for i in range(N+1)]
for i in range(N):
    if T[i] == "L":
        T_power[i][0] += 1
    elif T[i] == "R":
        T_power[i][1] += 1
    elif T[i] == "U":
        T_power[i][2] += 1
    else:
        T_power[i][3] += 1

for j in range(4):
    for i in range(N-1):
        S_power[i+1][j] += S_power[i][j]
for j in range(4):
    for i in range(N-1):
        T_power[i+1][j] += T_power[i][j]
T_power.append([0, 0, 0, 0])



flag = 1
power_diff = []
for i in range(N):
    power_diff.append(S_power[i][1]-T_power[i-1][0])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "L":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "R" and power_diff_max[i] + sr + 1 <= W:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][0]-T_power[i-1][1])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "R":
        sr += 1
    if sr > W:
        flag = 0
        break
    if T[i] == "L" and power_diff_max[i] + 1 <= sr:
        sr -= 1







power_diff = []
for i in range(N):
    power_diff.append(S_power[i][2]-T_power[i-1][3])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "U":
        sr -= 1
    if sr <= 0:
        flag = 0
        break
    if T[i] == "D" and power_diff_max[i] + sr + 1 <= H:
        sr += 1






power_diff = []
for i in range(N):
    power_diff.append(S_power[i][3]-T_power[i-1][2])
power_diff_max = power_diff[::-1]
for i in range(N-1):
    power_diff_max[i+1] = max(power_diff_max[i+1], power_diff_max[i])
power_diff_max.reverse()

sr = r
for i in range(N):
    if S[i] == "D":
        sr += 1
    if sr > H:
        flag = 0
        break
    if T[i] == "U" and power_diff_max[i] + 1 <= sr:
        sr -= 1


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

Submission status: Runtime Error

Solution code:
h,w=input().split()
H = int(h)
W = int(w)
A = list()
A_ = list()
for i in range(H):
    A.append([""]*W)
    A_.append([""]*W)
    x = input()
    for j in range(W):
        for k in range(W):
            A[i][j] = x[k]
            A_[i][j] = x[k]
go = True

#黒「#」 白「.」
#「.」を「＃」にする
#ひっくり返し関数
def reverse(A,Pre):
    #Aリバースあと
    #Bリバース前
    for i in range(H):
        for j in range(W):
            if Pre[i][j] == "#":
                if i == 0:
                    if j == 0:
                        A[0][1] = "#"
                        A[1][0] = "#"
                    elif j == W-1:
                        A[0][W-2] = "#"
                        A[1][W-1] = "#"
                    else:
                        A[i][j-1] = "#"
                        A[i][j+1] = "#"
                        A[i+1][j] = "#"
                
                elif i == H-1:
                    if j == 0:
                        A[H-1][1] = "#"
                        A[H-1-1][0] = "#"
                    elif j == W-1:
                        A[H-1-1][W-1] = "#"
                        A[H-1][W-2] = "#"
                    else:
                        A[H-1][j-1] = "#"
                        A[H-1][j+1] = "#"
                        A[H-1-1][j] = "#"
                
                elif i != 0 and i != H-1:
                    if j == 0:
                        A[i][0+1] = "#"
                        A[i-1][0] = "#"
                        A[i+1][0] = "#"
                    elif j == W-1:
                        A[i][W-1-1] = "#"
                        A[i-1][W-1] = "#"
                        A[i+1][W-1] = "#"
                    else:
                        A[i-1][j] = "#"
                        A[i][j-1] = "#"
                        A[i][j+1] = "#"
                        A[i+1][j] = "#"

#全部黒か判定関数
def judge(A):
    global n
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                n += 1
            else:
                pass
    n == H*W

#本体プログラム
from copy import deepcopy
ans = 0
n = 0
while n != H*W:
    n = 0
    ans += 1
    reverse(A_,A)
    #Pre == A
    A = deepcopy(A_)
    judge(A)

print(ans)

Submission status: Runtime Error

Solution code:

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


def change(x, y):
    if x < 0 or x >= W:
        return False
    if y < 0 or y >= H:
        return False

    if G[x][y] == ".":
        G[x][y] = "@"
        return False
    elif G[x][y] == "#":
        return True


def search(x, y):
    global ans
    count = 0
    sharp = 0

    while sharp == 0:
        if count == 0:
            G[x][y] = "@"
        else:
            for i in range(count+1):
                if change(x+count-i, y+1) or change(x-i, y+count-i) or \
                    change(x-count+i, y-i) or change(x+i, y-count+i):
                    sharp += 1

        if count > ans:
            ans = count

        count += 1


for i in range(H):
    for j in range(W):
        if G[i][j] == ".":
            search(i, j)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(700000000)
H,W = map(int, input().split())
A = [list(map(str,list(input()))) for i in range(H)]
def check(l,a,b):
    count = 1
    for k in (-1,1):
        if 0<=b+k<W and l[a][b+k] == "#":
            
            return count
        if 0<=a+k<H and l[a+k][b] == "#":
            
            return count 
        if (0<=a+k<W and 0<=b+k<H and l[a+k][b+k] == "#") or (0<=a+k<W and 0<=b-k<H and l[a+k][b-k] == "#"):
            count += 1
            return count 
       
    memo = []       
    if 0<=a+1<H:
        memo.append(check(l,a+1,b))
    if 0<=b+1<W:
        memo.append(check(l,a,b+1))
    if 0<=a-1<H:
        memo.append(check(l,a-1,b))
    if 0<=b-1<W:
        memo.append(check(l,a,b-1)) 
    count += min(memo)
    return count
c = 0
cmax = 0
for i in range(H):
    for j in range(W):
        if A[i][j] == ".":
            c = check(A,i,j)
            if cmax < c:
                cmax = c
            c = 0            
    
print(cmax)

Submission status: Runtime Error

Solution code:
import sys

H, W = input().split()
H = int(H)
W = int(W)
A = []
for h in range(H):
    A.append(input())
A = ''.join(A)

count = 0

while True:
    A = list(A)
    indexes = [i for i, x in enumerate(A) if x == '#']

    for a in indexes:
        if a < W:
            A[a + W] = '#'
        elif a > (H*W)-W-1:
            A[a - W] = '#'
        else:
            A[a + W] = '#'
            A[a - W] = '#'

        if a%W == 0:
            A[a + 1] = '#'
        elif a%W == W-1:
            A[a - 1] = '#'
        else:
            A[a + 1] = '#'
            A[a - 1] = '#'
        A[a] = 0

    count = count + 1
    A = ''.join(A)

    b = A.find('.')
    if b < 0:
        print(count)
        sys.exit()

 #

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)

N = list(map(int, input().split(" ") ) )
H = N[0]
W = N[1]

A = [list(map(str, input().split())) for i in range(H)] # [['...'], ['.#.'], ['...']]

def count_distance(y, x, distance, from_pos):
    distance_max = distance

    left = BlackList[y][x-1]
    right = BlackList[y][x+1]
    top = BlackList[y-1][x]
    bottom = BlackList[y+1][x]

    if (left == 1 and from_pos != "left" ) \
            or (right == 1 and from_pos != "right" ) \
            or (top == 1 and from_pos != "top" ) \
            or (bottom == 1 and from_pos != "bottom"):
        return distance-1

    if from_pos != "left":
        if left == 0:
            distance_max = max(distance_max, count_distance(y, x-1, distance+1, "right"))
        elif left == 1:
            distance_max = max(distance_max, distance - 1)
        elif left == -1:
            distance_max = max(distance_max, distance -1)

    if from_pos != "right":
        if right == 0:
            distance_max = max(distance_max, count_distance(y, x+1, distance+1, "left"))
        elif right == 1:
            distance_max = max(distance_max, distance - 1)
        elif right == -1:
            distance_max = max(distance_max, distance -1)

    if from_pos != "top":
        if top == 0:
            distance_max = max(distance_max, count_distance(y-1, x, distance+1, "bottom"))
        elif top == 1:
            distance_max = max(distance_max, distance - 1)
        elif top == -1:
            distance_max = max(distance_max, distance -1)

    if from_pos != "bottom":
        if bottom == 0:
            distance_max = max(distance_max, count_distance(y+1, x, distance+1, "top"))
        elif bottom == 1:
            distance_max = max(distance_max, distance - 1)
        elif bottom == -1:
            distance_max = max(distance_max, distance -1)

    return distance_max


BlackList=[[0 for x in range(W+2)]for x in range(H+2)]
for i in range(W):
    for j in range(H):
        cur=A[i][0][j]
        if cur == "#":
            BlackList[i+1][j+1]=1

for i in range(W+2):
    for j in range(H+2):
      if i == 0 or i == W+1 or j == 0 or j == H+1:
        BlackList[i][j] = -1

ans = 0
for i in range(1, W+2):
    for j in range(1, H+2):
        if BlackList[i][j] == 1:
            cur_ans = count_distance(i, j, 0, "start")
            #print(cur_ans)
            ans = max(ans, cur_ans)
            #print(ans)

print(ans)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
masu = [['']*W]*H
next_masu = [['']*W]*H
is_change = False
ans = 0
for i in range(H):
  masu[i] = list(map(str, input().split()))
while True:  
  for i in range(H):
    for j in range(W):
      if masu[i][j] == '.':
        if (i != 0 or i != H-1) and (j != 0 or j != W-1):
          if masu[i-1][j] == '#' or masu[i+1][j] == '#' or masu[i][j-1] =='#' or masu[i][j+1] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
        elif (i == 0 or i == H-1) and (j != 0 or j != W-1):
          if masu[i][j-1] == '#' or masu[i][j+1] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
        elif (i != 0 or i != H-1) and (j == 0 or j == W-1):
          if masu[i-1][j] == '#' or masu[i+1][j] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
        elif i == 0 and j == 0:
          if masu[i+1][j] == '#' or masu[i][j+1] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
        elif i == 0 and j == W-1:
          if masu[i+1][j] == '#' or masu[i+1][j] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
        elif i == H-1 and j == 0:
          if masu[i-1][j] == '#' or masu[i][j+1] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
        elif i == H-1 and j == W-1:
          if masu[i+1][j] == '#' or masu[i][j-1] == '#':
            next_masu[i][j] = '#'
            is_change = True
          else:
            next_masu[i][j] = '.'
  if is_change:
    ans += 1
  else:
    break
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

H, W = [int(x) for x in input().split(' ')]
A = []
for i in range(H):
    A.extend([(i, j) for j, x in enumerate(input()) if x == '#'])

count = 0
done = copy.deepcopy(A)

while len(done) < H * W:
    # tmp = copy.deepcopy(A)
    h, w = zip(*A)
    h = np.array(h)
    w = np.array(w)
    A = []
    # A.extend([(x + 1, y) for x, y in tmp if x + 1 < H])
    # A.extend([(x - 1, y) for x, y in tmp if x > 0])
    # A.extend([(x, y + 1) for x, y in tmp if y + 1 < W])
    # A.extend([(x, y - 1) for x, y in tmp if y > 0])
    A.extend(zip(h+1, w))
    A.extend(zip(h-1, w))
    A.extend(zip(h, w+1))
    A.extend(zip(h, w-1))
    A = list(set(A))

    A = [x for x in A if x not in done and x[0] + 1 < H and x[0] > 0 and x[1] + 1 < W and x[1] > 0]   
    done.extend(A)
    count += 1

print(count)

Submission status: Runtime Error

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

data = [input()]
a = []
b = []
ans = 0
x = 1


while x == 1:

    for i in range(H):
        for j in range(W):
            if data[i][j] == 1:
                a.append(i)
                b.append(j)

   
    sub = len(a)
    

    for i in range(sub):
        if a[i]>0:
            data[a[i]-1][b[i]] = 1
        if a[i]+1<H:
            data[a[i]+1][b[i]] = 1
        if b[i]>0:
            data[a[i]][b[i]-1] = 1
        if b[i]+1<W:
            data[a[i]][b[i]+1] = 1
    

   

    a.clear()
    b.clear()

 

    ans += 1

    x = 0

    for i in range(H):
        for j in range(W):
            
            if data[i][j] == 0:
                x = 1

print(ans)

Submission status: Runtime Error

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

kuro = [[high,wide] for high in range(h) for wide in range(w) if a_list[high][wide] == "#" ]
shiro = [[high,wide] for high in range(h) for wide in range(w) if a_list[high][wide] == "." ]
list = []
for s in shiro:
    shiro_l = []
    for k in kuro:
        kyori =  abs(k[0]-s[0])+abs(k[1]-s[1])
        shiro_l.append(kyori)

    list.append(min(shiro_l))
print(max(list))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
kuro = []
siro = []
A = []
kyoris = []
ckyori = 1

def kyori(a, b):
    return abs(a[0] - b[0]) + abs(a[1] - b[1])

def search(poj):
    global ckyori
    i , j = poj[0], poj[1]
    while(True):
        for di in range(-ckyori, ckyori + 1):
            dj = ckyori - di
            if A[i + di][j + dj] == "#":
                return ckyori
        ckyori += 1


for i in range(h):
    Ai = input()
    A[i] = [Aij for Aij in Ai]
    for j, Aij in enumerate(Ai):
        if Aij == "#":
            kuro.append((i, j))
        else:
            siro.append((i,j))

for si in siro:
    kyoris.append(search(si))


print(max(kyoris))

Submission status: Runtime Error

Solution code:
from collections import deque

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]

def bfs(y, x):
    cnt = [[-1] * w for _ in range(h)]
    queue = deque([[y, x]])
    cnt[y][x] = 1
    while queue:
        i, j = queue.popleft()
        for di, dj in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            if 0 <= i + di < h and 0 <= j + dj < w and cnt[i + di][j + dj] == -1:
                if s[i + di][j + dj] == ".":
                    cnt[i + di][j + dj] = cnt[i][j] + 1
                    queue.append([i + di, j + dj])
                else:
                    return cnt[i][j]

ans = 0
i = 0
j = 0
while i < h:
    while j < w:
        if s[i][j] == ".":
            ans = max(ans, bfs(i, j))
            i += ans
            j += ans
print(ans)

Submission status: Runtime Error

Solution code:
i = 0
j = 0
num = 0

H,W = map(int,input().split())
A = [input().split() for  _ in range(W)]
if i == W and j == H:
    num += 1
else:
    while i <= W:
        if A[i][j] == "#":
            [i-1][j] =="#"
            [i+1][j] =="#"
            [i][j-1] =="#"
            [i][j+1] =="#"
            i += 2
        else :
            i += 1

Submission status: Runtime Error

Solution code:
import numpy as np

num_i, num_j = input().split(" ")
num_i = int(num_i)
num_j = int(num_j)
inputs = [input() for i in range(num_i)]


x = []
for _ in inputs:
    for value in _:
        x.append(value)
x = np.array(x)
x = x.reshape(num_i, num_j)

count = 0
boolean = True
while (boolean):
    count += 1
    i_arr = []
    j_arr = []
    for i in range(num_i):
        for j in range(num_j):
            if (x[i][j] == "#"):
                i_arr.append(i)
                j_arr.append(j)
    for i, j in zip(i_arr, j_arr):
        if (i == 0) & (j == 0):
            x[1][0] = "#"
            x[0][1] = "#"
        elif (i == num_i - 1) & (j == num_j - 1):
            x[i - 1][j] = "#"
            x[i][j - 1] = "#"
        elif (i == num_i - 1) & (j == 0):
            x[i - 1][0] = "#"
            x[i][1] = "#"
        elif (i == 0) & (j == num_j - 1):
            x[1][j] = "#"
            x[0][j - 1] = "#"
        elif (i == 0):
            x[1][j] = "#"
            x[0][j - 1] = "#"
            x[0][j + 1] = "#"
        elif (j == 0):
            x[i - 1][0] = "#"
            x[i + 1][0] = "#"
            x[i][1] = "#"
        elif (i == num_i - 1):
            x[i - 1][j] = "#"
            x[i][j + 1] = "#"
            x[i][j - 1] = "#"
        elif (j == num_j - 1):
            x[i - 1][j] = "#"
            x[i + 1][j] = "#"
            x[i][j - 1] = "#"
        else:
            x[i - 1][j] = "#"
            x[i + 1][j] = "#"
            x[i][j - 1] = "#"
            x[i][j + 1] = "#"
    if len(set(list(x.flatten()))) == 1:
        print(str(count))
        boolean = False
    else:
        boolean = True

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
n = h*w
d = []
for i in range(h):
    d.append(list(input()))
hw_list = []
dark = []
white = []
for i in range(h):
    for j in range(w):
        if d[i][j] == "#":
            dark.append((i,j))
        else:
            white.append((i,j))
ld = len(dark)
wd = len(white)
dist = []
for i in range(wd):
    di = []
    for j in range(ld):
        di.append(abs(white[i][0]-dark[j][0])+abs(white[i][1]-dark[j][1]))
    dist.append(min(di))
print(max(dist))

Submission status: Runtime Error

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

for i, item in enumerate(a):
    a[i] = list(item[0])

change = [[0 for _ in range(len(a[0]))] for _ in range(len(a))]


def isWhite(a):
    items = []
    for i in range(len(a)):
        items.extend(a[i])
    if "." in items:
        return True
    return False

cnt = 0
while isWhite(a):
    flg = False
    for i in range(len(a)):
        if "#" not in a[0]:
            continue
        for j in range(len(a[0])):
            if a[i][j] == "#":
                if i - 1 >= 0:
                    if a[i - 1][j] != "#":
                        change[i-1][j] = 1
                if i + 1 < len(a):
                    if a[i + 1][j] != "#":
                        change[i+1][j] = 1
                if j - 1 >= 0:
                    if a[i][j - 1] != "#":
                        change[i][j-1] = 1
                if j + 1 < len(a):
                    if a[i][j + 1] != "#":
                        change[i][j+1] = 1
                if not flg:
                    cnt += 1
                flg = True
    for i in range(len(a)):
        if 1 not in change[i]:
            continue
        for j in range(len(a[0])):
            if change[i][j] == 1:
                change[i][j] = 0
                a[i][j] = "#"

print(cnt)

Submission status: Runtime Error

Solution code:
a

Submission status: Runtime Error

Solution code:
from collections import deque


def calcDist(A, B, H, W, j, i, dist):
    q = deque([(j, i, dist)])
    B[j][i] = dist

    while(True):
        if len(q) <= 0:
            break
        qp = q.popleft()

        if qp[0]-1 >= 0 and B[qp[0]-1][qp[1]] == -1:
            q.append((qp[0]-1, qp[1], qp[2]+1))
            B[qp[0]-1][qp[1]] = qp[2]+1
        if qp[0]+1 < H and B[qp[0]+1][qp[1]] == -1:
            q.append((qp[0]+1, qp[1], qp[2]+1))
            B[qp[0]+1][qp[1]] = qp[2]+1
        if qp[1]-1 >= 0 and B[qp[0]][qp[1]-1] == -1:
            q.append((qp[1], qp[0]-1, qp[2]+1))
            B[qp[0]][qp[1]-1] = qp[2]+1
        if qp[1]+1 < W and B[qp[0]][qp[1]+1] == -1:
            q.append((qp[0], qp[1]+1, qp[2]+1))
            B[qp[0]][qp[1]+1] = qp[2]+1

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

B = []
for j in range(H):
    b = []
    for i in range(W):
        if A[j][i] == '#':
            b.append(0)
        else:
            b.append(-1)
    B.append(b)

cnt = 0
for j in range(H):
    for i in range(W):
        if A[j][i] == '#':
            calcDist(A, B, H, W, j, i, 0)

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

Submission status: Runtime Error

Solution code:
# coding: utf-8

from collections import deque

h, w = [int(x) for x in input().split()]

i_mat = []

queue = deque()


for i in range(h):
    x = input()
    i_mat.append([0 if y == "#" else 99999 for y in list(x)])

    for (j, t) in enumerate(i_mat[-1]):
        if t == 0:
            queue.append((i, j))


while queue:
    index = queue.pop()
    curr = i_mat[index[0]][index[1]]

    # ←
    if index[0]-1 >= 0 and i_mat[index[0]-1][index[1]] > curr + 1:
        i_mat[index[0]-1][index[1]] = curr + 1
        queue.append((index[0]-1, index[1]))

    # ↑
    if index[1]-1 >= 0 and i_mat[index[0]][index[1]-1] > curr + 1:
        i_mat[index[0]][index[1]-1] = curr + 1
        queue.append((index[0], index[1]-1))

    # →
    if index[0]+1 < w and i_mat[index[0]+1][index[1]] > curr + 1:
        i_mat[index[0]+1][index[1]] = curr + 1
        queue.append((index[0]+1, index[1]))
    # ↓
    if index[1]+1 < h and i_mat[index[0]][index[1]+1] > curr + 1:
        i_mat[index[0]][index[1]+1] = curr + 1
        queue.append((index[0], index[1]+1))

result = 0

for data in i_mat:
    if result < max(data):
        result = max(data)

print(result)

Submission status: Runtime Error

Solution code:
h,w=input().split()
H = int(h)
W = int(w)
A = list()
A_ = list()
for i in range(H):
    A.append([""]*W)
    A_.append([""]*W)
    x = input()
    for j in range(W):
        for k in range(W):
            A[i][j] = x[k]
            A_[i][j] = x[k]
go = True

#黒「#」 白「.」
#「.」を「＃」にする
#ひっくり返し関数
def reverse(A,Pre):
    #Aリバースあと
    #Bリバース前
    for i in range(H):
        for j in range(W):
            if Pre[i][j] == "#":
                if i == 0:
                    if j == 0:
                        A[0][1] = "#"
                        A[1][0] = "#"
                    elif j == W-1:
                        A[0][W-2] = "#"
                        A[1][W-1] = "#"
                    else:
                        A[i][j-1] = "#"
                        A[i][j+1] = "#"
                        A[i+1][j] = "#"
                
                elif i == H-1:
                    if j == 0:
                        A[H-1][1] = "#"
                        A[H-1-1][0] = "#"
                    elif j == W-1:
                        A[H-1-1][W-1] = "#"
                        A[H-1][W-2] = "#"
                    else:
                        A[H-1][j-1] = "#"
                        A[H-1][j+1] = "#"
                        A[H-1-1][j] = "#"
                
                elif i != 0 and i != H-1:
                    if j == 0:
                        A[i][0+1] = "#"
                        A[i-1][0] = "#"
                        A[i+1][0] = "#"
                    elif j == W-1:
                        A[i][W-1-1] = "#"
                        A[i-1][W-1] = "#"
                        A[i+1][W-1] = "#"
                    else:
                        A[i-1][j] = "#"
                        A[i][j-1] = "#"
                        A[i][j+1] = "#"
                        A[i+1][j] = "#"

#全部黒か判定関数
def judge(A):
    global n
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                n += 1
            else:
                pass
    n == H*W

#本体プログラム
from copy import deepcopy
ans = 0
n = 0
while n != H*W:
    n = 0
    ans += 1
    reverse(A_,A)
    #Pre == A
    A = deepcopy(A_)
    judge(A)

print(ans+1)

Submission status: Runtime Error

Solution code:
import copy

h, w  = map(int,input().split())
grid = [0]*h
for i in range(h):
    grid[i] = list(input())



memo = [[0]*(w+2) for j in range(h+2)]


#メモの初期化
for i in range(h+2):
    for j in range(w+2):
        memo[i][j] = 0

for i in range(h):
    for j in range(w):
        if grid[i][j] == '.':
            memo[i+1][j+1] = 0
        else:
            memo[i+1][j+1] = 1

another = copy.deepcopy(memo)


count = 0
q = []
for i in range(1,h+1):
        for j in range(1,w+1):
            if memo[i][j] == 0:
                f = 1
                another[i][j] = memo[i-1][j] or memo[i+1][j] or memo[i][j-1] or memo[i][j+1]
                if another[i][j]:
                    if not memo[i-1][j] and i != 1:
                        q.append((i-1,j))
                    if not memo[i+1][j] and i != h:
                        q.append((i+1,j))
                    if not memo[i][j-1] and j != 1:
                        q.append((i,j-1))
                    if not memo[i][j+1] and j != w:
                        q.append((i,j+1))
if f:
    count = 1
memo = copy.deepcopy(another)

another_q = []
while 1:
    f = 0 
    while q:
        i, j = q.pop(0)
        if memo[i][j] == 0:
            f = 1
            another[i][j] = memo[i-1][j] or memo[i+1][j] or memo[i][j-1] or memo[i][j+1]
            if another[i][j]:
                if (not memo[i-1][j]) and i != 1 :
                    another_q.append((i-1,j))
                if (not memo[i+1][j]) and i != h:
                    another_q.append((i+1,j))
                if (not memo[i][j-1]) and j != 1:
                    another_q.append((i,j-1))
                if (not memo[i][j+1]) and j != w:
                    another_q.append((i,j+1))

    memo = copy.deepcopy(another)

    if f == 0:
        break

    q = copy.deepcopy(another_q)
    another_q = []
    count += 1

print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
import math

h, w = map(int, input().split())
A=[]

for i in range(0,h):
    A.append(list(str(x) for x in input()))
    
i=0

A = np.array(A)

B = np.where(A == "#")
B1 = np.array([B[0], B[1]]).T

B2 = []

for i in range(0,max(h,w)):
    if (i<h) & (A[i,0] == "."):
         B2.append([i,0])
    if (i<h) & (A[i,w-1] == "."):
        B2.append([i,w-1])
    if (i<w) & (A[0,i] == "."):
         B2.append([0,i])
    if (i<w) & (A[h-1,i] == "."):
        B2.append([h-1,i])
    
C1=np.zeros(len(B1))
C2=np.zeros(len(B2))

for i in range(0,max(len(B1),len(B2))):
    temp1=[]
    temp2=[]
    for j in range(0,max(i, len(B2))):
        if j<i:
            temp1.append(math.ceil(sum(np.abs(B1[i]-B1[j]))/2))
        if j<len(B2):
            temp2.append(sum(np.abs(B1[i]-B2[j])))
    if len(temp1)>0:
        C1[i]=min(temp1)
    if len(temp2)>0:
        C2[i]=min(temp2)
        
C3= np.insert(C1,0,C2)
print(int(max(C3)))

Submission status: Runtime Error

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


def change(x, y):
    if x < 0 or x >= W:
        return False
    if y < 0 or y >= H:
        return False

    if G[x][y] == ".":
        G[x][y] = "@"
        return False
    elif G[x][y] == "#":
        return True


def search(x, y):
    global ans
    count = 0
    sharp = 0

    while sharp == 0:
        if count == 0:
            G[x][y] = "@"
        else:
            for i in range(count+1):
                if change(x+count-i, y+1) or change(x-i, y+count-i) or \
                    change(x-count+i, y-i) or change(x+i, y-count+i):
                    sharp += 1

        if count > ans:
            ans = count

        count += 1


for i in range(H):
    for j in range(W):
        if G[i][j] == ".":
            search(j, i)

print(ans)