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:
from sys import stdin
import sys


H, W = [int(x) for x in stdin.readline().rstrip().split()]

data = [list(stdin.readline()) for _ in range(H)]

counter = 0


def checker(arr):
    for i in range(H):
        if '.' in data[i]:
            return False
    return True


while(True):
    for i in range(H):
        for j in range(W):
            if data[i][j] == '#':
                if i != 0:
                    data[i-1][j] = '#'
                if i != H-1:
                    data[i+1][j] = '#'
                if j != 0:
                    data[i][j-1] = '#'
                if j != W-1:
                    data[i][j+1] = '#'

    counter += 1
    if checker(data):
        print(counter)
        sys.exit()

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
a = [[] for j in range(h)]
ans = 0
for i in range(h):
    a[i] = [x for x in input()]
dp1 = [[1000001 for i in range(w)] for j in range(h)]
dp2 = [[1000001 for i in range(w)] for j in range(h)]
dp3 = [[1000001 for i in range(w)] for j in range(h)]
dp4 = [[1000001 for i in range(w)] for j in range(h)]
for k,po in zip([dp1,dp2,dp3,dp4],[[0,0],[0,w-1],[h-1,0],[h-1,w-1]]):
    finw = -1 if po[1] != 0 else w
    finh = -1 if po[0] != 0 else h
    addw = -1 if po[1] != 0 else 1
    addh = -1 if po[0] != 0 else 1
    for hh in range(po[0],finh,addh):
        for ww in range(po[1],finw,addw):
            #print(ww,hh)
            for y,x in zip([1,-1,0,0],[0,0,-1,1]):
                if hh+y >= h or ww+x >= w or hh+y < 0 or ww+x < 0:continue
                if a[hh+y][ww+x] == '#':
                    k[hh][ww] = 1
                    break
                #print(y,x)
                #print(hh,ww,hh+y,ww+x)
                k[hh][ww] = min(k[hh][ww],k[hh+y][ww+x] + 1)
                if a[hh][ww] == '#':
                    k[hh][ww] = 0
                    break
for hh in range(h):
    for ww in range(w):
        point = 1000000000000000
        for k in [dp1,dp2,dp3,dp4]:
            if k[hh][ww] == 1000001:continue
            point = min(point,k[hh][ww])
        ans = max(ans,point)
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
HW = [[] for h in range(H)]
for h in range(H):
    hw = list(input())
    for w in hw:
        HW[h].append(w)

count_value = 0
# 白が無かったら終了
white = True
while white == True:
    # 黒を探す
    blackH = list()
    blackW = list()
    for h in range(H):
        for w in range(W):
            if HW[h][w] == '#':
                # 黒の位置を記録
                blackH.append(h)
                blackW.append(w)
    # 記録した位置の縦列を黒に
    for h in range(H):
        for w in range(W):
            if w in blackW:
                HW[h][w] = '#'
    # 記録した位置の横列を黒に
    for h in range(H):
        if h in blackH:
            for w in range(W):
                HW[h][w] = '#'
    # 白を探し、記録する
    white = False
    for h in range(H):
        for w in range(W):
            if HW[h][w] == '.':
                white = True

    count_value += 1
    
print(count_value)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# agc033/agc033_a
import re
import math
import fractions
from pprint import pprint


[H, W] = [int(x) for x in input().split()]
A = [[0 for col in range(W)] for row in range(H)]
B = []

for i in range(H):
    input_str = input()
    for j in range(W):
        A[i][j] = int(input_str[j] == '#')
        if not A[i][j]:
            B.append((i, j))


def main():
    n = 0
    pprint(A)
    while True:
        C = []
        for k in range(len(B)-1, 0, -1):
            [i, j] = B[k]
            if ((i > 0 and A[i-1][j]) or
                    (i < H - 1 and A[i+1][j]) or
                    (j > 0 and A[i][j-1]) or
                    (j < W - 1 and A[i][j+1])):
                C.append(B.pop(k))
        if C:
            n += 1
            for l in range(len(C)):
                [i, j] = C[l]
                A[i][j] = 1
        else:
            break
    print(str(n))


main()

Submission status: Wrong Answer

Solution code:
h, w = [int(i) for i in input().split()]
c = [[float("inf") for _ in range(w)] for _ in range(h)]
for i in range(h):
    _a = input()
    for j in range(w):
        if _a[j] == "#":
            c[i][j] = 0
#右方向
def right():
    for i in range(h):
        for j in range(1, w):
            #if c[i][j-1] != -1:
            c[i][j] = min(c[i][j], c[i][j-1]+1)

def left():
    for i in range(h):
        for j in reversed(range(w-1)):
            # if c[i][j + 1] != -1:
            c[i][j] = min(c[i][j], c[i][j+1]+1)

