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: Wrong Answer

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

M = {};

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


#print(M)

count = 0

#flg = False

import copy

while True:
    #flg = False

    #temp = [x[:] for x in M]

    temp = copy.deepcopy(M)

    count += 1

    for h in range(H):
        for w in range(W):
            if temp[h][w] == '#':
                #i = 0

                #print(h, w)

                #while i < 4:
                for i in range(4):
                    try:
                        if i == 0:
                            if h == 0:
                                #i += 1
                                continue
                            M[h - 1][w] = '#'
                            #i += 1

                        elif i == 1:
                            if h == H:
                                #i += 1
                                continue
                            M[h + 1][w] = '#'
                            #i += 1

                        elif i == 2:
                            if w == 0:
                                #i += 1
                                continue
                            M[h][w - 1] = '#'
                            #i += 1

                        elif i == 3:
                            if w == W:
                                #i += 1
                                continue
                            M[h][w + 1] = '#'
                            #i += 1

                    except IndexError:
                        #i += 1
                        pass

                    except KeyError:
                        #i += 1
                        pass

    flg = True

    #print(M == temp)

    for h in range(H):
        flg = '.' in M[h]   

        if flg == True:
            break

    #print(M)

    # print(count)
    
    # for h in range(H):
    #     for w in range(W):
    #         print(M[h][w], end="")

    #     print()

    if flg != True:
        break


print(count)

Submission status: Wrong Answer

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

A = []
D = [[-1 for w in range(W)] for h in range(H)]
for h in range(H):
    A.append(list(input()))

m = -1
for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            D[h][w] = 0
        elif 0 < w:
            if D[h][w - 1] >= 0:
                D[h][w] = D[h][w - 1] + 1
                if m < D[h][w]:
                    m = D[h][w]
    for w in range(W - 1)[::-1]:
        if 0 <= D[h][w + 1] and (D[h][w + 1] + 1 < D[h][w] or D[h][w] == -1):
            D[h][w] = D[h][w + 1] + 1
            if m < D[h][w]:
                m = D[h][w]

for h in range(1, H):
    for w in range(W):
        if 0 <= D[h - 1][w] and (D[h - 1][w] + 1 < D[h][w] or D[h][w] == -1):
            D[h][w] = D[h - 1][w] + 1
            if m < D[h][w]:
                m = D[h][w]
for h in range(H - 1)[::-1]:
    for w in range(W):
        if 0 <= D[h + 1][w] and (D[h + 1][w] + 1 < D[h][w] or D[h][w] == -1):
            D[h][w] = D[h + 1][w] + 1
            if m < D[h][w]:
                m = D[h][w]
