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:
def main():
  dep, wid = 3,3
  a = [[".",".","."],[".","#","."],[".",".", "."]]
      
  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 = [0]*H
for i in range(W):
    A[i] = list(input())

o = [0]*(H*W)
p = 0
f = 0

for x in range(W):
    for y in range(H):
        if A[x][y] == '#':
            A[x][y] = 0
            o[p] = [x,y]
            p+=1

while f < H * W:
    x,y = o[f]
    v = A[x][y]
    if 0 <= x-1:
        if A[x-1][y] == '.':
            A[x-1][y] = v + 1
            o[p] = [x-1,y]
            p+=1
    if x+1 <= W-1:
        if A[x+1][y] == '.':
            A[x+1][y] = v + 1
            o[p] = [x+1,y]
            p+=1
    if 0 <= y-1:
        if A[x][y-1] == '.':
            A[x][y-1] = v + 1
            o[p] = [x,y-1]
            p+=1
    if y+1 <= H-1:
        if A[x][y+1] == '.':
            A[x][y+1] = v + 1
            o[p] = [x,y+1]
            p+=1
    f+=1

print(max(map(max,A)))

Submission status: Runtime Error

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

def paint(a):
    for i in range(h):
        for j in range(w):
            if a[i][j]=="#":
                a[i-1][j]="#"
                a[i+1][j]="#"
                a[i][j-1]="#"
                a[i][j+1]="#"
    for k in range(h):
        for l in range(w):
            if a[k][l]==".":
                return 1
            else:
                return 0

ans=0
while t==0:
    t=paint(a)
    ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import*
import sys;input=sys.stdin.readline
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=[]
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Submission status: Runtime Error

Solution code:
INF = 10**20
H, W = list(map(int, input().split()))
queue = []
board = [[0]*(W+2)] + [[0]+[INF for i in range(H)]+[0] for j in range(W)] + [[0]*(W+2)]
max_n = 0

A = [input()for i in range(H)]# + ['#'*W]

for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            queue.append((i+1, j+1))
            board[i+1][j+1] = 0
               
while len(queue):
    i, j = queue.pop(0)
    n = board[i][j] + 1
    for i_, j_ in [(i-1,j), (i+1,j), (i,j-1), (i,j+1)]:
        if board[i_][j_] == INF:
            board[i_][j_] = n
            queue.append((i_, j_))
            max_n = max(max_n, n)
print(max_n)

Submission status: Runtime Error

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

flag = 0
loop_count = 0

while flag == 0:
    count = 0
    B = []
    C = []
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                count += 1
                B.append(i)
                C.append(j)
    if count == H * W:
        flag = 1
    else:
        for i in range(count):
            if B[i] == 0:
                if C[i] == W-1:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]+1][C[i]] = "#"
                elif C[i] == 0:
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]+1][C[i]] = "#"
                else:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]+1][C[i]] = "#"
                    A[B[i]][C[i]+1] = "#"
            elif B[i] == H-1:
                if C[i] == W-1:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                elif C[i] == 0:
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]-1][C[i]] = "#"
                else:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]][C[i]+1] = "#"
            else:
                if C[i] == W-1:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]+1][C[i]] = "#"
                elif C[i] == 0:
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]+1][C[i]] = "#"
                else:
                    A[B[i]][C[i]-1] = "#"
                    A[B[i]-1][C[i]] = "#"
                    A[B[i]][C[i]+1] = "#"
                    A[B[i]+1][C[i]] = "#"
        loop_count += 1
        
print(loop_count)

Submission status: Runtime Error

Solution code:
import numpy as np
mas = np.array(list(map(int,input().split())))
go = []
while True:
    try:
        go.append(list(map(input().split())))

    except:
        break
go = np.array(go)
H = int(mas[0])
W = int(mas[1])

b = np.zeros((H,W))
for i in range(H):
    for j in range(W):
        b[i,j] = '#'
itr = 0
while not any(go == b):
    b_w = np.where(go == '#')
    for i in range(len(x)):
        try:
            go[b_w[i] + [-1,-1]] ='#'
            go[b_w[i] + [-1,1]] = '#'
            go[b_w[i] + [1,1]] = '#'
            go[b_w[i] + [1,-1]] = '#'
        except:
            continue
    itr += 1
