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:
hw = list(map(int, list(input().split())))
h = hw[0]
w = hw[1]

a = []
for i in range(h):
    apend = list(input())
    a.append(apend)

white = []

for i in range(h):
    for j in range(w):
        if a[i][j] == ".":
            white.append([i, j])

number1 = []
for i in range(h):
    number = []
    for j in range(w):
        if a[i][j] == ".":
            if i == 0:
                if j == 0:
                    number.append(1)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number1[i - 1][j] + 1)
                else:
                    number.append(min(number[j - 1], number1[i - 1][j]) + 1)
        else:
            number.append(0)
    number1.append(number)

#print(number1)

number2 = []
for i in range(h):
    number = []
    for j in range(w):
        if a[i][- j - 1] == ".":
            if i == 0:
                if j == 0:
                    number.append(1)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number2[i - 1][- j - 1] + 1)
                else:
                    number.append(min(number[j - 1], number2[i - 1][- j - 1]) + 1)
        else:
            number.append(0)
    newnumber = []
    for k in number[::-1]:
        newnumber.append(k)
    number2.append(newnumber)

#print(number2)


number3 = []
for i in range(h):
    number = []
    for j in range(w):
        if a[- i - 1][j] == ".":
            if i == 0:
                if j == 0:
                    number.append(1)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number3[i - 1][j] + 1)
                else:
                    number.append(min(number[j - 1], number3[i - 1][j]) + 1)
        else:
            number.append(0)
    number3.append(number)

number6 = number3
number3 = []
for i in number6[::-1]:
    number3.append(i)

#print(number3)

number4 = []
for i in range(h):
    number = []
    for j in range(w):
        if a[- i - 1][- j - 1] == ".":
            if i == 0:
                if j == 0:
                    number.append(1)
                else:
                    number.append(number[j - 1] + 1)
            else:
                if j == 0:
                    number.append(number4[i - 1][- j - 1] + 1)
                else:
                    number.append(min(number[j - 1], number4[i - 1][- j - 1]) + 1)
        else:
            number.append(0)
    newnumber = []
    for k in number[::-1]:
        newnumber.append(k)
    number4.append(newnumber)

number6 = number4
number4 = []
for i in number6[::-1]:
    number4.append(i)

#print(number4)

number5 = []

if len(white) == 0:
    print(0)
else:
    for i in range(len(white)):
        number5.append(min(number1[white[i][0]][white[i][1]], number2[white[i][0]][white[i][1]], number3[white[i][0]][white[i][1]], number4[white[i][0]][white[i][1]]))
    print(max(number5))

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

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

ended = False
count = 0
while not ended:
  As_copy = deepcopy(As)
  for i in range(H):
    for j in range(W):
      for dh, dw in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if i+dh >= H or j+dw >= W:
          continue
        if As[i][j] == "." and As[i+dh][j+dw] == "#":
          As_copy[i][j] = "#"
  As = As_copy
  count += 1
  ended = is_end(As)

print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = [int(x) for x in stdin.readline().rstrip().split()]
Input = [list(stdin.readline().rstrip()) for _ in range(N[0])]


Room = [[1 for j in range(N[1] + 2)] for i in range(N[0] + 2)]
Room_next = [[1 for j in range(N[1] + 2)] for i in range(N[0] + 2)]
count = 0


def is_darkroom(array):
    p = True
    for i in range(1, N[0] + 1, 3):
        for j in range(1, N[1] + 1, i):
            if array[i][j] == 1:
                p = False
    return p

for i in range(N[0] + 2):
    for j in range(N[1] + 2):
        if i == 0 or i == N[0] + 1 or j == 0 or j == N[1] + 1:
            Room[i][j] = 1
        else:
            Room[i][j] = 1 if Input[i-1][j-1] == "." else 0

for k in range(N[0] + N[1] + 2):
    if k % 2 == 0:
        if is_darkroom(Room):
            count = k
            break
        for i in range(1, N[0] + 1):
            for j in range(1, N[1] + 1):
                Room_next[i][j] = Room[i][j] * Room[i - 1][j] * Room[i + 1][j] * Room[i][j - 1] * Room[i][j + 1]

        Room = [[1 for j in range(N[1] + 2)] for i in range(N[0] + 2)]
    else:
        if is_darkroom(Room_next):
            count = k
            break
        for i in range(1, N[0] + 1):
            for j in range(1, N[1] + 1):
                Room[i][j] = Room_next[i][j] * Room_next[i - 1][j] * Room_next[i + 1][j] * Room_next[i][j - 1] * Room_next[i][j + 1]

        Room_next = [[1 for j in range(N[1] + 2)] for i in range(N[0] + 2)]       