print(m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
input = sys.stdin.readline
 
# bisect_left(lists, 3)
# bisect_right(lists, 3)
 
def main():
	H, W = map(int, input().split(" "))
	A = []
	shiro = 0
	
	for _ in range(H):
		tmp = input()
		tmp = tmp[:-1]
		tmp = list(tmp)
		for i in tmp:
			if i == ".":
				shiro += 1
		A.append(tmp)

	#if shiro == 0:
		#print(0)
		#sys.exit()
		
	#print(shiro)
	A2 = copy.deepcopy(A)
	trun = 0
	for _ in range(1000):
		trun += 1
		#print(shiro)
		for i in range(H):
			for j in range(W):
				if trun % 2 == 0:
					if A[i][j] == "#":
						if i < H-1 and A[i+1][j] == "." and A2[i+1][j] == ".":
							A2[i+1][j] = "#"
							shiro -= 1
						if i > 0 and A[i-1][j] == "." and A2[i-1][j] == ".":
							A2[i-1][j] = "#"
							shiro -= 1
						if j < W-1 and A[i][j+1] == "." and A2[i][j+1] == ".":
							A2[i][j+1] = "#"
							shiro -= 1
						if j > 0 and A[i][j-1] == "." and A2[i][j-1] == ".":
							A2[i][j-1] = "#"
							shiro -= 1
						A[i][j] == "+"
						A2[i][j] == "+"
						#print(A2)
				else:
					if A2[i][j] == "#":
						if i < H-1 and A2[i+1][j] == "." and A[i+1][j] == ".":
							A[i+1][j] = "#"
							shiro -= 1
						if i > 0 and A2[i-1][j] == "." and A[i-1][j] == ".":
							A[i-1][j] = "#"
							shiro -= 1
						if j < W-1 and A2[i][j+1] == "." and A[i][j+1] == ".":
							A[i][j+1] = "#"
							shiro -= 1
						if j > 0 and A2[i][j-1] == "." and A[i][j-1] == ".":
							A[i][j-1] = "#"
							shiro -= 1
						A[i][j] == "+"
						A2[i][j] == "+"
						#print(A)
				if shiro == 0:
					print(trun)
					sys.exit()

	print(trun)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import copy

h, w = list(map(int, input().split()))
data = []
for _ in range(h):
  data.append(list(map(lambda x: 1 if x == '#' else 0 , list(input()))))
count = 0
while True:
  tmp = copy.deepcopy(data)
  for i in range(h):
    for j in range(w):
      if data[i][j] != 1:
        if j > 0 and data[i][j-1] == 1:
          tmp[i][j] = 1 
        elif j < w-1 and data[i][j+1] == 1:
          tmp[i][j] = 1 
        elif i > 0 and data[i-1][j] == 1:
          tmp[i][j] = 1 
        elif i < h-1 and data[i+1][j] == 1:
          tmp[i][j] = 1 
  count += 1
  result = sum(map(sum, tmp))
  if result == h * w:
    break
  data = tmp
print(count)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

A = []
for i in range (H):
    a_s = list(map(str,input()))
    A.append(a_s)
    
A =np.array(A)

count = 0
temp = A.copy()
for _ in range (10**4):
    count +=1
    temp = A.copy()
    for i in range (H):
        for j in range (W):
            if A[i][j] == '#':
                if i-1>=0:
                    temp[i-1][j] = '#'
                if j-1>=0:
                    temp[i][j-1] = '#'
                if i+1<=H-1:
                    temp[i+1][j] = '#'
                if j+1<=W-1:
                    temp[i][j+1] = '#'
    if np.all(temp.flatten()== '#') == True:
        print(count)
        sys.exit()
    else:
        A = temp.copy()

Submission status: Wrong Answer

Solution code:
import copy

#入力取得
H, W = map(int, input().split())
l = [list(input()) for i in range(H)]


result = 0
flag = 1

while flag:
    flag = 0
    temp = copy.deepcopy(l)

    for i in range(len(l)):
        for j in range(len(l[i])):
            if l[i][j] == "#":
                if i + 1 < len(l):
                    if l[i + 1][j] == ".":
                        temp[i + 1][j] = "#"

                if j + 1 < len(l[0]):
                    if l[i][j + 1] == ".":
                        temp[i][j + 1] = "#"

                if i - 1 >= 0:
                    if l[i - 1][j] == ".":
                        temp[i - 1][j] = "#"

                if j - 1 >= 0:
                    if l[i][j - 1] == ".":
                        temp[i][j - 1] = "#"

    for line in temp:
        if line.count('.') > 0:
            flag = 1
            break
    l = copy.deepcopy(temp)

    result = result + 1


print(result)

Submission status: Wrong Answer

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

black = []
nextb = []
done = []
count = 0
allwhite = True
white = []
for i in range (H):
    line = input()
    l = []
    for j in range(W):
        if line[j] == '#':
            black.append([i,j])
        else:
            white.append([i,j])
        l.append(line[j])
    grid.append(l)
newgrid = grid
if H == 1 and W == 1:
    print(0)
else:
    if len(black) < len(white):
        while allwhite:
            for coord in black:
                if coord not in done:
                    y = coord[0]
                    x = coord[1]
                    if y > 0:
                        if grid[y-1][x] == '.':
                            grid[y-1][x] = '#'
                            nextb.append([y-1, x])
                    if y <= H-2:
                        if grid[y+1][x] == '.':
                            grid[y+1][x] = '#'
                            nextb.append([y+1, x])
                    if x > 0:
                        if grid[y][x-1] == '.':
                            grid[y][x-1] = '#'
                            nextb.append([y, x-1])
                    if x <= W-2:
                        if grid[y][x+1] == '.':
                            grid[y][x+1] = '#'
                            nextb.append([y, x+1])
                    done.append(coord)
            black = nextb
            nextb = []
            count += 1
            allwhite = False
            for row in grid:
                if '.' in row:
                    allwhite = True
    else:
        while len(white) > 1:
            for coord in white:
                y = coord[0]
                x = coord[1]
                if y > 0:
                    if grid[y-1][x] == '#':
                        newgrid[y][x] = '#'
                        white.remove(coord)
                        break
                if y <= H-2:
                    if grid[y+1][x] == '#':
                        newgrid[y][x] = '#'
                        white.remove(coord)
                        break
                if x > 0:
                    if grid[y][x-1] == '#':
                        newgrid[y][x] = '#'
                        white.remove(coord)
                        break
                if x <= W-2:
                    if grid[y][x+1] == '#':
                        newgrid[y][x] = '#'
                        white.remove(coord)
                        break
        count += 1
        grid = newgrid
        
    print(count)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
p = [(i, j) for i in range(h) for j in range(w) if a[i][j] == '#']
r = 0
while p:
    r += 1
    s = {
        (k, l) for i, j in p
        for k in range(i - 1, i + 2) for l in range(j - 1, j + 2)
        if 0 <= k < h and 0 <= l < w and a[k][l] == '.'
        }
    for i, j in s:
        a[i][j] = '#'
    p = s
print(r)

Submission status: Wrong Answer

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

M = {};

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


#print(M)

count = 0

#flg = False

import copy

while True:
    #flg = False

    #temp = [x[:] for x in M]

    temp = copy.deepcopy(M)

    count += 1

    for h in range(H):
        for w in range(W):
            if temp[h][w] == '#':
                #i = 0

                #print(h, w)

                #while i < 4:
                for i in range(4):
                    try:
                        if i == 0:
                            if h == 0:
                                #i += 1
                                continue
                            M[h - 1][w] = '#'
                            #i += 1

                        elif i == 1:
                            if h == H:
                                #i += 1
                                continue
                            M[h + 1][w] = '#'
                            #i += 1

                        elif i == 2:
                            if w == 0:
                                #i += 1
                                continue
                            M[h][w - 1] = '#'
                            #i += 1

                        elif i == 3:
                            if w == W:
                                #i += 1
                                continue
                            M[h][w + 1] = '#'
                            #i += 1

                    except IndexError:
                        #i += 1
                        pass

                    except KeyError:
                        #i += 1
                        pass

    flg = True

    #print(M == temp)

    for h in range(H):
        for w in range(W):
            if M[h][w] == ".":
                flg = False
                break   

        if flg == False:
            break

    #print(M)

    # print(count)
    
    # for h in range(H):
    #     for w in range(W):
    #         print(M[h][w], end="")

    #     print()

    if flg:
        break


print(count)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A_list = []
count = 0
for i in range(H):
    A_list.append(list(input()))
while True:
    flag = True
    for i in range(H):
        if A_list[i].count(".") != 0:
            flag = False
            break
    if flag == True:
        break
    index_list = []
    for i in range(H):
        for j in range(W):
            if A_list[i][j] == "#":
                index_list.append((i, j))
    for i, j in index_list:
        try:
            A_list[i][j-1] = "#"
        except:
            pass
        try:
            A_list[i][j+1] = "#"
        except:
            pass
        try:
            A_list[i-1][j] = "#"
        except:
            pass
        try:
            A_list[i+1][j] = "#"
        except:
            pass
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
bd=[[0 for i in range(w)] for j in range(h)]
s=[]
que=[]
for i in range(h):
  s.append(input())

for i in range(h):
  for j in range(w):
    if s[i][j]==".":
      bd[i][j]=0
    else:
      bd[i][j]=1

black=0
cnt=0
while(black<h*w):
  for i in range(h):
    for j in range(w):
      if bd[i][j]==1:
        que.append([i,j])
        if cnt==0:
          black+=1
      
  while(len(que)>0):
    i,j=que.pop(0)
    if i-1>=0 and bd[i-1][j]==0:
      black+=1
      bd[i-1][j]=1
    if i+1<=h-1 and bd[i+1][j]==0:
      black+=1
      bd[i+1][j]=1
    if j-1>=0 and bd[i][j-1]==0:
      black+=1
      bd[i][j-1]=1
    if j+1<=w-1 and bd[i][j+1]==0:
      black+=1
      bd[i][j+1]=1
  cnt+=1
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
from itertools import chain
import numpy as np
h,w=map(int,input().split())
s=[list(input())for i in range(h)]
a=[[float("inf")]*w for i in range(h)]
b=[[False]*w for i in range(h)]
d=set()
def f(x,y,t):
    if 0<=x<h and 0<=y<w:
        if s[x][y]=="#":
            if b[x][y]==False:
                a[x][y]=0
                b[x][y]=True
                f(x,y+1,1)
                f(x+1,y,1)
                f(x-1,y,1)
                f(x,y-1,1)
        else:
            if a[x][y]>t:
                a[x][y]=t
                f(x,y+1,t+1)
                f(x+1,y,t+1)
                f(x-1,y,t+1)
                f(x,y-1,t+1)
i,j= divmod(list(chain(*s)).index("#"), len(s[0]))
f(i,j,0)
print(np.array(a).max())
print(a)

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from operator import itemgetter

def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))

