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 = [ int(v) for v in input().split() ]
field = [ list(input()) for i in range(h) ]

for i in range(h):
    if field[i][0] == "#":
            field[i][0] = 0
    for j in range(1,w):
        if field[i][j] == "#":
            field[i][j] = 0
        elif field[i][j-1] != ".":
            field[i][j] = field[i][j-1] + 1
    
    for j in range(w-2,-1,-1):
        if field[i][j] != ".":
            if field[i][j+1] != ".":
                field[i][j] = min(field[i][j+1] + 1, field[i][j])
        else:
            if field[i][j+1] != ".":
                field[i][j] = field[i][j+1] + 1

for i in range(w):
    for j in range(h):
        if field[j][i] != ".":
            if field[j-1][i] != ".":
                field[j][i] = min(field[j-1][i] + 1, field[j][i])
        else:
            if field[j-1][i] != ".":
                field[j][i] = field[j-1][i] + 1

    for j in range(h-2,-1,-1):
        if field[j][i] != ".":
            if field[j+1][i] != ".":
                field[j][i] = min(field[j+1][i] + 1, field[j][i])
        else:
            if field[j+1][i] != ".":
                field[j][i] = field[j+1][i] + 1


print(max([max(i) for i in field]))

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy

H, W = map(int, input().split())
blacks = set()
maps = []
for h in range(H):
  line = list(input())
  for w in range(W):
    if line[w] == '#':
      blacks.add((h,w))
  maps.append(line)
copied_maps = copy.deepcopy(maps)

def fill(x,y):
  new_blacks = set()
  for dx, dy in [[0,1],[0,-1],[1,0],[-1,0]]:
    nx = x+dx
    ny = y+dy
    if H > nx >= 0 and W > ny >= 0 and copied_maps[nx][ny] != '#':
      copied_maps[nx][ny] = '#'
      new_blacks.add((nx,ny))    
  return new_blacks

end = False
for i in range(H*W):
  new_blacks = set()
  for b in blacks:
    new_blacks = new_blacks | fill(b[0], b[1])
    if len(blacks | new_blacks) >= H*W:
      print(i+1)
      exit()
  blacks = blacks | new_blacks

Submission status: Wrong Answer

Solution code:
import copy


def bfs(sx, sy):
    for dx, dy in nb:
        nx = sx + dx
        ny = sy + dy
        if 0 <= nx and nx < h and 0 <= ny and ny < w and origin[nx][ny] == ".":
            grid[nx][ny] = "#"


h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
black_row = ["#"] * w
nb = [(-1, 0), (0, -1), (1, 0), (0, 1)]
ans = 0

while True:
    origin = copy.deepcopy(grid)
    flag = True
    for i in range(h):
        for j in range(w):
            if origin[i][j] == "#":
                bfs(i, j)
    ans += 1
    for row in grid:
        if row != black_row:
            flag = False
    if flag:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def get_ij_black(W, rows):
  ans = None
  for i, row in enumerate(rows):
    for j, a in enumerate(row):
      if a == "#":
        cnt = max([i,j,W-1-i,H-1-j]) + 1
        print(cnt)
        if ans is None or ans > cnt:
          ans = cnt
  return ans


def get_closest_cross_dist(i,j,H,W,distlist):
  return 

H, W = map(int, input().split())
rows = [input() for i in range(H)]
ans = get_ij_black(W, rows)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

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

a = set()
i = 0
for h in range(H):
    s = list(input().strip())
    for c in s:
        if c == "#":
            a.add(i)
        i += 1
#print(a)

"""
m = 0
for i in range(H*W):
    ni = H+W-2
    for j in a:
        ni = min(ni, abs(j//W-i//W) + abs(j%W -i%W))
#        print(ni, abs(j-i)//W, abs(j%W -i%W))
    m = max(m, ni)
print(m)
"""

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

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

a = []
i = 0
for h in range(H):
    s = list(input().strip())
    for c in s:
        if c == "#":
            a.append(i)
        i += 1
#print(a)

"""
m = 0
for i in range(H*W):
    ni = H+W-2
    for j in a:
        ni = min(ni, abs(j//W-i//W) + abs(j%W -i%W))
#        print(ni, abs(j-i)//W, abs(j%W -i%W))
    m = max(m, ni)
print(m)
"""

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
def main():
    H,W = list(map(int,sys.stdin.readline().split()))
    place_h = [0,-1,0,1]
    place_w = [1,0,-1,0]
    black = 0
    q = []
    dist = []
    A = []
    for i in range(H):
        a = sys.stdin.readline().split()
        a = list(a[0])
        dist_sub = []
        for j in range(W):
            if a[j] == "#":
                black += 1
                q.append(deque([[i,j]]))
                dist_sub.append(0)
            else:
                dist_sub.append(-1)
        A.append(a)
        dist.append(dist_sub)

    ans = 0
    count=0
    while count < black:
        for i in range(black):
            if len(q[i]) == 0:
                count += 1
                q[i].append(-1)
                continue
            if q[i][0] == -1:
                continue
            vh,vw = q[i].popleft()
            for m in range(4):
                nvh = vh+place_h[m]
                nvw = vw+place_w[m]
                if 0<=nvh<H and 0<=nvw<W and dist[nvh][nvw] == -1:
                    q[i].append([nvh,nvw])
                    dist[nvh][nvw] = dist[vh][vw]+1
                    if ans < dist[nvh][nvw]:
                        ans = dist[nvh][nvw]

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
def main():
    H,W = list(map(int,sys.stdin.readline().split()))
    A = [sys.stdin.readline().split() for i in range(H)]
    place_h = [0,-1,0,1]
    place_w = [1,0,-1,0]
    black = 0
    q = []
    dist = []
    for i in range(H):
        A[i] = list(A[i][0])
        dist_sub = []
        for j in range(W):
            if A[i][j] == "#":
                black += 1
                q.append(deque([[i,j]]))
                dist_sub.append(0)
            else:
                dist_sub.append(-1)
        dist.append(dist_sub)

    ans = 0
    count=0
    while count < black:
        for i in range(black):
            if len(q[i]) == 0:
                count += 1
                q[i].append(-1)
                continue
            if q[i][0] == -1:
                continue
            vh,vw = q[i].popleft()
            for m in range(4):
                nvh = vh+place_h[m]
                nvw = vw+place_w[m]
                if 0<=nvh<H and 0<=nvw<W and dist[nvh][nvw] == -1:
                    q[i].append([nvh,nvw])
                    dist[nvh][nvw] = dist[vh][vw]+1
                    if ans < dist[nvh][nvw]:
                        ans = dist[nvh][nvw]

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = list(map(int,input().split()))
A = [input().split() for i in range(H)]
place_h = [0,-1,0,1]
place_w = [1,0,-1,0]
black = 0
q = []
dist = []
for i in range(H):
    A[i] = list(A[i][0])
    dist_sub = []
    for j in range(W):
        if A[i][j] == "#":
            black += 1
            q.append(deque([[i,j]]))
            dist_sub.append(0)
        else:
            dist_sub.append(-1)
    dist.append(dist_sub)

ans = 0
count=0
while count < black:
    for i in range(black):
        if len(q[i]) == 0:
            count += 1
            q[i].append(-1)
            continue
        if q[i][0] == -1:
            continue
        vh,vw = q[i].popleft()
        for m in range(4):
            nvh = vh+place_h[m]
            nvw = vw+place_w[m]
            if 0<=nvh<H and 0<=nvw<W and dist[nvh][nvw] == -1:
                q[i].append([nvh,nvw])
                dist[nvh][nvw] = dist[vh][vw]+1
                if ans < dist[nvh][nvw]:
                    ans = dist[nvh][nvw]

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = list(map(int,input().split()))
A = [input().split() for i in range(H)]
place_h = [0,-1,0,1]
place_w = [1,0,-1,0]
black = 0
q = []
dist = []
for i in range(H):
    A[i] = list(A[i][0])
    dist_sub = []
    for j in range(W):
        if A[i][j] == "#":
            black += 1
            q.append(deque([[i,j]]))
            dist_sub.append(0)
        else:
            dist_sub.append(-1)
    dist.append(dist_sub)

ans = 0
while len(q)>0:
    i = 0
    while i < black:
        if len(q[i]) == 0:
            q.pop(i)
            black -= 1
            continue
        vh,vw = q[i].popleft()
        for m in range(4):
            nvh = vh+place_h[m]
            nvw = vw+place_w[m]
            if 0<=nvh<H and 0<=nvw<W and dist[nvh][nvw] == -1:
                q[i].append([nvh,nvw])
                dist[nvh][nvw] = dist[vh][vw]+1
                if ans < dist[nvh][nvw]:
                    ans = dist[nvh][nvw]
        i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
H,W = list(map(int,input().split()))
A = [input().split() for i in range(H)]
place_h = [0,-1,0,1]
place_w = [1,0,-1,0]
black = 0
q = []
dist = [[-1 for i in range(W)] for j in range(H)]
for i in range(H):
    A[i] = list(A[i][0])
    for j in range(W):
        if A[i][j] == "#":
            black += 1
            q.append(deque([[i,j]]))
            dist[i][j] = 0
ans = 0
while len(q)>0:
    i = 0
    while i < black:
        if len(q[i]) == 0:
            q.pop(i)
            black -= 1
            continue
        vh,vw = q[i].popleft()
        for m in range(4):
            nvh = vh+place_h[m]
            nvw = vw+place_w[m]
            if 0<=nvh<H and 0<=nvw<W and dist[nvh][nvw] == -1:
                q[i].append([nvh,nvw])
                dist[nvh][nvw] = dist[vh][vw]+1
                if ans < dist[nvh][nvw]:
                    ans = dist[nvh][nvw]
        i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [[] for _ in range(H)]
inf = 1000000007
for i in range(H):
    A[i] = list(input())
    for j in range(W):
        if A[i][j] == '.':
            A[i][j] = inf
        elif A[i][j] == '#':
            A[i][j] = 0

#print(A)

for i in range(H):
    for j in range(1, W):
        A[i][j] = min(A[i][j-1]+1, A[i][j])

#print(A)


for i in range(H):
    for j in range(W-2, -1, -1):
        A[i][j] = min(A[i][j+1]+1, A[i][j])

#print(A)

for i in range(1, H):
    for j in range(0, W):
        A[i][j] = min(A[i-1][j]+1, A[i][j])

#print(A)

for i in range(H-2, -1, -1):
    for j in range(W):
        A[i][j] = min(A[i+1][j]+1, A[i][j])



print(max(max(A)))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [[] for _ in range(H)]
inf = 1000000007
for i in range(H):
    A[i] = list(input())
    for j in range(W):
        if A[i][j] == '.':
            A[i][j] = inf
        elif A[i][j] == '#':
            A[i][j] = 0



for i in range(H):
    for j in range(1, W):
        A[i][j] = min(A[i][j-1]+1, A[i][j])

for i in range(H):
    for j in range(W-2, -1, -1):
        A[i][j] = min(A[i][j+1]+1, A[i][j])

for i in range(1, H):
    for j in range(0, W):
        A[i][j] = min(A[i-1][j]+1, A[i][j])

for i in range(H-2, -1, -1):
    for j in range(1, W):
        A[i][j] = min(A[i+1][j]+1, A[i][j])

print(max(max(A)))

Submission status: Wrong Answer

Solution code:
def check(before_masu, after_masu, count):
  for i in range(h):
    for n in range(w):
      if before_masu[i][n] == '#':
        after_masu[i][n] = '#'
        if i-1 >-1:
          after_masu[i-1][n] = '#'
        if i+1 < h:
          after_masu[i+1][n] = '#'
        if n-1 > -1:
          after_masu[i][n-1] = '#'
        if n+1 < w:
          after_masu[i][n+1] = '#'
  for n, i in enumerate(after_masu):
    if '.' in i:
      check(after_masu, before_masu, count+1)
      break
    if n == h-1:
      print(count+1)
h, w = map(int, input().split())
before_masu = [['.' for i in range(w)] for n in range(h)]
after_masu = [['.' for i in range(w)] for n in range(h)]
for i in range(h):
  line = list(input())
  for n in range(w):
    before_masu[i][n] = line[n]
check(before_masu, after_masu, 0)

Submission status: Wrong Answer

Solution code:
def check(before_masu, after_masu, count):
  for i in range(h):
    for n in range(w):
      if before_masu[i][n] == '#':
        after_masu[i][n] = '#'
        if i-1 >-1:
          after_masu[i-1][n] = '#'
        if i+1 < h:
          after_masu[i+1][n] = '#'
        if n-1 > -1:
          after_masu[i][n-1] = '#'
        if n+1 < w:
          after_masu[i][n+1] = '#'
  for n, i in enumerate(after_masu):
    if '.' in i:
      check(after_masu, before_masu, count+1)
      break
    if n == h-1:
      print(count)
h, w = map(int, input().split())
before_masu = [['.' for i in range(w)] for n in range(h)]
after_masu = [['.' for i in range(w)] for n in range(h)]
for i in range(h):
  line = list(input())
  for n in range(w):
    before_masu[i][n] = line[n]
check(before_masu, after_masu, 0)

Submission status: Wrong Answer

Solution code:
from collections import deque
import copy
H, W = map(int, input().split())
A = tuple(tuple(input()) for _ in range(H))
next_q=deque([(i, j) for i in range(H) for j in range(W) if A[i][j] == '#'])
L = [[0] * W for _ in range(H)]
q=deque()

res = 0
while len(next_q) != 0:
    q=copy.copy(next_q)
    next_q.clear()
    while q:
        y, x = q.popleft()
        if y > 0 and L[y - 1][x] == 0:
            L[y - 1][x] = 1
            next_q.append((y - 1, x))
        if y < H - 1 and L[y + 1][x] == 0:
            L[y + 1][x] = 1
            next_q.append((y + 1, x))
        if x > 0 and L[y][x - 1] == 0:
            L[y][x - 1] = 1
            next_q.append((y, x - 1))
        if x < W - 1 and L[y][x + 1] == 0:
            L[y][x + 1] = 1
            next_q.append((y, x + 1))
    if len(next_q)==0: break
    res += 1
print(res)

Submission status: Wrong Answer

Solution code:
##Test##
import sys
input=sys.stdin.readline
H,W=map(int,input().split())
A=[list(input()) for _ in range(H)]
B=[[0]*W for _ in range(H)]
L=[]
ans=0
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            B[i][j]==1
            L.append([i,j])
while True:
    LL=[]
    for l in L:
        h,w=l[0],l[1]
        if h!=0:
            if B[h-1][w]==0:
                LL.append([h-1,w])
                B[h-1][w]=1
        if h!=H-1:
            if B[h+1][w]==0:
                LL.append([h+1,w])
                B[h+1][w]=1
        if w!=0:
            if B[h][w-1]==0:
                LL.append([h,w-1])
                B[h][w-1]=1
        if w!=W-1:
            if B[h][w+1]==0:
                LL.append([h,w+1])
                B[h][w+1]=1
    if LL==[]:
        break
    else:
        ans+=1
        L=LL
print(ans)

Submission status: Wrong Answer

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

cnt=0
f=0
for k in range(H*W):
    f=0
    cnt+=1
    print("cnt",cnt)
    for i in range(H):
        for j in range(W):
            if A[i][j]=='#':
                print("hit! i ,j ",i,j)
                if j-1>=0 and B[i][j-1]!='#': 
                    B[i][j-1]='#'
                    f=1
                if j+1<=W-1 and B[i][j+1]!='#':
                    B[i][j+1]='#'
                    f=1
                if i-1>=0 and B[i-1][j]!='#':
                    B[i-1][j]='#'
                    f=1
                if i+1<=H-1 and B[i+1][j]!='#':
                    B[i+1][j]='#'
                    f=1
    A=B
    if f==0:
        break

print(cnt)

Submission status: Wrong Answer

Solution code:
import math

count=0
h,w = map(int, input().split())
lst = [] #迷路

for i in range(h): 
    x =list(input())
    lst.append(x) #迷路の読み込み
while 1:
    count+=1
    judge=[]
    for k in range(h):
        for j in range(w):
            if lst[k][j]=="#":
                hNow, wNow = k,j
                if hNow < h-1:#下
                        if lst[hNow+1][wNow] != "#":
                            lst[hNow+1][wNow]="a"
                if wNow < w-1:#右
                    if lst[hNow][wNow+1] != "#":
                        lst[hNow][wNow+1] = "a"
                if hNow > 0:#上
                    if lst[hNow-1][wNow] != "#":
                        lst[hNow-1][wNow] = "a"
                if wNow > 0:#左
                    if lst[hNow][wNow-1] != "#":
                        lst[hNow][wNow-1] = "a"
    lst=[[s.replace('a', '#') for s in lst[t]]  for t in range(h) ]
    for t in range(h):
        if all([e == "#" for e in lst[t][:]]):
            judge.append("T")
        else:
            judge.append("F")
    if all(e == "T" for e in judge):
        break
        
print(count)

Submission status: Wrong Answer

Solution code:
import math

count=0
h,w = map(int, input().split())
lst = [] #迷路

for i in range(h): 
    x =list(input())
    lst.append(x) #迷路の読み込み
while 1:
    count+=1
    judge=[]
    for k in range(h):
        for j in range(w):
            if lst[k][j]=="#":
                hNow, wNow = k,j
                if hNow < h-1:#下
                        if lst[hNow+1][wNow] != "#":
                            lst[hNow+1][wNow]="a"
                if wNow < w-1:#右
                    if lst[hNow][wNow+1] != "#":
                        lst[hNow][wNow+1] = "a"
                if hNow > 0:#上
                    if lst[hNow-1][wNow] != "#":
                        lst[hNow-1][wNow] = "a"
                if wNow > 0:#左
                    if lst[hNow][wNow-1] != "#":
                        lst[hNow][wNow-1] = "a"
    lst=[[s.replace('a', '#') for s in lst[t]]  for t in range(h) ]
    for t in range(h):
        if all([e == "#" for e in lst[t][:]]):
            judge.append("T")
        else:
            judge.append("F")
    print(judge)
    print(count)
    if all(e == "T" for e in judge):
        break
        
print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
a = [list(input()) for _ in range(H)]
q = deque()
inf_ = float("inf")
grid = [[inf_] * W for _ in range(H)]
dire = [(1,0),(0,1),(-1,0),(0,-1)]
ans = 0

for i in range(H):
    for j in range(W):
        if a[i][j] == "#":
            q.append([i,j,0])
            grid[i][j] = 0

while q:
    i, j, now = q.popleft()
    grid[i][j] = now
    ans = max(ans,now)
    for dy, dx in dire:
        ny,nx = i+dy, j+dx
        if ny>=0 and ny<H and nx>=0 and nx<W and grid[ny][nx] > now+1:
            q.append([ny,nx,now+1])

print(ans)

Submission status: Wrong Answer

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

import copy

ans=0
while 1:
    print(ans, A)


    if all('.' not in A[i] for i in range(H)):
        break
    else:
        ans+=1

    B=copy.deepcopy(A)
    for i in range(H):
        for j in range(W):
            if A[i][j]=='#':
                if i!=0   and A[i-1][j]=='.': B[i-1][j]='#'
                if i!=H-1 and A[i+1][j]=='.': B[i+1][j]='#'
                if j!=0   and A[i][j-1]=='.': B[i][j-1]='#'
                if j!=W-1 and A[i][j+1]=='.': B[i][j+1]='#'
                B[i][j]='X'

    A=copy.deepcopy(B)

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [['.' for w in range(W)] for h in range(H)]
black_points = []
for h in range(H):
    A[h] = list(input())
    for w in range(W):
        if A[h][w] == '#':
            black_points.append([h, w])

ans = -1
for h in range(H):
    for w in range(W):
        if A[h][w] == '.':
            nd = 10**13
            for x, y in black_points:
                nd = min(nd, abs(x-h)+abs(y-w))
            ans = max(ans, nd)

print(ans)

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split()) # 横に2個
    checked = [[False if a == '.' else True for a in list(input()+'#')] for i in range(H)]
    checked.extend([[True]*(W+1)])
    qs = [(h, w) for h in range(H) for w in range(W) if checked[h][w]]

    c = 0
    for _ in range(2000):
        qqs = []
        for h,w in qs:
            checked[h][w] = True
            if not checked[h-1][w]:
                qqs.append((h-1, w))
            if not checked[h+1][w]:
                qqs.append((h+1, w))
            if not checked[h][w-1]:
                qqs.append((h, w-1))
            if not checked[h][w+1]:
                qqs.append((h, w+1))
        if not qqs:
            break
        c += 1
        qs = qqs

    print(c)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split()) # 横に2個
    checked = [[False if a == '.' else True for a in list(input()+'#')] for i in range(H)]
    checked.extend([[True]*(W+1)])
    qs = [[h, w] for h in range(H) for w in range(W) if checked[h][w]]

    c = 0
    for k in range(2000):
        qqs = []
        for h,w in qs:
            checked[h][w] = True
            if not checked[h-1][w]:
                qqs.append([h-1, w])
            if not checked[h+1][w]:
                qqs.append([h+1, w])
            if not checked[h][w-1]:
                qqs.append([h, w-1])
            if not checked[h][w+1]:
                qqs.append([h, w+1])
        if not qqs:
            break
        c += 1
        qs = qqs

    print(c)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:

def main():
    H, W = map(int, input().split()) # 横に2個
    state = [[0 if a == '.' else 1 for a in list(input())] for _ in range(H)]

    checked = [[False for _ in range(W+1)] for _ in range(H+1)]
    qs = []
    for h in range(H):
        for w in range(W):
            if state[h][w] == 1:
                qs.append([h, w])
                checked[h][w] = True
    for h in range(H+1):
        checked[h][W] = True
    for w in range(W+1):
        checked[H][w] = True

    c = 0
    while qs:
        qqs = []
        for h,w in qs:
            checked[h][w] = True
            if 0 < h and not checked[h-1][w]:
                qqs.append([h-1, w])
            if h < H-1 and not checked[h+1][w]:
                qqs.append([h+1, w])
            if 0 < w and not checked[h][w-1]:
                qqs.append([h, w-1])
            if w < W-1 and not checked[h][w+1]:
                qqs.append([h, w+1])
        if not qqs:
            break
        c += 1
        qs = qqs

    print(c)

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split()) # 横に2個
state = [[0 if a == '.' else 1 for a in list(input())] for _ in range(H)]

checked = [[False for _ in range(W+1)] for _ in range(H+1)]
qs = []
for h in range(H):
    for w in range(W):
        if state[h][w] == 1:
            qs.append([h, w])
            checked[h][w] = True
for h in range(H+1):
    checked[h][W] = True
for w in range(W+1):
    checked[H][w] = True

c = -1
while qs:
    qqs = []
    for h,w in qs:
        checked[h][w] = True
        if 0 < h and not checked[h-1][w]:
            qqs.append([h-1, w])
        if h < H-1 and not checked[h+1][w]:
            qqs.append([h+1, w])
        if 0 < w and not checked[h][w-1]:
            qqs.append([h, w-1])
        if w < W-1 and not checked[h][w+1]:
            qqs.append([h, w+1])
    c += 1
    qs = qqs

print(c)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h, w = map(int, input()[:-1].split())
board = []
cnt = 0
for _ in range(h):
  s = input()
  row = []
  for c in s:
    if c == '.':
      row.append(0)
    else:
      row.append(1)
      cnt += 1
  board.append(row)

iter = 0
while cnt < h*w:
  iter += 1
  for i in range(h):
    for j in range(w):
      if board[i][j] != iter:
        continue
      
      if i != 0:
        if board[i-1][j] == 0:
          cnt += 1
          board[i-1][j] = iter + 1
      
      if i != h-1:
        if board[i+1][j] == 0:
          cnt += 1
          board[i+1][j] = iter + 1
      
      if j != 0:
        if board[i][j-1] == 0:
          cnt += 1
          board[i][j-1] = iter + 1
      
      if j != w-1:
        if board[i][j+1] == 0:
          cnt += 1
          board[i][j+1] = iter + 1
            
print(iter)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h, w = map(int, input().split())
board = []
cnt = 0
for _ in range(h):
  s = input()
  row = []
  for c in s:
    if c == '.':
      row.append(0)
    else:
      row.append(1)
      cnt += 1
  board.append(row)

iter = 0
while cnt < h*w:
  iter += 1
  for i in range(h):
    for j in range(w):
      if board[i][j] != iter:
        continue
      
      if i != 0:
        if board[i-1][j] == 0:
          cnt += 1
          board[i-1][j] = iter + 1
      
      if i != h-1:
        if board[i+1][j] == 0:
          cnt += 1
          board[i+1][j] = iter + 1
      
      if j != 0:
        if board[i][j-1] == 0:
          cnt += 1
          board[i][j-1] = iter + 1
      
      if j != w-1:
        if board[i][j+1] == 0:
          cnt += 1
          board[i][j+1] = iter + 1
            
print(iter)

Submission status: Wrong Answer

Solution code:
import numpy as np

h, w = map(int, input().split())
max_value = h + w
values = []

for i in range(h):
    values.append([0 if i == '#' else max_value for i in list(input())])
values = np.array(values)
print(values)
for x in range(w - 1):
    values[:, x+1] = np.minimum(values[:, x] + 1, values[:, x+1])
print(values)
for x in range(w - 1, 0 , -1):
    values[:, x-1] = np.minimum(values[:, x-1], values[:, x] + 1)
print(values)
for x in range(h - 1):
    values[x+1, :] = np.minimum(values[x, :] + 1,values[x+1, :])
print(values)
for x in range(h - 1, 0, -1):
    values[x-1, :] = np.minimum(values[x, :] + 1, values[x-1, :])
print(values)

print(np.max(values))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

def main():
    h,w = map(int,input().split())
    q = deque()
    A = ["" for i in range(h)]
    for i in range(h):
        b = input()
        A[i] = b
    #    print(b)
        for j in range(w):
            if b[j]=="#": q.append([i,j,0])
    #print(A)
    while q:
        a,b,c = q.pop()
        for x,y in [(1,0),(0,1),(-1,0),(0,-1)]:
            if 0<=b+y<h and 0<=a+x<w and A[b+y][a+x] == ".":
                #print(b+y,a+x)
                q.append([b+y,a+x,c+1])
                l = list(A[b+y])
                l[a+x] = "#"
                A[b+y] = "".join(l)
        if not q:
            print(c)
            break
    #print(A)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split()) # 横に2個

blacks = []
for i in range(H):
    for j, l in enumerate(list(input())):
        if l == '#':
            blacks.append([i, j])

c = -1
news = blacks[:]
while len(blacks) < H*W:
    if c >= 0:
        for new in news:
            blacks.append(new)
    next = []
    for new in news:
        i = new[0]
        j = new[1]
        nears = [[i, j+1], [i, j-1], [i+1, j], [i-1, j]]
        for near in nears:
            ni = near[0]
            nj = near[1]
            if 0 <= ni and ni <= H-1 and 0 <= nj and nj <= W-1 and (not near in blacks) and (not near in next):
                next.append(near)

    news = next
    c += 1

print(c)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
d=deque()
dc=0
b=[]
for i in range(h):
  s=input()
  for j in range(w):
    if s[j]=="#":d.append([i,j]);dc+=1
  b.append(list(s))
count=0
while dc!=0:
  dc=0
  for i in range(dc):
    x=d.popleft()
    if x[0]<h-1:
      if b[x[0]+1][x[1]]==".":b[x[0]+1][x[1]]=count;d.append([x[0]+1,x[1]]);dc+=1
    if 0<x[0]:
      if b[x[0]-1][x[1]]==".":b[x[0]-1][x[1]]=count;d.append([x[0]-1,x[1]]);dc+=1
    if x[1]<w-1:
      if b[x[0]][x[1]+1]==".":b[x[0]][x[1]+1]=count;d.append([x[0],x[1]+1]);dc+=1
    if 0<x[1]:
      if b[x[0]][x[1]-1]==".":b[x[0]][x[1]-1]=count;d.append([x[0],x[1]-1]);dc+=1
  count+=1
print(count-1)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, input().split())
s = [input() for i in range(h)]

d = [[h+w for j in range(w)] for i in range(h)]
que = deque()

#黒だったらスタートキューに入れる
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
        que.append((i, j))
        d[i][j] = 0

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


while que:
    p = que.popleft()
    # 黒にする4方向をループ
    for i in range(4):
        nx = p[1] + dx[i]
        ny = p[0] + dy[i]

        if (0<=nx<w) and (0<=ny<h) and (d[ny][nx]==h+w):
            que.append((ny, nx))
            d[ny][nx] = d[p[0]][p[1]] + 1

res = max(map(max, d))
print(d)
print(res)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(h):
    a=input()
    s.append(a)
x=[[] for i in range(h)]
stack=[[],[]]
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            x[i].append(0)
            stack[0].append([i,j])
        else:
            x[i].append(1)
count=0
print(x)
sw=0
while stack[sw]:
    [ix,iy]=stack[sw].pop(0)
    for p,m in zip([1,0,0,-1],[0,-1,1,0]):
        if -1<ix+p<h and -1<iy+m<w:
            if x[ix+p][iy+m]==1:
                stack[1-sw].append([ix+p,iy+m])
                x[ix+p][iy+m]=0
    if stack[sw]==[] and stack[1-sw]!=[]:
        sw=1-sw
        count+=1
#print(x)
print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
C = []
Q = deque()
dist = [[-1]*W for i in range(H)]

for i in range(H):
  s = list(input())
  yxidx = [(i,x) for x,v in enumerate(s) if v == "#"]
  for (y, x) in yxidx:
    Q.append((y,x))
    dist[y][x] = 0
  C.append(s)
X = [1,0,-1,0]
Y = [0,-1,0,1]


while Q:
  y,x = Q.popleft()
  for i in range(4):
    nx = x + X[i]
    ny = y + Y[i]
    if (0 <= nx < W) and (0 <= ny < H) and (dist[ny][nx] == -1) and (C[ny][nx] != "#"):
      dist[ny][nx] = dist[y][x] + 1
      Q.append((ny,nx))
      print(ny,nx)

ans = 0
for i in range(H):
  ans = max(ans,max(dist[i]))
print(ans)