print(itr)

Submission status: Runtime Error

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

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 = copy.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 = copy.deepcopy(newa)

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

Submission status: Runtime Error

Solution code:
import numpy as np
mas = np.array(list(map(int,input().split())))
go = []
while True:
    try:
        go.append(list(map(input().split())))

    except:
        break
go = np.array(go)
H = int(mas[0])
W = int(mas[1])

b = np.zeros((H,W))
for i in range(H):
    for j in range(W):
        b[i,j] = '#'
itr = 0
while not any(go == b):
    b_w = np.where(go == '#')
    for i in range(len(x)):
        try:
            go[b_w[i] + [-1,-1]] ='#'
            go[b_w[i] + [-1,1]] = '#'
            go[b_w[i] + [1,1]] = '#'
            go[b_w[i] + [1,-1]] = '#'
        except:
            continue

Submission status: Runtime Error

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

b = "#"
w = "."

def out(i, j):
  if i < 0:
    return True
  if i >= W:
    return True
  if j < 0:
    return True
  if j >= H:
    return True
  return False

def check(i, j):
  if A[i][j] == b:
    return 0
  k = 1
  while True:
    F = False
    for ii in range(i - k, i):
      jj = j - (k - (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    for ii in range(i - k, i):
      jj = j + (k - (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    for ii in range(i, i + k + 1):
      jj = j - (k + (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    for ii in range(i, i + k + 1):
      jj = j + (k + (i - ii))
      if out(ii, jj):
        continue
      if A[ii][jj] == b:
        return k
    k += 1

Ans = 0
for i in range(H):
  for j in range(W):
    k = check(i, j)
    Ans = max(Ans, k)

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 = []
    for j in bl:
        mn.append(abs(j[0]-i[0]) + abs(j[1]-i[1]))
    mx.append(min(mn))
print(max(mx))

Submission status: Runtime Error

Solution code:
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]=="#":
                        continue
                    grid[y+i][x+j]="#"
                    lock.append((y+i,x+j))
    g+=1
    lock=[]
                    
    if all(all(j=="#" for j in i)for i in grid):
        print(g)
        exit()

Submission status: Runtime Error

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

grid = []
for _ in range(w):
    grid.append([x == '#' for x in input()])

maxd = 0
for i in range(h):
    for j in range(w):
        if grid[i][j] is False:
            mind = 10000000
            for ii in range(h):
                for jj in range(w):
                    if grid[ii][jj] is True:
                        d = abs(i - ii) + abs(j - jj)
                        if d < mind:
                            mind = d
            if mind > maxd:
                maxd = mind

print(maxd)

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
  for i in range(dep):
    flg = 0
    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)]
HMANY = [ [10**9] * W for _ in range(H)]


def dfs(h,w,A,HMANY, num,H,W):
    if not ( 0<= h < H and 0 <= w < W) : 
        return

    if HMANY[h][w] > num:
        HMANY[h][w] = num  

        if 0 <= h-1 :
            dfs(h-1,w,A,HMANY,num+1 ,H,W)
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 ,H,W)
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 ,H,W)
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 ,H,W)

for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            dfs(h,w, A,HMANY, 0, H,W)

ans = 0
for h in range(H):
    ans = max(ans, max(HMANY[h]))
    # ans = max(ans, HMANY[h][w])
print(ans)

Submission status: Runtime Error

Solution code:


h,w = map(int, input().split(" "))

black_table = [ [ 0 if c == "#" else 99999999 for c in input() ] for _ in range(h) ]

#print(black_table)

blacks = []

for y, array in enumerate(black_table):
    for x,n in enumerate(array):
        if n == 0:
            blacks.append([y,x])


def erosion(y, x, n):
    if n < black_table[y][x] or n == 0:
        black_table[y][x] = n

        if y < h-1:
            erosion(y+1, x, n+1)
        if y > 0:
            erosion(y-1, x, n+1)
        if x < w-1:
            erosion(y, x+1, n+1)
        if x > 0:
            erosion(y, x-1, n+1)

for b in blacks:
    erosion(b[0], b[1], 0)

max_dis = 0

for array in black_table:
    for n in array:
        max_dis = max(max_dis, n)

print(max_dis)

Submission status: Runtime Error