def down():
    for i in range(1, h):
        for j in range(w):
            # if c[i-1][j] != -1:
            c[i][j] = min(c[i][j], c[i - 1][j]+1)

def up():
    for i in reversed(range(h-1)):
        for j in range(w):
            # if c[i+1][j] != -1:
            c[i][j] = min(c[i][j], c[i + 1][j]+1)

right()
left()
down()
up()
_max = 0
exit()
for i in range(len(c)):
    # print(c[i])
    _max = max(_max, max(c[i]))
print(_max)

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] == '.':
                continue

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

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

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

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

    flg = True

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

        if flg == True:
            break

    # 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 = 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(check1)
#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])

        if check1[i][w - j] >= 1:
            check1[i][w - j - 1] = min(check1[i][w - j] + 1, check1[i][w - j - 1])
ans = 1
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])

        if check1[h - i][j] >= 1:
            check1[h - i - 1][j] = min(check1[h - i][j] + 1, check1[h - i - 1][j])
        ans = max(ans, check1[i][j])

print(check1)
print(ans - 1)

Submission status: Wrong Answer

Solution code:
import copy

h_max,w_max = [int(i) for i in input().split()]
a = [input() for i in range(h_max)]

numOfBlack = 0
result = 0

while(numOfBlack < (h_max * w_max)):
    a_tmp = copy.deepcopy(a)
    numOfBlack = 0
    for h in range(h_max):
        for w in range(w_max):
            if(a[h][w] == '#'):
                if((h-1) >= 0):
                    a_tmp[h-1] = a_tmp[h-1][:w] +'#' + a_tmp[h-1][w+1:]
                if((h+1) < h_max):
                    a_tmp[h+1] = a_tmp[h+1][:w] +'#' + a_tmp[h+1][w+1:]
                if((w-1) >= 0):
                    a_tmp[h] = a_tmp[h][:w-1] +'#' + a_tmp[h][w:]
                if((w+1) < w_max):
                    a_tmp[h] = a_tmp[h][:w+1] +'#' + a_tmp[h][w+2:]
        numOfBlack = numOfBlack + a_tmp[h].count('#')
    a = copy.deepcopy(a_tmp)
    result = result + 1

print(result)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
c_list = list()
for i in range(H):
  b_list = list(map(lambda x: x, input()))
  for w in range(W):
    if b_list[w] == '#':
      c_list.append([i, w])
ans = 1
for i in range(H+W):
  ll = c_list.copy()
  for c in ll:
    if not c[0]-1 < 0 and not [c[0]-1, c[1]] in ll:
      c_list.append([c[0]-1, c[1]])
    if not c[0]+1 > W-1 and not [c[0]+1, c[1]] in ll:
      c_list.append([c[0]+1, c[1]])
    if not c[1]-1 < 0 and not [c[0], c[1]-1] in ll:
      c_list.append([c[0], c[1]-1])
    if not c[1]+1 > W-1 and not [c[0], c[1]+1] in ll:
      c_list.append([c[0], c[1]+1])
    c_list = list(set(map(tuple, c_list)))
  if len(c_list) == H*W:
    break;
  else:
    ans += 1
  
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] == '.':
                continue

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

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

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

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

    flg = True

    if count % 5 == 0:
        for h in range(H):
            flg = '.' in M[h]   

            if flg == True:
                break

    # 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:
import copy
h, w = map(int, input().split())
field = []
for i in range(h):
    field.append(list(input()))

count = 0
continue_ = False
new_field = copy.deepcopy(field)
while True:
    count += 1
    for h_i in range(h):
        for w_i in range(w):
            if field[h_i][w_i] == '#':
                new_field[max(h_i-1, 0)][w_i] = '#'
                new_field[h_i][max(w_i-1, 0)] = '#'
                new_field[min(h_i+1, h-1)][w_i] = '#'
                new_field[h_i][min(w_i+1, w-1)] = '#'
        if not continue_ and new_field[h_i].count('.'):
            continue_ = True
    if not continue_:
        break
    continue_ = False
    field = new_field
    new_field = copy.deepcopy(field)

print(count)

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] == 1:
                if _w-1 >= 0:
                    if a[_h][_w-1] == 0:
                        _a[_h][_w] = 0
                if _w+1 <= w-1:
                    if a[_h][_w+1] == 0:
                        _a[_h][_w] = 0
                if _h-1 >= 0:
                    if a[_h-1][_w] == 0:
                        _a[_h][_w] = 0
                if _h+1 <= h-1:
                    if a[_h+1][_w] == 0:
                        _a[_h][_w] = 0
    ans += 1
    a = copy.deepcopy(_a)
    cnt = sum([sum(x) for x in a])
    if cnt == 0:
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
a=[]
for i in range(h):
    tmp=input()
    tmp2=[]
    for j in tmp:
        if j=='.':
            a.append(0)
        else:
            a.append(1)