print(count)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

string_list=[input() for i in range(h)]

string_dict = {}


for i, s in enumerate(string_list):
    for j, c in enumerate(list(s)):
        if c == '#':
            string_dict[(i, j)] = ('#', 0)
        else:
            string_dict[(i, j)] = ('.', 0)


def check_set_black(i, j, h, w, count, string_dict):
    regist_count = deepcopy(count)
    plus_count = 0
    if 0 <= i < h and 0 <= j < w and string_dict[(i, j)][0] == '.':
        string_dict[(i, j)] = ('#', regist_count)
        plus_count = 1
    return string_dict, plus_count


all_value = h * w
count = 1
break_count = 0
while 1:
    for k, v in string_dict.items():
        i = k[0]
        j = k[1]
        tmp_count = int(v[1])
        if v[0] == '#' and tmp_count + 1 == count:
            if count == 1:
                break_count += 1
            string_dict, plus_count1 = check_set_black(i+1, j, h, w, count, string_dict)
            string_dict, plus_count2 = check_set_black(i-1, j, h, w, count, string_dict)
            string_dict, plus_count3 = check_set_black(i, j + 1, h, w, count, string_dict)
            string_dict, plus_count4 = check_set_black(i, j - 1, h, w, count, string_dict)
            break_count += plus_count1 + plus_count2 + plus_count3 + plus_count4

    if break_count >= all_value:
        break

    if count > 100000000:
        break
    count += 1

print(count)

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

import copy

f = [[0 for _ in range(W) ] for _ in range(H) ]

temp = copy.deepcopy(M)

while True:
    count += 1

    for h in range(H):
        if M[h].count(1) == W:
            continue

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

                f[h][w] = 1

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

                    elif i == 1 and h != H - 1:
                        M[h + 1][w] = '#'

                    elif i == 2 and w != 0:
                        M[h][w - 1] = '#'

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

        temp[h] = M[h][:]

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

    flg = False

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

        if flg:
            break

    if flg == False:
        print(count)
        exit(0)

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
flag = False

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

print(result)

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

import copy

f = [[0 for _ in range(W) ] for _ in range(H) ]

temp = copy.deepcopy(M)

while True:
    count += 1

    for h in range(H):
        if f[h].count(1) == W:
            continue

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

                f[h][w] = 1

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

                    elif i == 1 and h != H - 1:
                        M[h + 1][w] = '#'

                    elif i == 2 and w != 0:
                        M[h][w - 1] = '#'

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

        temp[h] = M[h][:]

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

    flg = False

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

        if flg:
            break

    if flg == False:
        print(count)
        exit(0)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

# [競プロ等におけるpython3の標準入力 \- Qiita](https://qiita.com/zenrshon/items/c4f3849552348b3dbe67)
# [Pythonのリスト（配列）の特定の要素を抽出、置換、変換 \| note\.nkmk\.me](https://note.nkmk.me/python-list-select-replace/)

H, W = [int(x) for x in stdin.readline().rstrip().split()]
A = np.array([[1 if s == '#' else 0 for s in l.strip()] for l in stdin])

# [NumPy配列ndarrayの対角成分の抽出、対角行列の作成（diag, diagonal） \| note\.nkmk\.me](https://note.nkmk.me/python-numpy-diag-diagonal/)

max = 0
tmp = 0

# 右上から斜め方向に走査する
for i in range(W+H-2):
    # [NumPy配列ndarrayから条件を満たす要素・行・列を抽出、削除 \| note\.nkmk\.me](https://note.nkmk.me/python-numpy-condition/)
    if np.any(A.diagonal(W - 1 - i) == 1) and (len(A.diagonal(W - 1 - i)) > 1) :
        if max < tmp :
            max = tmp
            tmp = 0
    else : 
        tmp = tmp + 1

if max < tmp :
    max = tmp