Solution code:
h, w = [int(_) for _ in input().split()]
a = [list(input()) for __ in range(h)]
bl = []
wh = []
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            bl.append([i,j])
        else:
            wh.append([i,j])
d = []
for i in range(len(wh)):
    dmin = 1e9
    for j in range(len(bl)):
        dmin = min(dmin, abs(wh[i][0] - bl[j][0]) + abs(wh[i][1] - bl[j][1]))
    d.append(dmin)
d.sort()
print(d[-1])

Submission status: Runtime Error

Solution code:
import itertools

H, W = [int(_) for _ in input().split()]
A = [[_ for _ in input()] for _ in range(H)]
D = []
S = []
for x, y in itertools.product(range(H), range(W)):
    if A[x][y] == '.':
        D += [[x, y]]
    else:
        S += [[x, y]]

print(max(min([abs(X - x) + abs(Y - y) for x, y in S]) for X, Y in D))

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[i] == '#':
            bl.append([i,j])
        else:
            wh.append([i,j])

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

Submission status: Runtime Error

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

ans = 0
def f(h,w,count):
  global H,W,cou,B
  if h<0 or H<=h or w<0 or W<=w:
    return
  if A[h][w]=="#":
    cou = min(cou,count)
    k = 1
    return
  if B[h][w]>count:
    B[h][w]=count
  else:
    return
  f(h-1,w,count+1)
  f(h+1,w,count+1)
  f(h,w-1,count+1)
  f(h,w+1,count+1)
def return_map():
  B = [[10**10 for i in range(W)] for i in range(H)]
  return B
for x in range(H):
  for y in range(W):
    B = return_map()
    cou = 10**10
    f(x,y,0)
    ans = max(ans,cou)
print(ans)

Submission status: Runtime Error

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

import sys
sys.setrecursionlimit(10000)

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[0] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != 0:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque
INF = float("inf")


def solve(H: int, W: int, A: "List[str]"):
    # 黒の位置をx, y座標として持つ。
    black = []
    B = [[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if A[i][j] == "#":
                black.append((i, j))
            B[i][j] = 0 if A[i][j] == "#" else -INF  # 黒は1

    black2 = black.copy()
    black.sort(key=lambda x: x[0])
    black2.sort(key=lambda x: x[1])

    gene = 0
    q = deque()
    q.extend(black)
    q2 = deque()
    counter = len(black)
    while True:
        gene += 1
        while len(q) > 0:
            y, x = q.popleft()
            for yy, xx in zip([y-1, y, y+1, x], [x, x-1, x, x+1]):
                if 0 <= yy < H and 0 <= xx < W and B[yy][xx] < 0:
                    B[yy][xx] = gene
                    q2.append((yy, xx))
                    counter += 1
        q = q2
        q2 = deque()
        if counter == H*W:
            break
    print(gene)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [next(tokens) for _ in range(W)]  # type: "List[str]"
    solve(H, W, A)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
listA=[]
index=0
while index < h:
    a = list(input())
    listA.append(a)
    index+=1

flag=1
count=0
while flag:
    flag=0
    index=0
    while index < len(listA):
        index2=0
        while index2 < len(listA):
            if listA[index][index2]==".":
                if index!=0:
                    if listA[index-1][index2]=="#":
                        listA[index][index2] = "0"
                if index!=len(listA)-1:
                    if listA[index+1][index2] == "#":
                        listA[index][index2] = "0"
                if index2 != 0:
                    if listA[index][index2-1] == "#":
                        listA[index][index2] = "0"
                if index2 != len(listA[0])-1:
                    if listA[index][index2+1] == "#":
                        listA[index][index2] = "0"
                if listA[index][index2] == "0":
                    flag=1
            index2+=1
        index+=1
    index=0
    while index < len(listA):
        index2 = 0
        while index2 < len(listA):
            if listA[index][index2] == "0":
                listA[index][index2] = "#"       
            index2 += 1
        index += 1
    if flag:
        count += 1
print(count)

Submission status: Runtime Error

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

import sys
sys.setrecursionlimit(10000)

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[-1] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != -1:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Submission status: Runtime Error

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


def dfs(h,w,A,HMANY, num):
    if not ( 0<= h < H and 0 <= w < W) : 
        return
    if HMANY[h][w] > num:
        HMANY[h][w] = num        
        if 0 <= h-1 :
            dfs(h-1,w,A,HMANY,num+1 )
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 )
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 )
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 )