def update(bit,i, dp):
    bit[i] = dp
    return bit

def query(bit, i):
    lis = [bit[j] for j in range(i)]
    return max(lis)


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

H,W=MAP()

a = [input() for _ in range(H)]

black = []
lis_hidariue = []
lis_migiue = []
lis_hidarishita = []
lis_migishita = []

for i in range(H):
    for j in range(W):
        if a[i][j] == '#':
            black.append([i, j])
            lis_hidariue.append(i + j)
            lis_migiue.append( W - j + i - 1)
            lis_hidarishita.append( H - i + j - 1)
            lis_migishita.append(H + W - i - j - 2)

ct_black = len(black)

#print(black)

dis_kabe = max(min(lis_hidarishita),min(lis_hidariue),min(lis_migishita),min(lis_migiue))
#print(dis_kabe)

dis_point = []

if ct_black>=2:
    for i in range(ct_black-1):
        tmp = []
    
        for j in range(i+1, ct_black):
            tmp.append(abs(black[j][0] - black[i][0]) + abs(black[j][1] - black[i][1]))
        dis_point.append(int(min(tmp)/2)+1)
        #print('i:',i,'j:',j ,'dis_point:',dis_point)

    print(max(dis_kabe, max(dis_point)))
else:
    print(dis_kabe)

