Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())

M = [list(map(lambda x:0 if x == "#" else -1, input())) for _ in range(H)]

def set(i, j, x):
    if x < 0 or M[i][j] == 0:
        return
    x += 1
    if M[i][j] < 0:
        M[i][j] = x
    else:
        M[i][j] = min(M[i][j], x)

for i in range(H):
    for j in range(W):
        if j > 0:
            set(i, j, M[i][j-1])
        if i > 0:
            set(i, j, M[i-1][j])

for i in range(H-1, -1, -1):
    for j in range(W-1, -1, -1):
        if j < W-1:
            set(i, j, M[i][j+1])
        if i < H-1:
            set(i, j, M[i+1][j])

print(max(y for x in M for y in x))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())

M = h + w
blck = [M]*(w+2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [M]*(w+2)
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            lineN[r] = 0
    data += [lineN]
data += [blck]


for r, c, d in ((range(1, h+1), range(1,w+1), 1), (range(h, 0, -1), range(w,0,-1), -1)):
    for x in r:
        for y in c:
            data[x][y] = min(data[x][y], data[x-d][y]+1, data[x][y-d]+1)

print(max((data[x][y] for x in range(1, h+1) for y in range(1, w+1))))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())

M = h + w
blck = [M]*(w+2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [M]*(w+2)
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            lineN[r] = 0
    data += [lineN]
data += [blck]
def cp(d, s):
    d[0] = s[0]
    d[1] = s[1]
def ad(d, s):
    d[0] += s[0]
    d[1] += s[1]
def eq(d, s):
    return d[0] == s[0] and d[1] == s[1]
def eql(d, s):
    return d[0] == s[0] or d[1] == s[1]
def mn(s):
    return [-s[0], -s[1]]


cur = [0] * 2
curE = [0] * 2
addS = [0] * 2
addE = [0] * 2

for d in range(1, -2, -2):
    if d == 1:
        curS, curW, addS, addE, mov= [[1, 1], [h+1, w+1], [0, 1], [1, 0], [1, -1]]
    else:
        curS, curW, addS, addE, mov= [[h, w], [0, 0], [0, -1], [-1, 0], [-1, 1]]
    cp(curE, curS)

    for i in range(w+h-1):

        cp(cur, curS)

        while True:
            x = cur[0]
            y = cur[1]
            data[x][y] = min(data[x][y], data[x-d][y]+1, data[x][y-d]+1)

            if eq(cur, curE):
                break
            ad(cur, mov)

        ad(curS, addS)
        if eql(curS, curW):
            ad(curS, mn(addS))
            cp(addS, [addS[1], -addS[0]])
            ad(curS, addS)

        ad(curE, addE)
        if eql(curE, curW):
            ad(curE, mn(addE))
            cp(addE, [-addE[1], addE[0]])
            ad(curE, addE)

print(max((data[x][y] for x in range(1, h+1) for y in range(1, w+1))))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h, w = map(int, input().split())

M = h + w
data = np.ones((h+2, w+2), np.int16) * M
for ln in range(1, h+1):
    line = list(input().rstrip())
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            data[ln,r] = 0


for r, c, d in ((range(1, h+1), range(1,w+1), 1), (range(h, 0, -1), range(w,0,-1), -1)):
    for x in r:
        for y in c:
            data[x][y] = min(data[x][y], data[x-d][y]+1, data[x][y-d]+1)

print(np.max(data[1:h+1,1:w+1]))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ 1 if x == "#" else 0 for x in input()] ]

import numpy as np


A = np.array(A)
WK = np.zeros(A.shape, np.int32)
i = 0
S = H * W
while True:
    if np.min(A) == 1:
        break
    WK -= WK
    WK += A
    A[:-1] |= WK[1:]
    A[1:] |= WK[:-1]
    A[:,:-1] |= WK[:,1:]
    A[:,1:] |= WK[:,:-1]
    i += 1

print(i)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Runtime Error):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ 1 if x == "#" else 0 for x in input()] ]

import numpy as np


A = np.array(A)
WK = np.zeros(A.shape)
i = 0
S = H * W
while True:
    if np.min(A) == 1:
        break
    WK -= WK
    WK += A
    A[:-1] |= WK[1:]
    A[1:] |= WK[:-1]
    A[:,:-1] |= WK[:,1:]
    A[:,1:] |= WK[:,:-1]
    i += 1