import copy
b=a.copy()
count=0
flag=1
while flag==1:
    flag=0
    count+=1
    for i in range(len(a)):
        if a[i]==0:
            flag=1
            try:
                b[i]+=a[i+h]*1
            except:
                pass
            try:
                b[i]+=a[i-h]*1
            except:
                pass
            if i%6!=5:
                try:
                    b[i]+=a[i+1]*1
                except:
                    pass
            if i%6!=0:
                try:
                    b[i]+=a[i-1]*1
                except:
                    pass
    a=b.copy()
print(count-1)

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 - 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 0 < h and 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 = 0
for h in range(H):
    for w in range(W):
        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()
				if shiro < 4 :
					print(trun+1)
					sys.exit()

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

Submission status: Wrong Answer

Solution code:
import copy
n=[int(n) for n in input().split(" ")]
squares=[]
while True:
  try:
  	s=list(input())
  except EOFError:
    break
  squares.append(s)
tmp=copy.deepcopy(squares)
num=0
while True:
  for i in range(n[0]):
    for j in range(n[1]):
      if(squares[i][j]=='#'):
        if(i-1>-1):
          tmp[i-1][j]='#'
        if(i+1<n[0]):
          tmp[i+1][j]='#'
        if(j-1>-1):
          tmp[i][j-1]='#'
        if(j+1<n[1]):
          tmp[i][j+1]='#'
        
  num+=1
  squares=copy.deepcopy(tmp)
  flg=True
  for i in range(n[0]):
    for j in range(n[1]):
      if squares[i][j] != '#':
        flg=False
  if flg or num>10:
    break
print(num)

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:
    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] == '.'
        }
    if s:
        r += 1
    for i, j in s:
        a[i][j] = '#'
    p = s
print(r)

Submission status: Wrong Answer

Solution code:
import copy

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

arr = []
for ih in range(h):
  tmp_arr = []
  x = str(input())
  for iw in range(w):
    tmp_arr.append(x[iw])
  arr.append(tmp_arr)

arr_next = copy.deepcopy(arr)
cnt = 0
flg = 0

while flg == 0:
  flg = 1
  for ih in range(h):
    for iw in range(w):
      tmp_cell = ''
      if arr[ih][iw] == '#':
        arr_next[ih][iw] = '#'
      else:
        if ih - 1 >= 0:
          if arr[ih - 1][iw] == '#' and arr_next[ih][iw] == '.':
            arr_next[ih][iw] = '#'
        if ih + 1 < h:
          if arr[ih + 1][iw] == '#' and arr_next[ih][iw] == '.':
            arr_next[ih][iw] = '#'
        if iw - 1 >= 0:
          if arr[ih][iw - 1] == '#' and arr_next[ih][iw] == '.':
            arr_next[ih][iw] = '#'
        if iw + 1 < w:
          if arr[ih][iw + 1] == '#' and arr_next[ih][iw] == '.':
            arr_next[ih][iw] = '#'
      if arr_next[ih][iw] == '.':
        flg = 0
  arr = copy.deepcopy(arr_next)
  cnt = cnt + 1

print(cnt)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
c_list = list()
for i in range(H):
  b_list = list(map(lambda x: x, input()))
  for w in range(W):
    if b_list[w] == '#':
      c_list.append([i, w])
ans = 1
for i in range(H+W):
  ll = c_list.copy()
  for c in ll:
    if not c[0]-1 < 0 and not [c[0]-1, c[1]] in ll:
      c_list.append([c[0]-1, c[1]])
    if not c[0]+1 > W-1 and not [c[0]+1, c[1]] in ll:
      c_list.append([c[0]+1, c[1]])
    if not c[1]-1 < 0 and not [c[0], c[1]-1] in ll:
      c_list.append([c[0], c[1]-1])
    if not c[1]+1 > W-1 and not [c[0], c[1]+1] in ll:
      c_list.append([c[0], c[1]+1])
  c_list = list(set(map(tuple, c_list)))
  if len(c_list) == H*W:
    break;
  else:
    ans += 1
  
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] == '.':
                continue

            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 - 1:
                            #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 - 1:
                            #i += 1
                            continue
                        M[h][w + 1] = '#'
                        #i += 1

                    # except IndexError:
                    #     #i += 1
                    #     pass

                    # except KeyError:
                    #     #i += 1
                    #     pass

    flg = True

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

        if flg == True:
            break

    # 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 = map(int,input().split())