print(max)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy
import sys
b, c = map(int, input().split())
K = np.zeros((b,c))
for i in range(b):
    s = input()
    for j in range(c):
        if s[j] == "#" :
            K[i,j] =1
#print(K.shape)
K = K.reshape(b,c)
#print(K.shape)
sys.exit()
count = 0
if np.all(K>0):
    print(0)
else:
    while(1):
        count =count+1
        #print(K)
        S = copy.deepcopy(K)
        #print(S)
        k = np.zeros(b).reshape(1,-1)
        #S1 = np.vstack((S,k))
        S1 = np.vstack((S,np.zeros(b).reshape(1,-1)))
        S1 = S1[1:,:]
        #print(S1)
        S2 = np.vstack((np.zeros(b).reshape(1,-1),S))
        S2 = S2[0:-1,:]
        #print(S2)
        S3 = np.hstack((S,np.zeros(c).reshape(-1,1)))
        S3 = S3[:,1:]
        #print(S3)
        S4 = np.hstack((np.zeros(c).reshape(-1,1),S))
        S4 = S4[:,0:-1]
        #print(S4)
        K = S+S1+S2+S3+S4
        #print(K)
        if np.all(K>0):
            break
        

    print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect

H, W = map(int, input().split())
A = [input() for _ in range(H)]
blacks = deque([(-1,-1),(H, W)])
for h in range(H):
  for w in range(W):
    if A[h][w] == "#":
      blacks.append((h, w))
blacks_h = sorted(blacks, key=lambda x:x[0])
blacks_w = sorted(blacks, key=lambda x:x[0])
ans = 0
for h in range(H):
  for w in range(W):
    if A[h][w] == ".":
      h_index = bisect.bisect_left(blacks_h, (h, w))
      h_right = blacks_h[h_index]
      h_left = blacks_h[h_index-1]
      w_index = bisect.bisect_left(blacks_w, (h, w))
      w_right = blacks_w[w_index]
      w_left = blacks_w[w_index-1]
      ans = max(min(
      abs(h_right[0]-h)+abs(h_right[1]-w),
      abs(h_left[0]-h)+abs(h_left[1]-w),
      abs(w_right[0]-h)+abs(w_right[1]-w),
      abs(w_left[0]-h)+abs(w_left[1]-w)), ans)
print(ans)

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



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 = [[i,j] for i in range(H) for j in range(W) if a[i][j]=='#']
lis_hidariue = [x[0]+x[1] for x in black]
lis_migiue = [ W + x[0] - x[1] - 1 for x in black]
lis_hidarishita = [H - x[0] + x[1]-1 for x in black]
lis_migishita = [H + W - x[0] - x[1]-2 for x in black]
"""
print(lis_hidariue)
print(lis_migiue)
print(lis_hidarishita)
print(lis_migishita)
"""
ct_black = len(black)

#壁までの距離
dis_kabe = max(min(lis_hidarishita),min(lis_hidariue),min(lis_migishita),min(lis_migiue))

#もっとも近い黒点間の距離
dis_point = []
#print(black)

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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
h,w=1000,1000#h,w=map(int,input().split())
a,s=[],[["x"]*(w+2)]
for i in range(h):
  t,tt="."*w,[]#t,tt=input(),[]
  for j in range(w):
    if t[j]=="#":a.append([i+1,j+1])
    tt.append(t[j])
  s.append(["x"]+tt+["x"])
s.append(["x"]*(w+2))
a=[[1,1]]
f=1
for i in range(2000):
  b=[]
  for k in a:
    i,j=k[0],k[1]
    if s[i-1][j]==".":s[i-1][j]=f;b.append([i-1,j])
    if s[i+1][j]==".":s[i+1][j]=f;b.append([i+1,j])
    if s[i][j-1]==".":s[i][j-1]=f;b.append([i,j-1])
    if s[i][j+1]==".":s[i][j+1]=f;b.append([i,j+1])
  a=b[:]
  f+=1
  if len(a)==0:break
print(f-2)

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools
H,W = map(int,input().split(' '))
bk = []
for i in range(H):
	t = input()
	bk.extend([(x,i) for x in range(W) if t[x] == '#'])

ans = np.zeros((H,W)).astype(np.bool)