print(i)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Runtime Error):
import numpy as np
h, w = map(int, input().split())

M = h + w
data = np.ones((h+2, w+2), np.int16) * M
for ln in range(1, h+1):
    line = list(input().rstrip())
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            data[ln,r] = 0

data = data.reshape((w,h))

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h, w = map(int, input().split())

M = h + w
data = np.ones((h+2, w+2), np.int16) * M
for ln in range(1, h+1):
    line = list(input().rstrip())
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            data[ln,r] = 0


for d in range(1, -2, -2):
    if d == 1:
        cur, curW, addS, addE, mov= [[1, 1], [h+1, w+1], [0, 1], [1, 0], [1, -1]]
    else:
        cur, curW, addS, addE, mov= [[h, w], [0, 0], [0, -1], [-1, 0], [-1, 1]]
    curS = np.array(cur)
    curE = curS.copy()
    addS = np.array(addS)
    addE = np.array(addE)
    mov = np.array(mov)
    for i in range(w+h-1):

        cur = curS.copy()

        while True:
            x, y = cur

            data[x][y] = min(data[x][y], data[x-d][y]+1, data[x][y-d]+1)

            if np.alltrue(cur==curE):
                break
            cur += mov

        curS += addS
        if np.max(curS == curW):
            curS -= addS
            addS = np.array([addS[1], -addS[0]])
            curS += addS


        curE += addE
        if np.max(curE== curW):
            curE -= addE
            addE = np.array([-addE[1], addE[0]])
            curE += addE

print(np.max(data[1:h+1,1:w+1]))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
h, w = map(int, input().split())

M = h + w
blck = [M] * (w + 2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [0] * (w+2)
    lineN[0] = lineN[-1]= M
    for r, letter in zip(range(1, w+1), line):
        if letter == '.':
            lineN[r] = M
    data.append(lineN)
data.append(blck)

data = np.array(data)

for d in range(1, -2, -2):
    if d == 1:
        cur, curW, addS, addE, mov= [[1, 1], [h+1, w+1], [0, 1], [1, 0], [1, -1]]
    else:
        cur, curW, addS, addE, mov= [[h, w], [0, 0], [0, -1], [-1, 0], [-1, 1]]
    curS = np.array(cur)
    curE = curS.copy()
    addS = np.array(addS)
    addE = np.array(addE)
    mov = np.array(mov)
    for i in range(w+h-1):

        cur = curS.copy()

        while True:
            x, y = cur

            data[x][y] = min(data[x][y], data[x-d][y]+1, data[x][y-d]+1)

            if np.alltrue(cur==curE):
                break
            cur += mov

        curS += addS
        if np.max(curS == curW):
            curS -= addS
            addS = np.array([addS[1], -addS[0]])
            curS += addS


        curE += addE
        if np.max(curE== curW):
            curE -= addE
            addE = np.array([-addE[1], addE[0]])
            curE += addE

print(np.max(data[1:h+1,1:w+1]))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
que = deque()

blck = [1] * (w + 2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [0] * (w+2)
    lineN[0] = 1
    lineN[-1] = 1
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            lineN[r] = 1
            que.append((ln, r))
    data.append(lineN)
data.append(blck)


count = 0
next = deque()
while len(que) > 0:
    count += 1
    while len(que) > 0:
        r, c = que.popleft()

        r -= 1
        if data[r][c] == 0:
            next.append((r, c))
            data[r][c] = 1

        r += 2
        if data[r][c] == 0:
            next.append((r, c))
            data[r][c] = 1

        r-=1
        c-=1
        if data[r][c] == 0:
            next.append((r, c))
            data[r][c] = 1

        c+=2
        if data[r][c] == 0:
            next.append((r, c))
            data[r][c] = 1
    que, next = next, que

print(count - 1)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
que = deque()

blck = [1] * (w + 2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [0] * (w+2)
    lineN[0] = 1
    lineN[-1] = 1
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            lineN[r] = 1
            que.append((ln, r))
    data.append(lineN)
data.append(blck)


count = 0
next = deque()
while len(que) > 0:
    count += 1
    while len(que) > 0:
        row, col = que.popleft()
        for pos in ((row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1)):
            r, c = pos
            if data[r][c] == 0:
                next.append(pos)
                data[r][c] = 1
    que, next = next, que

print(count - 1)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
que = deque()

blck = [1] * (w + 2)
data = [blck]
for ln in range(1, h+1):
    line = list(input().rstrip())
    lineN = [0] * (w+2)
    lineN[0] = 1
    lineN[-1] = 1
    for r, letter in zip(range(1, w+1), line):
        if letter == '#':
            lineN[r] = 1
            que.append((ln, r))
    data.append(lineN)
data.append(blck)


count = 0
next = deque()
while len(que) > 0:
    count += 1
    while len(que) > 0:
        row, col = que.popleft()
        for r, c in ((row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1)):
            if data[r][c] == 0:
                next.append((r, c))
                data[r][c] = 1
    que, next = next, que

print(count - 1)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
que = deque()
data = []

for ln in range(h):
    line = list(input().rstrip())
    lineN = [0] * w
    for r, letter in enumerate(line):
        if letter == '#':
            lineN[r] = 1
            que.append((ln, r, 0b1100))
            que.append((ln, r, 0b0011))
    data.append(lineN)

def lt(row, col, dir):
    if dir & 0b0100:
        yield (row - 1, col, 0b0110)
    if dir & 0b0001:
        yield (row + 1, col, 0b1001)
    if dir & 0b0010:
        yield (row, col - 1, 0b0011)
    if dir & 0b1000:
        yield (row, col + 1, 0b1100)

count = 0
while len(que) > 0:
    count += 1
    next = deque()
    while len(que) > 0:
        for r, c, d in lt(*que.popleft()):
            if 0 <= r < h and 0 <= c < w and data[r][c] == 0:
                next.append((r, c, d))
                data[r][c] = 1
    que = next

print(count - 1)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())
que = deque()
data = []

for ln in range(h):
    line = list(input().rstrip())
    lineN = [0] * w
    for r, letter in enumerate(line):
        if letter == '#':
            lineN[r] = 1
            que.append((ln, r, 0b1100))
            que.append((ln, r, 0b0011))
    data.append(lineN)

def lt(row, col, dir):
    if dir & 0b0100:
        yield (row - 1, col, 0b0110)
    if dir & 0b0001:
        yield (row + 1, col, 0b1001)
    if dir & 0b0010:
        yield (row, col - 1, 0b0011)
    if dir & 0b1000:
        yield (row, col + 1, 0b1100)

count = 0
while len(que) > 0:
    count += 1
    next = deque()
    while len(que) > 0:
        for r, c, d in lt(*que.popleft()):
            if 0 <= r < h and 0 <= c < w and data[r][c] == 0:
                next.append((r, c, d))
                data[r][c] = 1
    que = next

print(count - 1)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Runtime Error):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ x == "#" for x in input()] ]