A=[]
"""
3 3
...
.#.
...
"""
for i in range(H):
    b = input()
    c = []
    for j in range(W):
        c.append(b[j])
    A.append(c)
B = A[:]
flag=True


count = 0
allcount=0
while(flag):
    for i in range(H):
        for j in range(W):
        #白色か判定
            if A[i][j] == '.':
                #四隅
                if i == 0 and j == 0:
                    if A[i+1][j]=='#' or A[i][j+1]=='#':
                        B[i][j]='#'
                elif i == 0 and j == W-1:
                    if A[i+1][j]=='#' or A[i][j-1]=='#':
                        B[i][j]='#'
                elif i == H-1 and j == 0:
                    if A[i-1][j]=='#' or A[i][j+1]=='#':
                        B[i][j]='#'
                elif i == H-1 and j == W-1:
                    if A[i-1][j]=='#' or A[i][j-1]=='#':
                        B[i][j]='#'
                #列
                elif i == 0:
                    if A[i+1][j]=='#' or A[i][j+1]=='#' or A[i][j-1]=='#':
                        B[i][j]='#'
                elif j == 0:
                    if A[i+1][j]=='#' or A[i][j+1]=='#' or A[i-1][j]=='#':
                        B[i][j]='#'
                elif i == H-1:
                    if A[i-1][j]=='#' or A[i][j+1]=='#' or A[i][j-1]=='#':
                        B[i][j]='#'
                elif j == W-1:
                    if A[i+1][j]=='#' or A[i-1][j]=='#' or A[i][j-1]=='#':
                        B[i][j]='#'
                #中央
                elif A[i-1][j]=='#' or A[i+1][j]=='#' or A[i][j-1]=='#' or A[i][j+1]=='#':
                        B[i][j]='#'
            else:
                count +=1
    A = B[:]
    allcount+=1
    if count == H*W:
        flag = False
print(allcount)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
list1 = ["."*(w+2)]+["."+input()+"." for i in range(h)]+["."*(w+2)]
cnt = 0
maxcnt = 0
for j in range(h+1):
  for k in range(w+1):
    if list1[j][k] == '.' and cnt == 0:
      cnt+=1
    if list1[j][k-1]=='.' and list1[j][k+1]=='.' and list1[j+1][k]=='.' and list1[j-1][k]=='.':
      if j != 0 and k != 0:  
        cnt+=1
    else:
      if cnt > maxcnt:
        maxcnt = cnt
      cnt = 0
print(maxcnt)

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] == '.':
                continue

            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

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

        if flg == True:
            break

    # 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 = (int(x) for x in input().split())

fl = []
count = 0
ck = 0

for i in range(H):
    a = input()
    fl.append(list(a))


def kuro(w, h):
    if w != 0:
        fl[h][w-1] = "#"
    if w != W-1:
        fl[h][w+1] = "#"
    if h != 0:
        fl[h-1][w] = "#"
    if h != H-1:
        fl[h+1][w] = "#"


def check():
    for l in fl:
        if "." in l:
            return 0
    return 1


while ck == 0:
    nlist = []
    for j in range(H):
        for k in range(W):
            if fl[j][k] == "#":
                nlist.append(k)
                nlist.append(j)
    for l in range(0, len(nlist), 2):
        kuro(int(nlist[l]), int(nlist[l+1]))
    count += 1
    ck = check()

print(count)

Submission status: Wrong Answer

Solution code:
def main():
  dep, wid = map(int, input().split())
  a = [["" for i in range(wid)] for j in range(dep)]
  for i in range(dep):
    s = input()
    for j in range(len(s)):
      a[i][j] = 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 = 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 = sum([sum(x) for x in a])
    if cnt == 0:
        print(ans)
        break

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 = 0
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:
import pprint
H, W = map(int, input().split())
black = []

for i in range(H):
  a = list(input())
  b = []
  for j in range(W):
    if (a[j] == '#'):
      b.append(1)
    else:
      b.append(0)
  black.append(b)

ans = 0
while (1):
  tmp = []
  for i in range(H):
    for j in range(W):
      if (i == 0):
        a = 0
      else:
        a = black[i-1][j]
      if (i == H - 1):
        c = 0
      else:
        c = black[i+1][j]
      if (j == 0):
        d = 0
      else:
        d = black[i][j-1]
      if (j == W - 1):
        b = 0
      else:
        b = black[i][j+1]
      if (1 in [a, b, c, d]):
        tmp.append([i, j])
  for k in range(len(tmp)):
    black[tmp[k][0]][tmp[k][1]] += 1
  count = True
  ans += 1
  for l in range(H):
    if (0 in black[l]):
      count = False
  if (count):
    print(ans)
    exit()