obk = set()
bk = set(bk)
cnt = 0
while(True):
	red = bk - obk
	obk = bk.copy()
	for x,y in red:
		ans[y,x] = True
		if x > 0:
			bk.add((x-1,y))
		if x < W-1:
			bk.add((x+1,y))
		if y > 0:
			bk.add((x,y-1))
		if y < H-1:
			bk.add((x,y+1))
	cnt += 1
	if len(bk) == H*W:
		break
print(cnt)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
A=[]
isa=0
jsa=0
HH=99999
WW=99999
for i in range(H):
    x=list(input())
    xc=x.count('#')
    A.append(x)
    if(xc<=HH):
        HH=xc
        isa=i

for i in range(W):
    yc=[row[i] for row in A].count('#')
    if(yc<=WW):
        WW=yc
        jsa=i
    ansj=-1
    ansi=0
for ii in range(H):
    indexes=[i for i, z in enumerate(A[ii]) if z=='#']
    indexes.append(-10)
    mx=max(indexes)
    if(ansj<=mx):
        ansj=mx
        ansi=ii
print(abs(ansi-isa)+abs(ansj-jsa))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
bp = []
for i in range(H):
    s = input()
    for j, e in enumerate(s):
        if e == '#':
            bp.append([i, j])
wp = []
for i in range(W):
    wp.append([0, i])
    wp.append([H-1, i])
for i in range(1, H-1):
    wp.append([i, 0])
    wp.append([i, W-1])
ans = 0
for w in wp:
    d = 1000*1000
    for b in bp:
        d = min(d, abs(w[0]-b[0])+abs(w[1]-b[1]))
    ans = max(ans, d)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
H,W = list(map(int,input().split()))
A = []
for i in range(H):
  A.append(list(input()))
B=[(-200,-200)]
n = -1
while len(B) != H * W:
  B = []
  for y, row in enumerate(A):
     for z,x in enumerate(row):
        if x == "#":
          pos = (y,z)
          B.append(pos)
  for i in range(len(B)):
    x_pos,y_pos = B[i]
    try:
      A[x_pos + 1][y_pos] = "#"
      A[x_pos - 1][y_pos] = "#"
      A[x_pos][y_pos + 1] = "#"
      A[x_pos][y_pos - 1] = "#"
    except IndexError:
      pass
  n += 1
print(n)

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

import copy

f = [[0 for _ in range(W) ] for _ in range(H) ]

temp = copy.deepcopy(M)

while True:
    count += 1

    #temp = copy.deepcopy(M)

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

                f[h][w] = 1

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

                    elif i == 1 and h != H - 1:
                        M[h + 1][w] = '#'

                    elif i == 2 and w != 0:
                        M[h][w - 1] = '#'

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

        temp[h] = M[h][:]

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

    flg = False

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

        if flg:
            break

    if flg == False:
        print(count)
        exit(0)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy
import sys
b, c = map(int, input().split())
K = np.zeros((b,c))
for i in range(b):
    s = input()
    for j in range(c):
        if s[j] == "#" :
            K[i,j] =1
print(K.shape)
K = K.reshape(b,c)
print(K.shape)
sys.exit()
count = 0
if np.all(K>0):
    print(0)
else:
    while(1):
        count =count+1
        #print(K)
        S = copy.deepcopy(K)
        #print(S)
        k = np.zeros(b).reshape(1,-1)
        #S1 = np.vstack((S,k))
        S1 = np.vstack((S,np.zeros(b).reshape(1,-1)))
        S1 = S1[1:,:]
        #print(S1)
        S2 = np.vstack((np.zeros(b).reshape(1,-1),S))
        S2 = S2[0:-1,:]
        #print(S2)
        S3 = np.hstack((S,np.zeros(c).reshape(-1,1)))
        S3 = S3[:,1:]
        #print(S3)
        S4 = np.hstack((np.zeros(c).reshape(-1,1),S))
        S4 = S4[:,0:-1]
        #print(S4)
        K = S+S1+S2+S3+S4
        #print(K)
        if np.all(K>0):
            break
        

    print(count)

Submission status: Wrong Answer

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


black=[]

for i in range(h):
    for j  in range(w):
        if li[i][j]=="#":
            black.append([i,j])
            
        
def check(li,i,j,black,count):
    v=[]
    for k in black:
        if abs(k[0]-i)+abs(k[1]-j)<=count:
            return(abs(k[0]-i)+abs(k[1]-j)-count)
        else:
            v.append(abs(k[0]-i)+abs(k[1]-j))
            
 #   print(min(v))
    return min(v)
    
        
        
    
dist=0
count=0
point=0
for i in range(h):
    for j  in range(w):
        if li[i][j]==".":
            
            if dist+abs(point-j)>0:
                dist=check(li,i,j,black,count)
                point=j
                
            count=max(count,dist)
            

            
            
print(count)

Submission status: Wrong Answer

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

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

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

ans = 1    
#print(check1)
print(ans - 1)

Submission status: Wrong Answer

Solution code:
import numpy as np
H,W =map(int ,raw_input().split())
l =[]
for i in range(H):
    l.append(map(str ,raw_input().split()))
for i in range(H):
    l[i] =list(l[i][0])
    
def distance(i,j,l):
    if l[i][j] == '#':
        return 1
    elif l[i][j] == '.':
        return 1000
    else:
        return l[i][j]+1
flag =1
Max = 0
while flag == 1:
    flag = 0
    for i in range(H):
        for j in range(W):
            if l[i][j] == '.':
                s = 1000
                if i>0:
                    k = distance(i-1,j,l)
                    if k < s:
                        s =k
                if i<H-1:
                    k = distance(i+1,j,l)
                    if k < s:
                        s =k
                if j>0:
                    k = distance(i,j-1,l)
                    if k < s:
                        s =k
                if j<W-1:
                    k = distance(i,j+1,l)
                    if k < s:
                        s =k
                if s == 1000:
                    flag =1
                else:
                    l[i][j] = s
                    if Max <= s:
                        Max = s
print str(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

import copy

f = [[0 for _ in range(W) ] for _ in range(H) ]

temp = copy.deepcopy(M)

while True:
    count += 1

    #temp = copy.deepcopy(M)

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

                f[h][w] = 1

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

                    elif i == 1 and h != H - 1:
                        M[h + 1][w] = '#'

                    elif i == 2 and w != 0:
                        M[h][w - 1] = '#'

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

        temp[h] = M[h][:]

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

    flg = False

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

        if flg:
            break

    if flg == False:
        break


print(count)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A_list = []
cnt = 0
stack = []
comp = [["#"]*W]*H
check = []

for i in range(H):
    A = list(str(input()))
    A_list.append(A)
while True:
    cnt += 1
    for i in range(H):
        jud = 0
        for j in range(W):
            if A_list[i][j] == "#":
                if [i,j] in check:
                    continue
                else:
                    if i > 0:
                        if A_list[i-1][j] == ".":
                                A_list[i-1][j] = "#"
                                check.append([i-1,j])
                    if j > 0:
                        if A_list[i][j-1] == ".":
                                A_list[i][j-1] = "#"
                                check.append([i,j-1])
                    if i < (H - 1):
                        if A_list[i+1][j] == ".":
                                A_list[i+1][j] = "#"
                                check.append([i+1,j])
                    if j < (W-1):
                        if A_list[i][j+1] == ".":
                                A_list[i][j+1] = "#"
                                check.append([i,j+1]) 
    check = []                                                        
    if A_list == comp:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy
import sys
b, c = map(int, input().split())
K = np.zeros((b,c))
for i in range(b):
    s = input()
    for j in range(c):
        if s[j] == "#" :
            K[i,j] =1
print(K.shape)
K = K.reshape(b,c)
print(K.shape)
sys.exit()
count = 0
if np.all(K>0):
    print(0)
else:
    while(1):
        count =count+1
        print(K)
        S = copy.deepcopy(K)
        print(S)
        k = np.zeros(b).reshape(1,-1)
        #S1 = np.vstack((S,k))
        S1 = np.vstack((S,np.zeros(b).reshape(1,-1)))
        S1 = S1[1:,:]
        print(S1)
        S2 = np.vstack((np.zeros(b).reshape(1,-1),S))
        S2 = S2[0:-1,:]
        print(S2)
        S3 = np.hstack((S,np.zeros(c).reshape(-1,1)))
        S3 = S3[:,1:]
        print(S3)
        S4 = np.hstack((np.zeros(c).reshape(-1,1),S))
        S4 = S4[:,0:-1]
        print(S4)
        K = S+S1+S2+S3+S4
        print(K)
        sys.exit()
        if np.all(K>0):
            break
        

    print(count)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
A=[]
isa=0
jsa=0
HH=99999
WW=99999
for i in range(H):
    x=list(input())
    xc=x.count('#')
    A.append(x)
    if(xc<=HH):
        HH=xc
        isa=i
 
for i in range(W):
    yc=[row[i] for row in A].count('#')
    if(yc<=WW):
        WW=yc
        jsa=i
    ansj=-1
    ansi=0
for ii in range(H):
    indexes=[i for i, z in enumerate(A[ii]) if z=='#']
    indexes.append(0)
    mx=max(indexes)
    if(ansj<=mx):
        ansj=mx
        ansi=ii
print(abs(ansi-isa)+abs(ansj-jsa))

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 = True

import copy

f = [[0 for _ in range(W) ] for _ in range(H) ]

temp = copy.deepcopy(M)

while flg:
    count += 1

    #temp = copy.deepcopy(M)

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

                f[h][w] = 1

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

                    elif i == 1 and h != H - 1:
                        M[h + 1][w] = '#'

                    elif i == 2 and w != 0:
                        M[h][w - 1] = '#'

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

        temp[h] = M[h][:]

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

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

        if flg == True:
            #flg = False
            break


print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import numpy as np

# [競プロ等におけるpython3の標準入力 \- Qiita](https://qiita.com/zenrshon/items/c4f3849552348b3dbe67)
# [Pythonのリスト（配列）の特定の要素を抽出、置換、変換 \| note\.nkmk\.me](https://note.nkmk.me/python-list-select-replace/)

H, W = [int(x) for x in stdin.readline().rstrip().split()]
A = np.array([[1 if s == '#' else 0 for s in l.strip()] for l in stdin])

# [NumPy配列ndarrayの対角成分の抽出、対角行列の作成（diag, diagonal） \| note\.nkmk\.me](https://note.nkmk.me/python-numpy-diag-diagonal/)

max = 0
tmp = 0

# 右上から斜め方向に走査する
for i in range(W+H-2):
    # [NumPy配列ndarrayから条件を満たす要素・行・列を抽出、削除 \| note\.nkmk\.me](https://note.nkmk.me/python-numpy-condition/)
    if np.any(A.diagonal(W - 1 - i) == 1) and (len(A.diagonal(W - 1 - i)) > 1) :
        if max < tmp :
            max = tmp
            tmp = 0
    else : 
        tmp = tmp + 1

print(max)

Submission status: Wrong Answer

Solution code:
x = input().split()
H = int(x[0])
W = int(x[1])
time = 0

Field = []
for i in range(0,H):
    al = list(input())
    Field.append(al)
print(Field)

def change():
    num_list = []
    for h in range(0,H):
        for w in range(0,W):
            if Field[h][w] == '#':
                pass
            elif 0<= h-1 <= H-1 and Field[h-1][w] == '#':
                num_list.append([h,w])
            elif 0<= h+1 <= H-1 and Field[h+1][w] == '#':
                num_list.append([h,w])
            elif 0<= w-1 <= W-1 and Field[h][w-1] == '#':
                num_list.append([h,w])
            elif 0<= w+1 <= W-1 and Field[h][w+1] == '#':
                num_list.append([h,w])
            else:
                pass
    len(num_list)
    for k in range(0,len(num_list)):
        h = num_list[k][0]
        w = num_list[k][1]
        Field[h][w] = '#'
    print(Field)

for h in range(0,H):
    for w in range(0,W):
        if Field[h][w] == '.':
            change()
            time = time + 1
        else:
            pass
print(time)

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 = True

import copy

f = [[0 for _ in range(W) ] for _ in range(H) ]

temp = copy.deepcopy(M)

while flg:
    count += 1

    #temp = copy.deepcopy(M)

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

                f[h][w] = 1

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

                    elif i == 1 and h != H - 1:
                        M[h + 1][w] = '#'

                    elif i == 2 and w != 0:
                        M[h][w - 1] = '#'

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

        temp[h] = M[h][:]

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

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

        if flg == True:
            #flg = False
            break


print(count)