for h in range(H):
    for w in range(W):
        # if HMANY[h][w] == 10**9:
            if A[h][w] == "#":
                dfs(h,w, A,HMANY, 0)

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, HMANY[h][w])
print(ans)

Submission status: Runtime Error

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

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[-1] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != -1:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Submission status: Runtime Error

Solution code:
import copy
import collections

def completed(A,H,W):
    for h in range(H):
        if '.' in A[h]:
            return False
    return True

def solve():
    inf = 1 << 60
    H,W = map(int,input().split())
    A = [list(input()) for h in range(H)]
    B = [[inf] * W for h in range(H)]
    q = collections.deque()
    for h in range(H):
        for w in range(W):
            if A[h][w] == '#':
                B[h][w] = 0
                q.append(0,h,w)
    ans = 0
    try:
        while True:
            c,h,w = q.popleft()
            if h > 0 and A[h-1][w] == '.':
                if B[h-1][w] > c + 1:
                    B[h-1][w] = c + 1
                    q.append(c+1,h-1,w)
                    ans = max(ans, c+1)
            if w > 0 and A[h][w-1] == '.':
                if B[h][w-1] > c + 1:
                    B[h][w-1] = c + 1
                    q.append(c+1,h,w-1)
                    ans = max(ans, c+1)
            if h < H-1 and A[h+1][w] == '.':
                if B[h+1][w] > c + 1:
                    B[h+1][w] = c + 1
                    q.append(c+1,h+1,w)
                    ans = max(ans, c+1)
            if w < W-1 and A[h][w+1] == '.':
                if B[h][w+1] > c + 1:
                    B[h][w+1] = c + 1
                    q.append(c+1,h,w-1)
                    ans = max(ans, c+1)
    except IndexError as e:
        pass
    return ans

print(solve())

Submission status: Runtime Error

Solution code:
import copy

H, W = map(int, input().split())
matrix = [[0] * W for i in range(H)]
for i in range(H):
  line_list = list(input())
  for j in range(W):
    if line_list[j] == ".":  
      matrix[i][j] = 0
    else:
      matrix[i][j] = 1
      
cnt = 0
while(True):
  
  flg = True
  for i in range(H):
    for j in range(W):
      if matrix[i][j] == 0:
        flg = False
        break
  
  if flg:
    print(cnt)
    break
  
  matrix_copy = copy.deepcopy(matrix)
  cnt += 1
  for i in range(H):
    for j in range(W):
      if matrix[i][j] == 1:
        if i > 0:
          matrix_copy[i-1][j] = 1
        if i < H-1:
          matrix_copy[i+1][j] = 1
        if j > 0:
          matrix_copy[i][j-1] = 1
        if j < H-1:
          matrix_copy[i][j+1] = 1
   
  matrix = matrix_copy

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(wid):
      a[i].append(s[j])
      
  result = 0
  for i in range(dep):
    flg = 0
    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
      if 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())

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

a = [ list(input()) for _ in range(h) ]
ans = 0
while True:
    tmp = [ list("." *w) for _ in range(h) ]

    for y in range(h):
        for x in range(w):
            if a[x][y] == "#":
                tmp[x][y] = "#"
                for i in range(4):
                    nx = x+dx[i]
                    ny = y+dy[i]
                    if nx < 0 or ny < 0 or w <= nx or h <= ny:
                        continue
                    tmp[nx][ny] = "#"
    for y in range(h):
        for x in range(w):
            a[y][x] = tmp[y][x]

    ans += 1
    all_black = True
    for y in range(h):
        for x in range(w):
            if a[y][x] == ".":
                all_black = False
    if all_black:
        break

print(ans)

Submission status: Runtime Error

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


def dfs(h,w,A,HMANY, num):
    if HMANY[h][w] > num:
        HMANY[h][w] = num        
        if 0 <= h - 1 :
            dfs(h-1,w,A,HMANY,num+1 )
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 )
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 )
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 )

for h in range(H):
    for w in range(W):
        # if HMANY[h][w] == 10**9:
            if A[h][w] == "#":
                dfs(h,w, A,HMANY, 0)

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, HMANY[h][w])
print(ans)