import numpy as np
from scipy import ndimage
A = np.array(A, dtype= np.bool)
B = np.zeros(A.shape, dtype=np.bool)
i = 0
S = H * W
struct = ndimage.generate_binary_structure(2, 1)
while True:
    if np.alltrue(A):
        break
    ndimage.binary_dilation(A, structure=struct, output=B)
    A, B = B, A

    i += 1

print(i)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ x == "#" for x in input()] ]

import numpy as np
from scipy import ndimage
A = np.array(A, dtype= np.bool)
B = np.zeros(A.shape, dtype=np.bool)
i = 0
S = H * W
struct = ndimage.generate_binary_structure(2, 1)
while True:
    if np.alltrue(A):
        break
    ndimage.binary_dilation(A, structure=struct, output=B)
    A, B = B, A

    i += 1

print(i)

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ 1 if x == "#" else 0 for x in input()] ]

import scipy as sp
import scipy.ndimage as ni
A = sp.array(A, dtype= sp.int8)
WKU = sp.zeros(A.shape, dtype=sp.int8)
WKD = sp.zeros(A.shape, dtype=sp.int8)
WKL = sp.zeros(A.shape, dtype=sp.int8)
WKR = sp.zeros(A.shape, dtype=sp.int8)
i = 0
S = H * W
while True:
    if sp.sum(A) == S:
        break
    WK = 0 + A
    ni.shift(A, (0, 1), WKR)
    ni.shift(A, (0, -1), WKL)
    ni.shift(A, (1, 0), WKU)
    ni.shift(A, (-1, 0), WKD)
    A |= WKR | WKL | WKU | WKD
    i += 1

print(i)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ 1 if x == "#" else 0 for x in input()] ]

import scipy as np

A = np.array(A, dtype= np.int16)
WK = np.zeros(A.shape, dtype=np.int16)
i = 0
S = H * W
while True:
    if np.sum(A) == S:
        break
    WK = 0+A
    A[:-1] |= WK[1:]
    A[1:] |= WK[:-1]
    A[:,:-1] |= WK[:,1:]
    A[:,1:] |= WK[:,:-1]
    i += 1

print(i)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = deque()
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat.append((h, w, 0))
            s[w] = 1
        else:
            s[w] = 0
    dp[h] = s

while len(mat):
    h, w, cnt = mat.popleft()

    for t, h, w, cnt in ((h > 0, h - 1, w, cnt),(h < H-1, h + 1, w, cnt),(w > 0, h , w - 1, cnt),(w < W-1, h , w + 1, cnt)):
        if t and dp[h][w] == 0:
            mat.append((h, w, cnt+1))
            dp[h][w] = 1

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = deque()
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat.append((h, w, 0))
    dp[h] = s

while len(mat):
    h, w, cnt = mat.popleft()

    for t, h, w, cnt in ((h > 0, h - 1, w, cnt),(h < H-1, h + 1, w, cnt),(w > 0, h , w - 1, cnt),(w < W-1, h , w + 1, cnt)):
        if t and dp[h][w] == ".":
            mat.append((h, w, cnt+1))
            dp[h][w] = "#"

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = deque()
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat.append((h, w, 0))
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    if dp[nH][nW] == ".":
        mat.append((nH, nW, cnt+1))
        dp[nH][nW] = "#"

while len(mat):
    h, w, cnt = mat.popleft()

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (W*H)
matS = matE = 0
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = dpr

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] < 0:
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = cnt+1

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = deque()
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat.append((h, w, 0))
    dp[h] = dpr

def search(dp, mat, nH, nW, cnt):
    if dp[nH][nW] < 0:
        mat.append((nH, nW, cnt+1))
        dp[nH][nW] = cnt+1

while len(mat) > 0:
    h, w, cnt = mat.popleft()

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# テスト対象（ABC081A - Placing Marbles (https://atcoder.jp/contests/abs/tasks/abc081_a)
H,W = map(int, input().split())
A = 0
dp = []
mat = []
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat += [(h, w, 0)]
    dp += [dpr]

def search(dp, mat, conf, nH, nW, cnt):
    if not conf:
        return
    if dp[nH][nW] < 0:
        mat.insert(0, (nH, nW, cnt+1))
        dp[nH][nW] = cnt+1

while len(mat) > 0:
    h, w, cnt = mat.pop()

    search(dp, mat, h > 0, h - 1, w, cnt)
    search(dp, mat, h < H-1, h + 1, w, cnt)
    search(dp, mat, w > 0, h , w - 1, cnt)
    search(dp, mat, w < W-1, h , w + 1, cnt)

print(cnt)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# テスト対象（ABC081A - Placing Marbles (https://atcoder.jp/contests/abs/tasks/abc081_a)
H,W = map(int, input().split())
A = 0
dp = []
mat = []
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat += [(h, w, 0)]
    dp += [dpr]

def search(dp, mat, conf, nH, nW, cnt):
    if not conf:
        return
    if dp[nH][nW] < 0:
        mat.insert(0, (nH, nW, cnt+1))
        dp[nH][nW] = cnt+1

i = 0
while len(mat) > 0:
    h, w, cnt = mat.pop()

    i+=1
    search(dp, mat, h > 0, h - 1, w, cnt)
    search(dp, mat, h < H-1, h + 1, w, cnt)
    search(dp, mat, w > 0, h , w - 1, cnt)
    search(dp, mat, w < W-1, h , w + 1, cnt)

import numpy as np
print(np.max(dp))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# テスト対象（ABC081A - Placing Marbles (https://atcoder.jp/contests/abs/tasks/abc081_a)
H,W = map(int, input().split())
A = 0
dp = []
mat = []
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat += [(h, w, 0)]
    dp += [dpr]

def search(dp, mat, conf, nH, nW, cnt):
    if not conf:
        return
    if dp[nH][nW] < 0 or cnt+1<dp[nH][nW]:
        if dp[nH][nW] < 0:
            mat.insert(0, (nH, nW, cnt+1))
        dp[nH][nW] = cnt+1

i = 0
while len(mat) > 0:
    h, w, cnt = mat.pop()

    i+=1
    search(dp, mat, h > 0, h - 1, w, cnt)
    search(dp, mat, h < H-1, h + 1, w, cnt)
    search(dp, mat, w > 0, h , w - 1, cnt)
    search(dp, mat, w < W-1, h , w + 1, cnt)

import numpy as np
print(np.max(dp))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
# テスト対象（ABC081A - Placing Marbles (https://atcoder.jp/contests/abs/tasks/abc081_a)
H,W = map(int, input().split())
A = 0
dp = []
mat = []
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat += [(h, w, 0)]
    dp += [dpr]

def search(dp, mat, conf, nH, nW, cnt):
    if not conf:
        return
    if dp[nH][nW] < 0 or cnt+1<dp[nH][nW]:
        if dp[nH][nW] < 0:
            mat += [(nH, nW, cnt+1)]
        dp[nH][nW] = cnt+1

i = 0
while len(mat) > 0:
    h, w, cnt = mat[0]
    mat = mat[1:]

    i+=1
    search(dp, mat, h > 0, h - 1, w, cnt)
    search(dp, mat, h < H-1, h + 1, w, cnt)
    search(dp, mat, w > 0, h , w - 1, cnt)
    search(dp, mat, w < W-1, h , w + 1, cnt)

import numpy as np
print(np.max(dp))


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = 0
dp = []
mat = []
for h in range(H):
    dpr = [-1] * W
    for w, x in zip(range(W), input()):
        if x == "#":
            dpr[w] = 0
            mat += [(h, w, 0)]
    dp += [dpr]

def search(dp, mat, conf, nH, nW, cnt):
    if not conf:
        return
    if dp[nH][nW] < 0 or cnt+1<dp[nH][nW]:
        dp[nH][nW] = cnt+1
        mat += [(nH, nW, cnt+1)]

i = 0
while len(mat) > 0:
    h, w, cnt = mat.pop()

    i+=1
    search(dp, mat, h > 0, h - 1, w, cnt)
    search(dp, mat, h < H-1, h + 1, w, cnt)
    search(dp, mat, w > 0, h , w - 1, cnt)
    search(dp, mat, w < W-1, h , w + 1, cnt)

import numpy as np
print(np.max(dp))

Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = []
for _ in range(H):
    v = 0
    for w, x in zip(range(W), input()):
        if x == "#":
            v |= 1 << w
    A += [ v ]

import numpy as np


A = np.array(A)
WK = np.zeros(A.shape)
MASK = pow(2, W) - 1
MASKSUM = MASK * H
i = 0
while True:
    if np.sum(A) == MASKSUM:
        break
    WK = 0+A
    A[:-1] |= WK[1:]
    A[1:] |= WK[:-1]
    A |= ((WK << 1)&MASK) | (WK >> 1)
    i += 1

print(i)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int, input().split())
A = []
for _ in range(H):
    A += [ [ 1 if x == "#" else 0 for x in input()] ]

import numpy as np


A = np.array(A)
WK = np.zeros(A.shape)
i = 0
S = H * W
while True:
    if np.sum(A) == S:
        break
    WK = 0+A
    A[:-1] |= WK[1:]
    A[1:] |= WK[:-1]
    A[:,:-1] |= WK[:,1:]
    A[:,1:] |= WK[:,:-1]
    i += 1

print(i)


Fixed submission:
from collections import deque
H,W = map(int, input().split())
A = 0
dp = [None] * H
mat = [None] * (H*W)
matS = matE = 0
for h in range(H):
    s = list(input())
    for w, x in zip(range(W), s):
        if x == "#":
            mat[matE] = (h, w, 0)
            matE+=1
    dp[h] = s

def search(dp, mat, nH, nW, cnt):
    global matE
    if dp[nH][nW] == ".":
        mat[matE] = (nH, nW, cnt+1)
        matE+=1
        dp[nH][nW] = "#"

while matS < matE:
    h, w, cnt = mat[matS]
    matS += 1

    if h > 0:
        search(dp, mat, h - 1, w, cnt)
    if h < H-1:
        search(dp, mat, h + 1, w, cnt)
    if w > 0:
        search(dp, mat, h , w - 1, cnt)
    if w < W-1:
        search(dp, mat, h , w + 1, cnt)

print(cnt)