Submission status: Wrong Answer

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

lines = [input() for s in range(H)]
distance = [[-1 for i in range(W)] for j in range(H)]
for j in range(H):
  for i in range(W):
    if lines[j][i] == '#':
      distance[j][i] = 0
number = 0
check = 1
while(check > 0):
  for j in range(H):
    for i in range(W):
      if distance[j][i] == number:
        if j-1 >= 0:
          if distance[j-1][i] == -1:
            distance[j-1][i] = number + 1
        if j+1 <= H-1:
          if distance[j+1][i] == -1:
            distance[j+1][i] = number + 1
        if i-1 >= 0:
          if distance[j][i-1] == -1:
            distance[j][i-1] = number + 1
        if i+1 <= W-1:
          if distance[j][i+1] == -1:
            distance[j][i+1] = number + 1
  check = 0
  for j in range(H):
    for i in range(W):
      if distance[j][i] == -1:
        check += 1
  number += 1
print(number)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
check1 = [[pow(10, 5) for i in range(w)] for j in range(h)]

for i in range(h):
    A = list(input())
    for j in range(w):
        a = A[j]
        if a == "#":
            check1[i][j] = 1
        else:
            check1[i][j] = pow(10, 5)

#print(222, check1)
ans = 0
for i in range(h):
    for j in range(1, w):
        if check1[i][j - 1] >= 1:
            check1[i][j] = min(check1[i][j - 1] + 1, check1[i][j])