Submission status: Runtime Error

Solution code:
H,W = (int(i) for i in input().split())
e = [[int(i) for i in input().split()] for i in range(H)]
result = 0
result += i.count("#") for i in e
onon = 0
while result != H * W:
    for tate in range(H):
        for yoko in range(W):
            if yoko == 0:
                if e[tate - 1][yoko] == "#" or  e[tate + 1][yoko] == "#" or  e[tate][yoko+1] == "#":
                e[tate][yoko] == "#"
                result += 1
            elif tate == 0:
                if e[tate + 1][yoko] == "#" or  e[tate][yoko-1] == "#" or  e[tate][yoko+1] == "#":
                e[tate][yoko] == "#"
                result += 1
            elif yoko == W - 1:
                if e[tate - 1][yoko] == "#" or  e[tate + 1][yoko] == "#" or  e[tate][yoko-1] == "#" :
                e[tate][yoko] == "#"
                result += 1
            elif tate == H - 1:
                if e[tate - 1][yoko] == "#" or  e[tate][yoko-1] == "#" or  e[tate][yoko+1] == "#":
                e[tate][yoko] == "#"
                result += 1
            else:
                if e[tate - 1][yoko] == "#" or  e[tate + 1][yoko] == "#" or  e[tate][yoko-1] == "#" or  e[tate][yoko+1] == "#":
                    e[tate][yoko] == "#"
                    result += 1
    onon += 1
print(onon)

Submission status: Runtime Error

Solution code:
def update_board(board):
    mirror = [[e for e in row] for row in board]
    for row in range(H):
        for column in range(W):
            if board[column][row] == '#':
                if row < H - 1:
                    mirror[row + 1][column] = '#'
                if row > 0:
                    mirror[row - 1][column] = '#'
                if column < W - 1:
                    mirror[row][column + 1] = '#'
                if column > 0:
                    mirror[row][column - 1] = '#'
    return mirror


def board_check(board):
    for row in range(H):
        for column in range(W):
            if board[row][column] == '.':
                return True
    return False


H, W = map(int, input().split())
board = [[c for c in input()] for row in range(H)]
count = 0
while board_check(board):
    board = update_board(board)
    count += 1
print(count)

Submission status: Runtime Error

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


def fillBlack(A, F):
    for h in range(H):
        for w in range(W):
            if A[h][w] == '.' and F[h][w] == 1:
                A[h] = A[h][:w] + '#' + A[h][w+1:]
                #A[h][w] = '#'


def checkExistWhite(A):
    for h in range(H):
        for w in range(W):
            if A[h][w] == '.':
                return True
    return False


ans = 0
existWhite = True
while existWhite:
    ans += 1
    F = [[0 for i in range(W)] for i in range(H)]
    for h in range(H):
        for w in range(W):
            if h-1 < H and A[h-1][w] == '#':
                F[h][w] = 1
            elif h+1 < H and A[h+1][w] == '#':
                F[h][w] = 1
            elif w-1 < H and A[h][w-1] == '#':
                F[h][w] = 1
            elif w+1 < H and A[h][w+1] == '#':
                F[h][w] = 1
    fillBlack(A, F)
    existWhite = checkExistWhite(A)

print(ans)

Submission status: Runtime Error

Solution code:

# coding: utf-8

# In[ ]:

H, W = input().split()
masu = [input().split() for _ in H]


# In[1]:

import numpy as np


# In[14]:

bin_masu = np.zeros((H, W))
masu = np.array(masu)
bin_masu[masu == '#'] = 1
#print(bin_masu.shape, masu.shape)
count = 0
while True:
    if np.all(bin_masu == 1):
        break
    hs, ws = np.where(bin_masu == 1)
    print(hs, ws)
    for i in range(len(hs)):
        if hs[i]-1 >= 0:
            bin_masu[hs[i]-1, ws[i]] = 1
        if hs[i]+1 <= H-1:
            bin_masu[hs[i]+1, ws[i]] = 1
        if ws[i]-1 >= 0:
            bin_masu[hs[i], ws[i]-1] = 1
        if ws[i]+1 <= W-1:
            bin_masu[hs[i], ws[i]+1] = 1
    count += 1
print(count)


# In[6]:

print(count)


# In[ ]: