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:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import deque

INF = -1
H, W = map(int,input().split())
Gmap = [list(input()) for _ in range(H)]
Dist = [[INF]*W for _ in range(H)]
direc = [[1,0], [-1,0], [0,1], [0,-1]]

def dfs():
    ret = INF
    q = deque([])
    for h in range(H):
        for w in range(W):
            if Gmap[h][w]=='#':
                q.append([h,w])
                Dist[h][w]=0
    while len(q)!=0:
        #スタック取り出し(先頭)幅優先
        h,w = q.popleft()
        for d in direc:
            hs, ws = h + d[0], w + d[1]
            if not (0<=hs<H and 0<=ws<W):
                continue
            if Gmap[hs][ws]=='.' and Dist[hs][ws]==INF:
                q.append([hs,ws])
                Dist[hs][ws] = Dist[h][w] + 1
                ret = max(ret, Dist[hs][ws])
    return ret

def main():
    print(dfs())

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
L=[]
for i in range(H):
    l=list(input())
    L.append(l)
T=[[0 for i in range(W)] for l in range(H)]
t=0
SS=[]
for i in range(H):
    for l in range(W):
        if L[i][l]=='#':
            t+=1
            T[i][l]=1
            SS.append([i,l])
count=0
while 1:
    count+=1
    S=SS
    SS=[]
    while 1:
        x=S[0][1]
        y=S[0][0]
        if 0<=y and y<H-1:
            if L[y+1][x]=="." and T[y+1][x]==0:
                s=[y+1,x]
                T[y+1][x]=1
                SS.append(s)
                t+=1
        if 0<y and y<=H-1:
            if L[y-1][x]=="." and T[y-1][x]==0:
                s=[y-1,x]
                T[y-1][x]=1
                SS.append(s)
                t+=1
        if 0<=x and x<W-1:
            if L[y][x+1]=='.' and T[y][x+1]==0:
                s=[y,x+1]
                T[y][x+1]=1
                SS.append(s)
                t+=1
        if 0<x and x<=W-1:
            if L[y][x-1]=='.' and T[y][x-1]==0:
                s=[y,x-1]
                T[y][x-1]=1
                SS.append(s)
                t+=1
        S.pop(0)
        if len(S)==0:
            break
    if t==H*W:
        break
print(count)

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
# from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

cnt=0

itta=[[10**9]*w for i in range(h)]

d=deque()
aaa=d.append
for i in range(h):
    for j in range(w):
        if a[i][j]=="#":
            aaa([i,j])
            itta[i][j]=0

# print(d)
# print(itta)

while d:
    cnt+=1
    for l in range(len(d)):
        nh,nw=d.popleft()
        for i in range(4):
            d1,d2=direction[i]
            if 0<=nh+d1<h and 0<=nw+d2<w:
                if itta[nh+d1][nw+d2]==float('inf'):
                    itta[nh+d1][nw+d2]=cnt
                    aaa([nh+d1,nw+d2])
    # print(d)

# print(itta)
print(cnt-1)

Submission status: Wrong Answer

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

h,w = map(int,input().split())
c = []
for i in range(h):
  a = input()
  c.append(a)
  
from collections import deque

d = [[False] * w for i in range(h)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
cnt = 0
que = deque([])
for i in range(h):
  for j in range(w):
    if c[i][j] == '#':
      que.append((i,j,0))
      d[i][j] = 0
      cnt += 1

ans = 0
while que:
  p = que.popleft()
  for i in range(4):
      nw = p[1] + dx[i]
      nh = p[0] + dy[i]
      if 0 <= nw < w and 0 <= nh < h and d[nh][nw] == False:
          que.append((nh,nw,p[2]+1))
          cnt += 1
          d[nh][nw] = p[2]+1
          ans = max(ans,p[2]+1)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]

move = ((1, 0), (-1, 0), (0, 1), (0, -1))
visited = [[-1] * W for _ in range(H)]

ans = 0
que = deque([])
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            visited[i][j] = 0
            for di, dj in move:
                ni, nj = i + di, j + dj
                if 0 <= ni < H and 0 <= nj < W and visited[ni][nj] == -1:
                    que.append((ni, nj, 1))
                    visited[ni][nj] = 1
                    ans = 1

while que:
    i, j, d = que.popleft()
    for di, dj in move:
        ni, nj = i + di, j + dj
        if 0 <= ni < H and 0 <= nj < W and visited[ni][nj] == -1:
            que.append((ni, nj, d+1))
            visited[ni][nj] = d+1
            ans = d+1

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    h, w = input_to_int_map()
    zone = [input() for i in range(h)]

    from collections import deque
    black_cell = deque()

    black_distance = [[-1] * w for i in range(h)]
    for i, row in enumerate(zone):
        for j, cell in enumerate(row):
            if cell == "#":
                black_cell.append((i, j))

    while black_cell:
        black_h, black_w = black_cell.popleft()
        pre_black_distance = black_distance[black_h][black_w]
        for i, j in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
            move_h = black_h + i
            move_w = black_w + j
            if move_h < 0 or move_w < 0 or move_h >= h or move_w >= w:
                continue
            if black_distance[move_h][move_w] == -1:
                black_distance[move_h][move_w] = pre_black_distance + 1
                black_cell.append((move_h, move_w))

    return max(list(map(max, black_distance)))


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

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.buffer.readline
h,w=map(int,input().split())
from collections import deque as que
q=que()
now=0
reached=[[0]*w for i in range(h)]
for i in range(h):
    s=input()
    for j in range(w):
        if s[j]==35:q.append((i,j));reached[i][j]=1;now+=1
ans=0
p=que()
for inf in range(h*w-now):
    while  len(q):
        a,s=q.pop()
        for x,y in [(a+1,s),(a-1,s),(a,s+1),(a,s-1)]:
            if 0<=x<h and 0<=y<w and reached[x][y]!=1:
                reached[x][y]=1
                p.append((x,y))
                now+=1
    ans+=1
    q=p
    p.clear()
    if now==h*w:break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    h,w=map(int,input().split())
    A=[input() for _ in range(h)]
    M=[[0]*w for _ in range(h)]
    for i in range(1,h):
        for j in range(w):
            if A[i][j]!="#":
                M[i][j]=M[i-1][j]+1
    for i in range(h-2,-1,-1):
        for j in range(w):
            if A[i][j]!="#":
                if M[i][j]==0:M[i][j]=M[i+1][j]+1
                else:M[i][j]=min(M[i+1][j]+1,M[i][j])
    for j in range(1,w):
        for i in range(h):
            if A[i][j]!="#":
                if M[i][j]==0:M[i][j]=M[i][j-1]+1
                else:M[i][j]=min(M[i][j-1]+1,M[i][j])                   
    for j in range(w-2,-1,-1):
        for i in range(h):
            if A[i][j]!="#":
                if M[i][j]==0:M[i][j]=M[i][j+1]+1
                else:M[i][j]=min(M[i][j+1]+1,M[i][j])   
    print(max([max(m) for m in M]))

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
print(a)
dist = [[-1]*w for _ in range(h)]  # 黒ならdist[i][j]は0、白なら-1

black_cells = deque()

for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            black_cells.append([i, j])
            dist[i][j] = 0

def bfs(black_cells, dist):
    d = 0
    while black_cells:
        print(dist)
        h_black, w_black = black_cells.popleft()
        d=dist[h_black][w_black]
        for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            new_h = h_black+dy
            new_w = w_black+dx
            if new_h < 0 or h <= new_h or new_w < 0 or w <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w] = d+1
                black_cells.append([new_h, new_w])
    return d

d = bfs(black_cells, dist)
print(d)

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
print(a)
dist = [[-1]*w for _ in range(h)]  # 黒ならdist[i][j]は0、白なら-1

black_cells = deque()

for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            black_cells.append([i, j])
            dist[i][j] = 0

def bfs(black_cells, dist):
    d = 0
    while black_cells:
        print(dist)
        h_black, w_black = black_cells.popleft()
        d=dist[h_black][w_black]
        for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            new_h = h_black+dy
            new_w = w_black+dx
            if new_h < 0 or h <= new_h or new_w < 0 or w <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w] = d+1
                black_cells.append([new_h, new_w])
    return max(sum(dist,[]))

d = bfs(black_cells, dist)
print(d)

Submission status: Wrong Answer

Solution code:
import copy
import itertools
    
h,w = (int(x) for x in input().split())
ab = []
step = []
count = 0
for i in range(h):
    a = list(input())
    ab.append(a+['#'])
    for j in range(w):
        if a[j] == '#':
            step.append([i,j])
            count += 1

ab.append(list('#'*w))           

dp = [[0 for a in range(w)] for b in range(h)]
 
while len(step) > 0 and count < h*w:
    y, x = step.pop(0)
    if ab[y+1][x] == '.' and dp[y+1][x] == 0 :
        step.append([y+1, x])
        dp[y+1][x] = dp[y][x] + 1
        count += 1
    if ab[y][x+1] == '.' and dp[y][x+1] == 0 :
        step.append([y, x+1])
        dp[y][x+1] = dp[y][x] + 1
        count += 1
    if ab[y-1][x] == '.' and dp[y-1][x] == 0 :
        step.append([y-1, x])
        dp[y-1][x] = dp[y][x] + 1
        count += 1
    if ab[y][x-1] == '.' and dp[y][x-1] == 0 :
        step.append([y, x-1])
        dp[y][x-1] = dp[y][x] + 1
        count += 1
    print(step)

mx = max(list(itertools.chain.from_iterable(dp)))
 
print(mx)

Submission status: Wrong Answer

Solution code:
from collections import deque

row, col = map(int, input().split())
graph = [list(input()) for i in range(row)]

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

queue = deque([])
for i in range(row):
  for j in range(col):
    if graph[i][j] == "#":
      queue.append([i, j])
      
counter = 1
while len(queue) > 0:
  
  before = len(queue)

  for i in range(counter):
    v = queue.popleft()
    for j in range(4):
      next_y, next_x = v[0]+dy[j], v[1]+dx[j]
      if not 0 <= next_y < row or not 0 <= next_x < col:
        continue
      if graph[next_y][next_x] == '#':
        continue 

      graph[next_y][next_x] = "#"
      queue.append([next_y, next_x])
  
  after = len(queue)
  counter = after - before
  if counter <= -1:
    break
  ans += 1
      
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

row, col = map(int, input().split())
graph = [list(input()) for i in range(row)]

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

queue = deque([])
for i in range(row):
  for j in range(col):
    if graph[i][j] == "#":
      queue.append([i, j])
      
counter = 1
while len(queue) > 0:
  
  before = len(queue)

  for i in range(counter):
    v = queue.popleft()
    for j in range(4):
      next_y, next_x = v[0]+dy[j], v[1]+dx[j]
      if not 0 <= next_y < row or not 0 <= next_x < col:
        continue
      if graph[next_y][next_x] == '#':
        continue 

      graph[next_y][next_x] = "#"
      queue.append([next_y, next_x])
  
  after = len(queue)
  counter = after - before
  if counter <= 0:
    break
  ans += 1
      
print(ans)

Submission status: Wrong Answer

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

w=[]
cnt=[[10**7 for _ in range(W)]for i in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]=="#":
            for y,x in w:
                val=abs(y-i)+abs(x-j)
                cnt[y][x]=min(cnt[y][x],val)
            w=[]
        else:
            w.append([i,j])
w=[]
for i in range(H):
    for j in range(W):
        y=i
        if i%2==0:
            x=j
        else:
            x=W-1-j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])
w=[]
for i in range(H):
    for j in range(W):
        y=i
        x=W-1-j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])

for i in range(H):
    for j in range(W):
        y=i
        if i%2==0:
            x=W-1-j
        else:
            x=j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])

for j in range(W):
    for i in range(H):
        y=i
        x=j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])
for j in range(W):
    for i in range(H):
        if j%2==0:
            y=i
        else:
            y=H-1-i
        x=j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])
for j in range(W):
    for i in range(H):
        y=H-1-i
        x=j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])
for j in range(W):
    for i in range(H):
        if j%2==0:
            y=H-1-i
        else:
            y=i
        x=j
        if A[y][x]=="#":
            for n,m in w:
                val=abs(n-y)+abs(m-x)
                cnt[n][m]=min(cnt[n][m],val)
            w=[]
        else:
            w.append([y,x])
ans=0
for i in range(H):
    for j in range(W):
        if A[i][j]==".":
            ans=max(ans,cnt[i][j])

print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
mapl = [list(input()) for i in range(h)]
searched = [[False for i in range(w)] for j in range(h)]
move = ((0,1),(1,0),(-1,0),(0,-1))
 
from collections import deque
s_queue = deque()
for y in range(h):
    for x in range(w):
        if(mapl[y][x]=='#'):
            s_queue.append([x,y,0])
            searched[y][x] = True
 
 
 
def bfs():
    last=0
    
    while(len(s_queue)):
        y,x,cnt = s_queue.popleft()
        for a,b in move:
            ny,nx=y+a,x+b
            
            if(ny<0 or ny>=h or nx<0 or nx>=w):
                continue
            if(searched[ny][nx]):
                continue
            searched[ny][nx]=True
            last = cnt + 1
            s_queue.append([ny,nx,last])
    return last
 
cnt = bfs()
print(cnt)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
mapl = [list(input()) for i in range(h)]
searched = [[False for i in range(w)] for j in range(h)]
move = ((0,1),(1,0),(-1,0),(0,-1))
s_queue = []
for y in range(h):
    for x in range(w):
        if(mapl[y][x]=='#'):
            s_queue.append([y,x,0])
            searched[y][x] = True


def bfs():
    last=0
    mlast=-1
    while(len(s_queue)>0):
        y,x,cnt = s_queue.pop(0)
        for a,b in move:
            ny,nx=x+a,y+b
            if(ny<0 or ny>=h or nx<0 or nx>=w):
                continue
            if(searched[ny][nx]):
                continue
            searched[ny][nx]=True
            last = cnt + 1
            if(mlast<last):
                mlast=last
            s_queue.append([ny,nx,last])
    return mlast

cnt = bfs()
print(cnt)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
mapl = [list(input()) for i in range(h)]
searched = [[False for i in range(w)] for j in range(h)]
move = ((0,1),(1,0),(-1,0),(0,-1))
s_queue = []
for y in range(h):
    for x in range(w):
        if(mapl[y][x]=='#'):
            s_queue.append([y,x,0])
            searched[y][x] = True


def bfs():
    last=0
    
    while(len(s_queue)>0):
        y,x,cnt = s_queue.pop(0)
        for a,b in move:
            ny,nx=x+a,y+b
            if(ny<0 or ny>=h or nx<0 or nx>=w):
                continue
            if(searched[ny][nx]):
                continue
            searched[ny][nx]=True
            last = cnt + 1
            s_queue.append([ny,nx,last])
    return last

cnt = bfs()
print(cnt)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
mapl = [list(input()) for i in range(h)]
searched = [[False for i in range(w)] for j in range(h)]
move = ((0,1),(1,0),(-1,0),(0,-1))
s_queue = []
for y in range(h):
    for x in range(w):
        if(mapl[y][x]=='#'):
            s_queue.append([y,x,0])
            searched[y][x] = True


def bfs():
    last=0
    
    while(len(s_queue)>0):
        y,x,cnt = s_queue.pop(0)
        for a,b in move:
            ny,nx=x+a,y+b
            if(ny<0 or ny>=h or nx<0 or nx>=w or mapl[ny][nx]=='#'):
                continue
            if(searched[ny][nx]):
                continue
            searched[ny][nx]=True
            mapl[ny][nx]='#'
            last = cnt + 1
            s_queue.append([ny,nx,last])
    return last

cnt = bfs()
print(cnt)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
mapl = [list(input()) for i in range(h)]
searched = [[False for i in range(w)] for j in range(h)]
move = ((0,1),(1,0),(-1,0),(0,-1))
s_queue = []
for y in range(h):
    for x in range(w):
        if(mapl[y][x]=='#'):
            s_queue.append([y,x,0])
            searched[y][x] = True


def bfs():
    last=0
    
    while(len(s_queue)>0):
        y,x,cnt = s_queue.pop(0)
        for a,b in move:
            ny,nx=x+a,y+b
            if(ny<0 or ny>=h or nx<0 or nx>=w or mapl[ny][nx]=='#'):
                continue
            if(searched[ny][nx]):
                continue
            searched[ny][nx]=True
            mapl[ny][nx]=='#'
            last = cnt + 1
            s_queue.append([ny,nx,last])
    return last

cnt = bfs()
print(cnt)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
mapl = [list(input()) for i in range(h)]
searched = [[False for i in range(w)] for j in range(h)]
move = ((0,1),(1,0),(-1,0),(0,-1))
s_queue = []
for y in range(h):
    for x in range(w):
        if(mapl[y][x]=='#'):
            s_queue.append([y,x,0])
            searched[y][x] = True


def bfs():
    last=0
    
    while(len(s_queue)>0):
        y,x,cnt = s_queue.pop(0)
        for a,b in move:
            ny,nx=x+a,y+b
            if(ny<0 or ny>=h or nx<0 or nx>=w or mapl[ny][nx]=='#'):
                continue
            if(searched[ny][nx]):
                continue
            searched[ny][nx]=True
            mapl[y][x]=='#'
            last = cnt + 1
            s_queue.append([ny,nx,last])
    return last

cnt = bfs()
print(cnt)

Submission status: Wrong Answer

Solution code:


def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    H,W=MI()
    A=[]
    A.append(list("#"*(W+2)))
    for i in range(H):
        A.append(list("#"+input()+"#"))
    A.append(list("#"*(W+2)))
    
    
    import queue
    q=queue.Queue()
    
    inf=-1
    dMap=[[inf]*(W+2)for _ in range(H+2)]
    for i in range(1,H+1):
        for j in range(1,W+1):
            if A[i][j]=="#":
                q.put((i,j,0))
                dMap[i][j]=0
                
    for i in range(H+2):
        dMap[i][0]=0
        dMap[i][-1]=0
        
    for j in range(W+2):
        dMap[0][j]=0
        dMap[-1][j]=0
        
    
    dx=[0,0,1,-1]
    dy=[1,-1,0,0]
    

    while not q.empty():
        ii,jj,d=q.get()
        dMap[ii][jj]=d
        for k in range(4):
            nxt_i=ii+dx[k]
            nxt_j=jj+dy[k]
            if dMap[nxt_i][nxt_j]==inf:
                q.put((nxt_i,nxt_j,d+1))
                
    print(max(dMap))
                
                
    
            

main()

Submission status: Wrong Answer

Solution code:
## coding: UTF-8


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

c = []
for i in range(H):
    c.append(list(input()))

fresh = []
for i in range(H):
    fresh.append([True]*W)
#print(fresh)

queue = []
n = []
for i in range(H):
    for j in range(W):
        if(c[i][j] == '#'):
            fresh[i][j] = False
            queue.append([i, j])
#print(fresh)
#print(queue)

print(-1)

Submission status: Wrong Answer

Solution code:
## coding: UTF-8


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

c = []
'''
c.append( ['w']*(W+2) )
for i in range(H):
    c.append(['w'] + list(input()) + ['w'])
c.append(['w']*(W+2))
'''
for i in range(H):
    c.append(list(input()))



#fresh = [[True]*(W)]*(H)　#これはコピーする際にIDが共通になってしまうので、よくない。各行でIDが同じなので、どこかの要素を変えると一列全部変わってしまう
fresh = []
for i in range(H):
    fresh.append([True]*W)
#print(fresh)

queue = []
n = []
for i in range(H):
    for j in range(W):
        if(c[i][j] == '#'):
            fresh[i][j] = False
            queue.append([i, j])
#print(fresh)
#print(queue)

print(-1)

Submission status: Wrong Answer

Solution code:
## coding: UTF-8
import sys
#input = sys.stdin.readline

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

c = []
'''
c.append( ['w']*(W+2) )
for i in range(H):
    c.append(['w'] + list(input()) + ['w'])
c.append(['w']*(W+2))
'''
for i in range(H):
    c.append(list(input()))

#fresh = [[True]*(W)]*(H)　#これはコピーする際にIDが共通になってしまうので、よくない。各行でIDが同じなので、どこかの要素を変えると一列全部変わってしまう
fresh = []
for i in range(H):
    fresh.append([True]*W)
#print(fresh)

queue = []
n = []
for i in range(H):
    for j in range(W):
        if(c[i][j] == '#'):
            fresh[i][j] = False
            queue.append([i, j])
#print(fresh)
#print(queue)

print(-1)

Submission status: Wrong Answer

Solution code:
## coding: UTF-8
#from math import gcd 旧環境でこれを有効化するとREを吐きます
 
H,W = map(int,input().split())

c = []
c.append(['w']*(W+2))
for i in range(H):
    c.append(['w'] + list(input()) + ['w'])
c.append(['w']*(W+2))
 
 
 
 
#queue = [start]
next_list = [] #次の歩数で探すものを格納しておく
step = 1
#mode = 0
#answer_mode = 0
status = True #ゴール判定でゴールについていたらFalseにする
#answer = -1
 
queue = []
for i in range(H+2):
    for j in range(W+2):
        if(c[i][j] == '#'):
            queue.append([i, j])

print(-1)

Submission status: Wrong Answer

Solution code:
## coding: UTF-8
from decimal import *
from itertools import permutations, combinations,combinations_with_replacement,product
import math
#from math import gcd 旧環境でこれを有効化するとREを吐きます
 
s = input().split()
p = [int(w) for w in s] #これで勝手にNまで拾ってくれます
H = p[0]
W = p[1]
 
a = []
for i in range(H):
    a.append(input())
 
 
#白ベースで最寄りの黒を探す方針
#パディングは'.'でも'#'でもないもので埋める
b = ''
for i in range(W+2):
    b += 'w'
#print(b)
#上と下と右と左に空きマスを挿入する
a.insert(0, b)
for i in range(1, 1+H):
    a[i] = 'w' + a[i] + 'w'
a.append(b)
 
 
header = ' '
for i in range(len(a[0])):
    header += ('{}'.format(i))
 
c=[]
for i in range(H+2):
    c.append([])
 
for i in range(H+2):
    for j in range(W+2):
        c[i].append(a[i][j])
 
 
def see_banmen():
    print(header)
    for i in range(len(c)):
        print('{}{}'.format(i,''.join(c[i])))
#see_banmen()
 
 
 
#queue = [start]
next_list = [] #次の歩数で探すものを格納しておく
step = 1
#mode = 0
#answer_mode = 0
status = True #ゴール判定でゴールについていたらFalseにする
#answer = -1
 
queue = []
for i in range(H+2):
    for j in range(W+2):
        if(c[i][j] == '#'):
            queue.append([i, j])

print(-1)

Submission status: Wrong Answer

Solution code:
def main():
    import queue
    import sys
    input = sys.stdin.readline
    h,w = map(int,input().split())
    
    #aij = [input() for i in range(h)]
    
    moji = [[0 for i in range(w)] for j in range(h)]
    
    for i in range(h):
        aij = input()
        #print(aij)
        for j in range(w):
            moji[i][j] = aij[j]
    
    #moji = aij
    #print(moji,aij)
    #exit()
    
    #print(aij)
    vx = [0,1,0,-1]
    vy = [1,0,-1,0]
    
    ans = 0
    q = queue.Queue()
    
    for i in range(h*w):
        y = i//w
        x = i%w
        #print(i)
        if moji[y][x] == '#':
            q.put(i)
    
    ans = -1
    
    while not q.empty():
        #print(q.qsize())
        for i in range(q.qsize()):
            tmp = q.get()
            x = tmp//w
            y = tmp%w
            #print(tmp,num)
            for j in range(4):
                nx = x+vx[j]
                ny = y+vy[j]
                #print(ny,nx)
                if nx < 0 or nx >= w or ny < 0 or ny >= h:
                    continue
                if moji[ny][nx] == ".":
                    moji[ny][nx] = '#'
                    q.put(ny*w+nx)
        ans += 1
    
    print(ans)                  

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import queue
    h,w = map(int,input().split())
    
    #aij = [input() for i in range(h)]
    
    moji = [[0 for i in range(w)] for j in range(h)]
    
    for i in range(h):
        aij = input()
        #print(aij)
        for j in range(w):
            moji[i][j] = aij[j]
    
    #moji = aij
    #print(moji,aij)
    #exit()
    
    #print(aij)
    vx = [0,1,0,-1]
    vy = [1,0,-1,0]
    
    ans = 0
    q = queue.Queue()
    
    for i in range(h*w):
        y = i//w
        x = i%w
        #print(i)
        if moji[y][x] == '#':
            q.put(i)
    
    ans = -1
    
    while not q.empty():
        #print(q.qsize())
        for i in range(q.qsize()):
            tmp = q.get()
            x = tmp//w
            y = tmp%w
            #print(tmp,num)
            for j in range(4):
                nx = x+vx[j]
                ny = y+vy[j]
                #print(ny,nx)
                if nx < 0 or nx >= w or ny < 0 or ny >= h:
                    continue
                if moji[ny][nx] == ".":
                    moji[ny][nx] = '#'
                    q.put(ny*w+nx)
        ans += 1
    
    print(ans)                  

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

dist = [[-1]*W for i in range(H)]
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
for i in range(H):
  for j in range(W):
    if A[i][j] == '#':
      dist[i][j] = 0
      que.append((i, j))

d = 0
while que:
  y, x = que.popleft()
  d = dist[y][x]
  for dx, dy in dd:
    nx = x + dx; ny = y + dy
    if not 0 <= nx < W or not 0 <= ny < H or dist[ny][nx] != -1:
      continue
    dist[ny][nx] = d + 1
    que.append((ny, nx))

print(max(max(dist)))

Submission status: Wrong Answer

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

#aij = [input() for i in range(h)]

moji = [[0 for i in range(w)] for j in range(h)]

for i in range(h):
    aij = input()
    #print(aij)
    for j in range(w):
        moji[i][j] = aij[j]

#moji = aij
#print(moji,aij)
#exit()

#print(aij)
vx = [0,1,0,-1]
vy = [1,0,-1,0]

ans = 0
q = queue.Queue()

for i in range(h*w):
    y = i//w
    x = i%w
    #print(i)
    if moji[y][x] == '#':
        q.put(i)

ans = -1

while not q.empty():
    #print(q.qsize())
    for i in range(q.qsize()):
        tmp = q.get()
        x = tmp//w
        y = tmp%w
        #print(tmp,num)
        for j in range(4):
            nx = x+vx[j]
            ny = y+vy[j]
            #print(ny,nx)
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue
            if moji[ny][nx] == ".":
                moji[ny][nx] = '#'
                q.put(ny*w+nx)
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
def agc033_a():
  h, w = map(int, input().split())
  grid = [[s for s in str(input())] for _ in range(h)]
  inf = 10**9

  # 黒マスからの距離(#は0)
  dist = [[inf] * w for _ in range(h)]
  for i in range(h):
    for j in range(w):
      if grid[i][j] == '#':
        dist[i][j] = 0

  def ask_dist(i, j):
    adj = inf
    if 0 < i: adj = min(adj, dist[i-1][j])
    if i < h-1: adj = min(adj, dist[i+1][j])
    if 0 < j: adj = min(adj, dist[i][j-1])
    if j < w-1: adj = min(adj, dist[i][j+1])
    if adj != inf:
      return adj + 1
    else:
      return inf

  # 1往復したら距離確定する？？ TLEする？？
  d_max = 0
  for i in range(h):
    for j in range(w):
      if dist[i][j] != inf: continue
      d_tmp = ask_dist(i, j)
      if d_tmp != inf:
        dist[i][j] = d_tmp
        d_max = max(d_max, d_tmp)
  for i in range(h-1, -1, -1):
    for j in range(w-1, -1, -1):
      if dist[i][j] != inf: continue
      d_tmp = ask_dist(i, j)
      if d_tmp != inf:
        dist[i][j] = d_tmp
        d_max = max(d_max, d_tmp)
  print(d_max)

agc033_a()