#print(check1)
for i in range(h):
    for j in range(w - 2, -1, -1):
        if check1[i][j + 1] >= 1:
            check1[i][j] = min(check1[i][j + 1] + 1, check1[i][j])
#print(check1)
for j in range(w):
    for i in range(1, h):
        if check1[i - 1][j] >= 1:
            check1[i][j] = min(check1[i - 1][j] + 1, check1[i][j])

for j in range(w):
    for i in range(h - 2, -1, -1):
        if check1[i + 1][j - 1] >= 1:
            check1[i][j] = min(check1[i + 1][j] + 1, check1[i][j])
            ans = max(ans, check1[i][j])

print(ans - 1)

Submission status: Wrong Answer

Solution code:
import copy
h,w = map(int ,input().split())
a = [list(input()) for l in range(h)]
ans = 0
while(True):
    ans += 1
    b = copy.deepcopy(a)
    print(a)
    count = 0
    for i in range(h):
        for j in range(w):
            if b[i][j] == "#":
                if i >= 1:
                    a[i-1][j] = "#"
                if j >= 1:
                    a[i][j-1] = "#"
                if i + 1 < h:
                    a[i+1][j] = "#"
                if j + 1 < w:
                    a[i][j+1] = "#" 
                count += 1
    print(ans,count)
    if count == h*w:
        ans -= 1
        break

print(ans)

Submission status: Wrong Answer

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

B = [[-1] * W for i in range(H)]

by = []
bx = []
for i in range(H):
    for j in range(W):
        if A[i][j] == '#':
            B[i][j] = 0
            by.append(i)
            bx.append(j)

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

ans = 0
b = 0
while True:
    ans += 1
    b += 1    
    for i in range(H):
        for j in range(W):
            for k in range(4):
                y = i + dy[k]
                x = j + dx[k]
                if 0 <= y < H and 0 <= x < W and B[i][j] == -1 and -1 < B[y][x] < b:
                    B[i][j] = b   
    
    clear = True
    for i in range(H):
        for j in range(W):
            if B[i][j] < 0:
                clear = False
                break
    
    if clear:
        break

print(ans)

Submission status: Wrong Answer

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

h,w = [int(i) for i in input().split()]
board = [input() for j in range(h)] # 縦「n」行

d4 = [[1,0],[0,1],[-1,0],[0,-1]]
memo = [[-1]*w for j in range(h)]
dist = [[100000]*w for j in range(h)]


q = collections.deque()
for i in range(h):
    for j in range(w):
        if board[i][j] == "#":
            memo[i][j] = 0
            q.append((i,j,0))

while len(q) != 0:
#    print(memo)
    i,j,c = q.popleft()
    if memo[i][j] < 0:
        continue
#    print(i,j)
    for k in d4:
#        print(k)
        ii,jj = k
        if  i+ii < 0 or i+ii>=h or j+jj <0 or j+jj>=w:
#            print("out",i+ii,j+jj)
            continue
        if memo[i+ii][j+jj] < 0:
#            print("put")
            q.append((i+ii,j+jj,c+1))
            memo[i+ii][j+jj] = 1
        dist[i][j] = c
    
    
ans = 0      
for i in range(h):
    for j in range(w):
        ans = max(ans,dist[i][j])

print(ans)

Submission status: Wrong Answer

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

M = {};

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


#print(M)

count = 0

#flg = False

import copy

while True:
    #flg = False

    #temp = [x[:] for x in M]

    temp = copy.deepcopy(M)

    count += 1

    for h in range(H):
        for w in range(W):
            if temp[h][w] == '#':
                i = 0

                #print(h, w)

                while i < 4:
                    try:
                        if i == 0:
                            if h == 0:
                                i += 1
                                continue
                            M[h - 1][w] = '#'
                            i += 1

                        elif i == 1:
                            if h == H:
                                i += 1
                                continue
                            M[h + 1][w] = '#'
                            i += 1

                        elif i == 2:
                            if w == 0:
                                i += 1
                                continue
                            M[h][w - 1] = '#'
                            i += 1

                        elif i == 3:
                            if w == W:
                                i += 1
                                continue
                            M[h][w + 1] = '#'
                            i += 1

                    except IndexError:
                        i += 1

                    except KeyError:
                        i += 1

    flg = True

    #print(M == temp)

    for h in range(H):
        for w in range(W):
            if M[h][w] == ".":
                flg = False
                break   

        if flg == False:
            break

    #print(M)

    # print(count)
    
    # for h in range(H):
    #     for w in range(W):
    #         print(M[h][w], end="")

    #     print()

    if flg:
        break


print(count)

Submission status: Wrong Answer

Solution code:
import sys 
H, W = list(map(int, sys.stdin.readline().strip().split()))
A = [["" for _ in range(W)] for _ in range(H)]
for i in range(H):
   for j, c in enumerate(list(sys.stdin.readline().strip())):
      A[i][j] = c

print(H, W, A)

que = []

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

step = -1

while len(que) != 0:
   new_que = []
   for (i, j) in que:
      for (x, y) in [(i+1, j), (i-1, j), (i, j+1), (i, j-1)]:
         if x >= 0 and x < H and y >=0 and y < W and A[x][y] == ".":
            new_que.append((x, y))
            A[x][y] = "#"
   step += 1
   que = new_que
print(step)

Submission status: Wrong Answer

Solution code:
import copy

h, w = map(int, input().split())
a = []
for i in range(h):
    s = str(input())
    a.append([])
    for x in s:
        if x == '.':
            a[i].append(1)
        else:
            a[i].append(0)

ans = 0

while True:
    _a = copy.deepcopy(a)
    for _h in range(h):
        for _w in range(w):
            if a[_h][_w] == 0:
                if _w-1 >= 0:
                    _a[_h][_w-1] = 0
                if _w+1 <= w-1:
                    _a[_h][_w+1] = 0
                if _h-1 >= 0:
                    _a[_h-1][_w] = 0
                if _h+1 <= h-1:
                    _a[_h+1][_w] = 0
    ans += 1
    a = copy.deepcopy(_a)
    cnt = 0
    for _h in range(h):
        cnt += sum(a[_h])
    if cnt == 0:
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
X = [list(map(str, list(input()))) for i in range(H)]
A = []
ans = 0
for i in X:
    tmp = []
    for j in i:
        if j == "#":
            tmp.append(0)
        elif j == ".":
            tmp.append(1)
    else:
        A.append(tmp)

B = [x[:] for x in A]

