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:
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(count)
        boolean = False
    else:
        boolean = True

Submission status: Runtime Error

Solution code:
import copy

h, w = map(int, input().split())
a = [h+1]
ax = [h+1]

check_lis = [h+1]
check=['#' for i in range(w)]

for i in range(h):
    a.append(list(input()))
    check_lis.append(check)

for i in range(h+1):
    ax.append(a[i])

# print(ax)
a.pop(0)
ax = copy.deepcopy(a)
ax.pop(0)
check_lis.pop(0)

flag=0

#print(a)
#print(ax)
#print(check_lis)

while True:
    if ax != check_lis:
        for i in range(h):
            for j in range(w):
                if a[i][j]=="#":
                    if i!=0 and ax[i-1][j]!='#':
                        ax[i-1][j]='#'
                    if i+1<=w-1 and ax[i+1][j]!='#':
                        ax[i+1][j]='#'
                    if j!=0 and ax[i][j-1]!='#':
                        ax[i][j-1]='#'
                    if j+1<=h-1 and ax[i][j+1]!='#':
                        ax[i][j+1]='#'
        a = copy.deepcopy(ax)
        #print('ax')
        #print(ax)
        flag += 1
    else:
        break

print(flag)

Submission status: Runtime Error

Solution code:
import copy
def main():
    dict_ = {'#': 1, '.': 0}
    h, w = map(int, (input().split()))
    num = []
    for hh in range(h):
        s = input()
        for ww in range(len(s)):
            if s[ww]=='#':
                num.append(ww + hh * h)
    print(max([min([abs(white//w - black//w) + abs(white%w - black%w) for black in num]) for white in list(set(range(h*w)) - set(num))]))

if __name__ == '__main__':
    main()

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
B = []

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

    for a in indexes:
        if a not in B:
            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] = '#'
            B.append(a)

    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(700000)
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)) 
    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:
# coding: utf-8

from collections import deque

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

i_mat = []

queue = deque()
i_s = set()


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))
            i_s.add((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
        if (index[0]-1, index[1]) not in i_s:
            queue.append((index[0]-1, index[1]))
            i_s.add((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
        if (index[0], index[1]-1) not in i_s:
            queue.append((index[0], index[1]-1))
            i_s.add((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
        if (index[0]+1, index[1]) not in i_s:
            queue.append((index[0]+1, index[1]))
            i_s.add((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
        if (index[0], index[1]+1) not in i_s:
            queue.append((index[0], index[1]+1))
            i_s.add((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 = 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):
    for i in range(len(a)):
        if "." in a[i]:
            return True
    return False

cnt = 0
while isWhite(a):
    flg = False
    for i in range(len(a)):
        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)):
        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:
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:
        return(count)
        boolean = False
    else:
        boolean = True

Submission status: Runtime Error

Solution code:
a,b=input().split()
a=int(a)
ppap=[]
for i in range(a):
    c=input()
    l=[]
    for i in c:
        l.append(i)
    ppap.append(l)
fl=[]
of=0
for j in ppap:
    x=[i for i,x in enumerate(j) if x=="#"]
    for i in x:
        fl.append([of,i])
    of+=1
dl=[]
ofset1=0
for i in ppap:
    ofset2=0
    for j in i:
        kl=[]
        if j==".":
            for k in fl:
                kl.append(abs(ofset1-k[0])+abs(ofset2-k[1]))
            dl.append(min(kl))
        ofset2+=1
    ofset1+=1
print(max(dl))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

h,w = na()

a = []

for i in range(h):
  s = ns()
  b = []
  for j in s:
    if j == '#':
      b.append(1)
    if j == '.':
      b.append(0)
  a.append(b)

t = []
c = 1
while(True):
  p = True
  for i in range(w):
    for j in range(h):
      if a[i][j] == c:
        if i > 0 and a[i-1][j] == 0:
          p = False
          a[i-1][j] = c+1
        if j > 0 and a[i][j-1] == 0:
          p = False
          a[i][j-1] = c+1
        if i < w-1 and a[i+1][j] == 0:
          p = False
          a[i+1][j] = c+1
        if j < h-1 and a[i][j+1] == 0:
          p = False
          a[i][j+1] = c+1
  if p:
    break
  else:
    c+=1

print(c-1)

Submission status: Runtime Error

Solution code:
import numpy as np


def main():
    # H, W = [int(x) for x in input().split()]
    H, W = map(int, [int(x) for x in input().split()])
    board = [[0 for i in range(H)] for j in range(W)]

    """ #は1, .は0 """
    for i in range(H):
        X = input()
        X_len = len(X)
        for j in range(X_len):
            if X[j] == "#":
                board[i][j] = 1

    num_board = np.array(board)
    height, width = num_board.shape[:2]
    count = 0
    while (np.sum(num_board) != height*width):
        num_board_ans = np.zeros((height, width))
        for i in range(height):
            if np.sum(num_board[:, j]) == width:
                continue
            for j in range(width):
                if num_board[i, j] == 1:
                    num_board_ans[i, j] = 1
                    a = max(i-1, 0)
                    num_board_ans[a, j] = 1
                    b = max(j-1, 0)
                    num_board_ans[i, b] = 1
                    c = min(i+1, height-1)
                    num_board_ans[c, j] = 1
                    d = min(j+1, width-1)
                    num_board_ans[i, d] = 1
        num_board = num_board_ans
        count += 1

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import copy

h, w = map(int, input().split())
a = [h+1]
ax = [h+2]

check_lis = [h+1]
check=['#' for i in range(w)]

for i in range(h):
    a.append(list(input()))
    check_lis.append(check)

for i in range(h+1):
    ax.append(a[i])

# print(ax)
ax = copy.deepcopy(a)

a.pop(0)
ax.pop(0)
check_lis.pop(0)

flag=0

#print(a)
#print(ax)
#print(check_lis)

while True:
    if ax != check_lis:
        for i in range(h):
            for j in range(w):
                if a[i][j]=="#":
                    if i!=0 and ax[i-1][j]!='#':
                        ax[i-1][j]='#'
                    if i+1<=w-1 and ax[i+1][j]!='#':
                        ax[i+1][j]='#'
                    if j!=0 and ax[i][j-1]!='#':
                        ax[i][j-1]='#'
                    if j+1<=h-1 and ax[i][j+1]!='#':
                        ax[i][j+1]='#'
        a = copy.deepcopy(ax)
        #print('ax')
        #print(ax)
        flag += 1
    else:
        break

print(flag)

Submission status: Runtime Error

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

lst = []
for i in range(H):
    lst += [list(input())]

#print (lst)
INF = 10 ** 9
dp =[[INF] * W for _ in range(H)]

def bfs1(i, j): #右下向き
#    print ('i, j= ', i, j)
    if i < 0 or i == W or j < 0 or j == H:
        return INF
    elif lst[i][j] == '#':
        dp[i][j] = 0
        return 0
    else:
        a = min(bfs1(i+1, j)+1, bfs1(i, j+1)+1)
        dp[i][j] = a
        return a

def bfs2(i, j): #左上向き
#    print ('i, j= ', i, j)
    if i < 0 or i == W or j < 0 or j == H:
        return INF
    elif lst[i][j] == '#':
        return 0
    else:
        b = min(bfs2(i-1, j)+1, bfs2(i, j-1)+1, dp[i][j])
        dp[i][j] = b
        return b


ans1 = INF
ans2 = INF
for x in range(W):
    for y in range(H):
        bfs1(x,y)

for v in range(W):
    for w in range(H):
        bfs2(W-1-v, H-1-w)

c = 0
for a in range(W):
    b = max(dp[a])
    c =max(b, c)
print (c)
#print (ans1)
#print (ans2)

#print ('ans= ', min(ans1, ans2))
#print (min(ans1, ans2))

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,len(B1)):
    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[j]=min(temp2)
        
C3= np.insert(C1,0,C2)
print(int(max(C3)))

Submission status: Runtime Error

Solution code:

# coding: utf-8

# In[24]:

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


# In[26]:

import numpy as np


# In[ ]:

bin_masu = np.zeros((H, W))#, dtype=np.uint8)
masu = np.array(masu)
bin_masu[masu == '#'] = 1
#print(bin_masu.shape, masu.shape)
count = 0
while True:
#print(bin_masu)
#for _ in range(3):
    if np.all(bin_masu != 0):
        #print(count)
        #exit()
        break
    tmp_masu = np.copy(bin_masu)
    #print(tmp_masu)
    tmp_masu[0:H-1] += bin_masu[1:H]
    #print(tmp_masu)
    tmp_masu[1:H] += bin_masu[0:H-1]
    #print(tmp_masu)
    tmp_masu[:,0:W-1] += bin_masu[:,1:W]
    #print(tmp_masu)
    tmp_masu[:,1:W] += bin_masu[:, 0:W-1]
    #print(tmp_masu)
    #tmp_masu[tmp_masu > 0] = 1
    #print(bin_masu)
    #print(tmp_masu)
    bin_masu = tmp_masu
    count += 1
    #print('a')
#print(np.min(bin_masu))
print(count)


# In[ ]:

#bin_masu = np.zeros((H, W))#, dtype=np.uint8)
#cal_masu = np.zeros((H, W))
#masu = np.array(masu)
#bin_masu[masu == '#'] = 1
#print(bin_masu.shape, masu.shape)
#count = 0

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:
        return(count)
        boolean = False
    else:
        boolean = True

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)

        # for i in a:
        #         print(i)
        # print(counter)
print(counter-1)

Submission status: Runtime Error

Solution code:
first_input = list(map(int, input().split()))
height = first_input[0]
width = first_input[1]

area = []
for i in range(0, height):
  line = input()
  for k in range(0, width):
    area.append(line[k:k+1])

area_length = len(area)
answer = 0

target_indices = []
  for i,item in enumerate(area):
    if item == '#':
      target_indices.append(i)

while(True):
  answer += 1
  for target_index in target_indices:
    # 左方向
    if target_index - 1 >= 0 and target_index % width != 0:
    	area[target_index - 1] = '#'
        target_indices.append(target_index - 1)
    # 右方向
    if target_index + 1 < area_length and target_index % width != width-1:
    	area[target_index + 1] = '#'
        target_indices.append(target_index + 1)
    # 下方向
    if target_index + width < area_length:
    	area[target_index + width] = '#'
        target_indices.append(target_index + width)
    # 上方向
    if target_index - width >= 0:
    	area[target_index - width] = '#'
        target_indices.append(target_index - width)
  if '.' not in area:
    break

print(answer)

Submission status: Runtime Error

Solution code:
hw = list(map(int,input().split()))
h = hw[0]
w = hw[1]
a = [[c for c in str(input())] for j in range(h)]

w_num = 0
for i in range(w):
  for j in range(h):
    if a[i][j] == '.':
      w_num += 1

x = []
for c in range(j*w):
  if c == 0:
    x.append('#')
  else:
    x.append(int(c))

for n in range(w*h):
  for i in range(w):
    for j in range(h):
      if i == 0 and j == 0:
        if a[i][j] == x[n]:
          if a[i+1][j] == '.':
            a[i+1][j] = x[n+1]
            w_num -= 1
          if a[i][j+1] == '.':
            a[i][j+1] = x[n+1]
            w_num -= 1
      elif i == 0 and j == w-1:
        if a[i][j] == x[n]:
          if a[i][j-1] == '.':
            a[i][j-1] = x[n+1]
            w_num -= 1
          if a[i+1][j] == '.':
            a[i+1][j] = x[n+1]
            w_num -= 1
      elif i == h-1 and j == 0:
        if a[i][j] == x[n]:
          if a[i-1][j] == '.':
            a[i-1][j] = x[n+1]
            w_num -= 1
          if a[i][j+1] == '.':
            a[i][j+1] = x[n+1]
            w_num -= 1
      elif i == h-1 and j == w-1:
        if a[i][j] == x[n]:
          if a[i-1][j] == '.':
            a[i-1][j] = x[n+1]
            w_num -= 1
          if a[i][j-1] == '.':
            a[i][j-1] = x[n+1]
            w_num -= 1
      elif i == 0:
        if a[i][j] == x[n]:
          if a[i][j-1] == '.':
            a[i][j-1] = x[n+1]
            w_num -= 1
          if a[i+1][j] == '.':
            a[i+1][j] = x[n+1]
            w_num -= 1
          if a[i][j+1] == '.':
            a[i][j+1] = x[n+1]
            w_num -= 1
      elif j == 0:
        if a[i][j] == x[n]:
          if a[i-1][j] == '.':
            a[i-1][j] = x[n+1]
            w_num -= 1
          if a[i+1][j] == '.':
            a[i+1][j] = x[n+1]
            w_num -= 1
          if a[i][j+1] == '.':
            a[i][j+1] = x[n+1]
            w_num -= 1
      elif i == h-1:
        if a[i][j] == x[n]:
          if a[i-1][j] == '.':
            a[i-1][j] = x[n+1]
            w_num -= 1
          if a[i][j-1] == '.':
            a[i][j-1] = x[n+1]
            w_num -= 1
          if a[i][j+1] == '.':
            a[i][j+1] = x[n+1]
            w_num -= 1
      elif j == w-1:
        if a[i][j] == x[n]:
          if a[i-1][j] == '.':
            a[i-1][j] = x[n+1]
            w_num -= 1
          if a[i][j-1] == '.':
            a[i][j-1] = x[n+1]
            w_num -= 1
          if a[i+1][j] == '.':
            a[i+1][j] = x[n+1]
            w_num -= 1
      else:
        if a[i][j] == x[n]:
          if a[i-1][j] == '.':
            a[i-1][j] = x[n+1]
            w_num -= 1
          if a[i][j-1] == '.':
            a[i][j-1] = x[n+1]
            w_num -= 1
          if a[i+1][j] == '.':
            a[i+1][j] = x[n+1]
            w_num -= 1
          if a[i][j+1] == '.':
            a[i][j+1] = x[n+1]
            w_num -= 1
  if w_num == 0:
    print(x[n+1])
    break

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[190]:


import numpy as np


# In[207]:


H,W = map(int, input().split())
A = [input().split() for i in range(H)]
A = np.array(A)


# In[227]:


B = np.full((H+2, W+2), '.')
B[1:-1, 1:-1] = A
count = 0

#print(B)

while np.all(B[1:-1, 1:-1] == '#') == False:
    count += 1
    for index in np.transpose(np.where(B[1:-1,1:-1] == '#')) + 1:
        B[index[0]-1, index[1]] = '#'
        B[index[0]+1, index[1]] = '#'
        B[index[0], index[1]-1] = '#'
        B[index[0], index[1]+1] = '#'     
    #print(B)
print(count)


# In[ ]:

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
A=[list(input()) for i in range(h)]
B=[]
for i in range(w):
  for j in range(h):
    if A[i][j]=="#":
      B.append([i,j])
ans=0
for i in range(w):
  for j in range(h):
    if [i,j] not in B:
      d=min(abs(i-b[0])+abs(j-b[1]) for b in B)     
      ans=max(d,ans)
print(ans)

Submission status: Runtime Error

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

As = []
for _ in range(H):
  As.append(list(input()))
  
def is_end(As):
  for i in range(H):
    if "." in As[i]:
      return False
  return True

def to_black(As, poslist):
  for h, w in poslist:
    As[h][w] = "#"
  return As

count = 0
while True:
  if is_end(As):
    break
    
  poslist = []
  for i in range(H):
    for j in range(W):
      for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if i+dh >= H or j+dw >= W:
          continue
        if As[i][j] == "." and As[i+dh][j+dw] == "#":
          As_copy[i][j] = "#"
          poslist.append((i, j))
  As = to_black(As, poslist)
  count += 1

print(count)

Submission status: Runtime Error

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

A  = [['0']* (W + 2)]

for _ in range(H):
    tmp = input()
    tmp = tmp.replace('.', '0').replace('#', '1')
    tmp = '0' + tmp + '0'
    A.append(list(tmp))
A.append(['0']* (W + 2))
    

#print(A)

import numpy as np
frame = np.array(A)
frame = np.reshape(frame, (H+2, W+2))
#print(frame)
#print("===================")

END_NUM = int(('1' * W + '0'), 2)
ans = 0

while True:
    # 縦方向のbit化
    bit_frame = []
    for a in frame:
        tmp = ''.join(a)
        bit_frame.append(int(tmp, 2))
    #print(bit_frame)
    
    # 終了チェック
    #print(END_NUM)
    if bit_frame.count(END_NUM) == len(bit_frame) - 2:
        break
    else:
        ans += 1

    # 縦方向のOR
    next_bit_frame = [0]
    for i in range(1, len(bit_frame)-1):
        next_bit_frame.append( bit_frame[i-1] | bit_frame[i] | bit_frame[i+1] )
    next_bit_frame.append(0)
    del bit_frame
    #print(next_bit_frame)
    
    
    # 横方向のbit化
    rot_frame = np.rot90(frame, k=1)
    #print(rot_frame)
    rot_bit_frame = []
    for a in rot_frame:
        tmp = ''.join(a)
        rot_bit_frame.append(int(tmp, 2))
    #print(rot_bit_frame)


    # 横方向のOR
    next_rot_bit_frame = [0]
    for i in range(1, len(rot_bit_frame)-1):
        next_rot_bit_frame.append( rot_bit_frame[i-1] | rot_bit_frame[i] | rot_bit_frame[i+1] )
    next_rot_bit_frame.append(0)
    #print(next_rot_bit_frame)
    del rot_bit_frame

    # 横方向を回転
    next_rot_bit_frame = np.array([list(format(x, 'b').zfill(W+2)) for x in next_rot_bit_frame])
    #print("--")
    #print(next_rot_bit_frame )
    next_rot_bit_frame = np.rot90(next_rot_bit_frame, k=3)
    #print(next_rot_bit_frame )
    tmp_bit_frame = []
    for a in next_rot_bit_frame:
        tmp = ''.join(a)
        tmp_bit_frame.append(int(tmp, 2))
    next_rot_bit_frame = tmp_bit_frame[:]
    #print(next_bit_frame)
    #print(next_rot_bit_frame)
    next_frame = []
    for a, b in zip(next_bit_frame, next_rot_bit_frame):
        next_frame.append(a | b)
    #print(next_frame)
    
    frame = np.array([list(format(x, 'b').zfill(W+2)) for x in next_frame])
    #print(frame)

print(ans)

Submission status: Runtime Error

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

black_list = []
white_list = []

ans = 0
for i in range(W):
  for j in range(H):
    if A[i][j] == "#":
      black_list.append([i,j])
    else:
      white_list.append([i,j])

if len(white_list) != 0 and len(black_list) != 0:
  for white_dot in white_list:
    min_distance = W+H
    for black_dot in black_list:
      min_distance = min(min_distance, abs(black_dot[0]-white_dot[0])+abs(black_dot[1]-white_dot[1]))
    ans = max(ans,min_distance)

print(ans)

Submission status: Runtime Error

Solution code:
black=[]
white=[]

for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            black.append([i,j])
        else:
            white.append([i,j])
edge=[[0,0],[0,W-1],[H-1,W-1],[H-1,0]]
            
ans=[]
for b1 in black:
    for b2 in black:
        x=abs(b1[0]-b2[0])
        y=abs(b1[1]-b2[1])
        l=x+y
        ans.append(l)
    for e in edge:
        x=abs(b1[0]-e[0])
        y=abs(b1[1]-e[1])
        l=x+y
        ans.append(l)
ans2=max(ans)
print(ans2)

Submission status: Runtime Error

Solution code:
import re
h,w=list(map(int,input().split()))
l=list(input() for i in range(h))
ans=0
for j in range(h*w):
      if any(r'.' in l[k] for k in range(h)):
            anosq=0
      else:
            break
      for m in range(h):
            for n in range(w):
                  if r'#' == l[m][n]:
                        l[m]=l[m].replace(r'.',r'x')
                        for p in range(h):
                              print(l[p][n])
                              l[p][n]=l[p][n].replace(r'.',r'x')
                              
      for q in range(h):
            l[q]=l[q].replace(r'x',r'#')
      ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
import copy
a,b=input().split()
a=int(a)
b=int(b)
ppap=[]
for i in range(a):
    c=input()
    l=[]
    for i in c:
        l.append(i)
    ppap.append(l)
fl=[]
of=0

for j in ppap:
    x=[i for i,x in enumerate(j) if x=="#"]
    for i in x:
        fl.append([of,i])
    of+=1
dl=[]
ofset1=0
for i in ppap:
    ofset2=0
    for j in i:
        kl=[]
        if j==".":
            for k in fl:
                kl.append(abs(ofset1-k[0])+abs(ofset2-k[1]))
            dl.append(min(kl))
        ofset2+=1
    ofset1+=1
print(max(dl))