cnt = 1
while cnt:
    cnt = 0
    ans+=1
    A = [x[:] for x in B]
    for y in range(H):
        for x in range(W):
            if A[y][x] == 0:
                if x - 1 >= 0:
                    B[y][x - 1] = 0
                if x + 1 < W:
                    B[y][x + 1] = 0
                if y - 1 >= 0:
                    B[y - 1][x] = 0
                if y + 1 < H:
                    B[y + 1][x] = 0
    for i in B:
        cnt += i.count(1)

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

h,w = map(int,input().split())
f = [[True for i in range(w)] for j in range(h)]
blist = []
temp_list = []
for i in range(h):
    s = input()
    lflag =False
    for j in range(w):
        if s[j] == "#":
            f[i][j] = False
            blist.append([i,j])
            lflag = True
    if not lflag:
        temp_list.append([i,0])
        temp_list.append([i,w-1])


ul = [0,0]
ur = [0,0]
bl = [0,0]
br = [0,0]
has_white = False

flag = False
for i in range(h):
    for j in range(w):
        if f[i][j]:
            ul = [i,j]
            flag = True
            has_white = True
            break
    if flag:
        break
flag = False
for i in range(h):
    for j in range(w):
        if f[i][w-1-j]:
            ur = [i,w-1-j]
            flag = True
            has_white = True
            break
    if flag:
        break
flag = False
for i in range(h):
    for j in range(w):
        if f[h-1-i][j]:
            bl = [h-i-1,j]
            flag = True
            has_white = True
            break
    if flag:
        break
flag = False
for i in range(h):
    for j in range(w):
        if f[h-1-i][w-1-j]:
            br = [h-i-1,w-j-1]
            flag = True
            has_white = True
            break
    if flag:
        break

u = [ul,ur,bl,br]

for t in temp_list:
    u.append([t[0],t[1]])
ans = 10000000000000
m = [10000000000000 for i in range(len(u))]
if has_white:
    for bp in blist:
        for i in range(len(u)):
            temp = abs(bp[0]-u[i][0]) + abs(bp[1]-u[i][1])
            if m[i] > temp:
                m[i] = temp
    print(max(m))
else:
    print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy

h, w = list(map(int, input().split()))
data = []
for _ in range(h):
  data.append(list(map(lambda x: 1 if x == '#' else 0 , list(input()))))
count = 0
while True:
  tmp = copy.deepcopy(data)
  for i in range(h):
    for j in range(w):
      if j > 0 and data[i][j-1] == 1:
        tmp[i][j] = 1 
      elif j < w-1 and data[i][j+1] == 1:
        tmp[i][j] = 1 
      elif i > 0 and data[i-1][j] == 1:
        tmp[i][j] = 1 
      elif i < h-1 and data[i+1][j] == 1:
        tmp[i][j] = 1 
  count += 1
  result = sum(map(sum, tmp))
  if result == h * w:
    break
  data = tmp
print(count)

Submission status: Wrong Answer

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

A = []
D = [[-1 for w in range(W)] for h in range(H)]
for h in range(H):
    A.append(list(input()))

for h in range(H):
    for w in range(W):
        if A[h][w] == '#':
            D[h][w] = 0
        elif 0 < w:
            if D[h][w - 1] >= 0:
                D[h][w] = D[h][w - 1] + 1
    for w in range(W):
        if w < W - 2:
            if 0 <= D[h][w + 1] and (D[h][w + 1] + 1 < D[h][w] or D[h][w] == -1):
                D[h][w] = D[h][w + 1] + 1

for h in range(1, H):
    for w in range(W):
        if 0 <= D[h - 1][w] and (D[h - 1][w] + 1 < D[h][w] or D[h][w] == -1):
            D[h][w] = D[h - 1][w] + 1
for h in range(H - 1)[::-1]:
    for w in range(W):
        if 0 <= D[h + 1][w] and (D[h + 1][w] + 1 < D[h][w] or D[h][w] == -1):
            D[h][w] = D[h + 1][w] + 1

m = -1
for h in range(H):
    for w in range(W):
        if m < D[h][w]:
            m = D[h][w]
print(m)