Buggy python submission (verdict: Time Limit Exceeded):

import copy

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

A = []

for i in range(H):

    a = input()
    la = list(a)
    A.append(la)

Q = []

for i in range(H):
    for j in range(W):

        if A[i][j] == "#":
            A[i][j] = 0
            Q.append([i,j])

ans = 0

while len(Q) > 0:

    now = Q.pop(0)

    if now[0]-1 >= 0 and A[now[0]-1][now[1]] == ".":

        A[now[0]-1][now[1]] = A[now[0]][now[1]] + 1
        Q.append ([ now[0]-1 , now[1] ])

    if now[0]+1 < H and A[now[0]+1][now[1]] == ".":

        A[now[0]+1][now[1]] = A[now[0]][now[1]] + 1
        Q.append ([ now[0]+1 , now[1] ])

    if now[1]-1 >= 0 and A[now[0]][now[1]-1] == ".":

        A[now[0]][now[1]-1] = A[now[0]][now[1]] + 1
        Q.append ([ now[0] , now[1]-1 ])

    if now[1]+1 < W and A[now[0]][now[1]+1] == ".":

        A[now[0]][now[1]+1] = A[now[0]][now[1]] + 1
        Q.append ([ now[0] , now[1]+1 ])

print(A[now[0]][now[1]])

    


Fixed submission:

"""
メモ型再起？
ダイクストラじゃあきつそう
1sec!?

各点高々1回しか選ばれない工夫が必要
qにいれて少しづつ進めてく？
→各点高々4回しか選択されない
"""


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

A = []
q = []

for i in range(H):

    a = list(input())
    A.append(a)

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

ans = 0
while len(q) > 0:

    #print (q)
    
    nq = []
    flag = False

    for i,j in q:
        if i > 0 and A[i-1][j] == ".":
            A[i-1][j] = "#"
            nq.append([i-1,j])
            flag = True
        if i < H-1 and A[i+1][j] == ".":
            A[i+1][j] = "#"
            nq.append([i+1,j])
            flag = True
        if j > 0 and A[i][j-1] == ".":
            A[i][j-1] = "#"
            nq.append([i,j-1])
            flag = True
        if j < W-1 and A[i][j+1] == ".":
            A[i][j+1] = "#"
            nq.append([i,j+1])
            flag = True
    q = nq
    if flag:
        ans += 1


print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

import copy

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

A = []

for i in range(H):

    a = input()
    la = list(a)
    A.append(la)

Q = []

for i in range(H):
    for j in range(W):

        if A[i][j] == "#":
            A[i][j] = 0
            Q.append([i,j])

ans = 0

while len(Q) > 0:

    now = Q.pop(0)

    if now[0]-1 >= 0 and A[now[0]-1][now[1]] == ".":

        A[now[0]-1][now[1]] = A[now[0]][now[1]] + 1
        Q.append ([ now[0]-1 , now[1] ])

    if now[0]+1 < H and A[now[0]+1][now[1]] == ".":

        A[now[0]+1][now[1]] = A[now[0]][now[1]] + 1
        Q.append ([ now[0]+1 , now[1] ])

    if now[1]-1 >= 0 and A[now[0]][now[1]-1] == ".":

        A[now[0]][now[1]-1] = A[now[0]][now[1]] + 1
        Q.append ([ now[0] , now[1]-1 ])

    if now[1]+1 < W and A[now[0]][now[1]+1] == ".":

        A[now[0]][now[1]+1] = A[now[0]][now[1]] + 1
        Q.append ([ now[0] , now[1]+1 ])

print(A[now[0]][now[1]])

    


Fixed submission:

"""
メモ型再起？
ダイクストラじゃあきつそう
1sec!?

各点高々1回しか選ばれない工夫が必要
qにいれて少しづつ進めてく？
→各点高々4回しか選択されない
"""


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

A = []
q = []

for i in range(H):

    a = list(input())
    A.append(a)

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

ans = 0
while len(q) > 0:

    #print (q)
    
    nq = []
    flag = False

    for i,j in q:
        if i > 0 and A[i-1][j] == ".":
            A[i-1][j] = "#"
            nq.append([i-1,j])
            flag = True
        if i < H-1 and A[i+1][j] == ".":
            A[i+1][j] = "#"
            nq.append([i+1,j])
            flag = True
        if j > 0 and A[i][j-1] == ".":
            A[i][j-1] = "#"
            nq.append([i,j-1])
            flag = True
        if j < W-1 and A[i][j+1] == ".":
            A[i][j+1] = "#"
            nq.append([i,j+1])
            flag = True
    q = nq
    if flag:
        ans += 1


print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

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

A = []

for i in range(H):

    a = input()
    la = list(a)
    A.append(la)

flag = True
cnt = 1

#print (A)

while flag:


    flag = False

    OA = copy.deepcopy(A)

    for i in range(H):

        for j in range(W):


            if OA[i][j] != ".":

                if i-1 >= 0:
                    
                    if OA[i-1][j] == ".":
                        A[i-1][j] = cnt
                        flag = True

                if i+1 < H:
                    
                    if OA[i+1][j] == ".":
                        A[i+1][j] = cnt
                        flag = True

                if j-1 >= 0:
                    
                    if OA[i][j-1] == ".":
                        A[i][j-1] = cnt
                        flag = True

                if j+1 < W:

                    #print (j+1)
                    
                    if OA[i][j+1] == ".":
                        A[i][j+1] = cnt
                        flag = True

    if flag:
        cnt += 1
    #print (OA)
    #print (A)
    #print (cnt)


print (cnt-1)

    

                    


Fixed submission:

"""
メモ型再起？
ダイクストラじゃあきつそう
1sec!?

各点高々1回しか選ばれない工夫が必要
qにいれて少しづつ進めてく？
→各点高々4回しか選択されない
"""


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

A = []
q = []

for i in range(H):

    a = list(input())
    A.append(a)

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

ans = 0
while len(q) > 0:

    #print (q)
    
    nq = []
    flag = False

    for i,j in q:
        if i > 0 and A[i-1][j] == ".":
            A[i-1][j] = "#"
            nq.append([i-1,j])
            flag = True
        if i < H-1 and A[i+1][j] == ".":
            A[i+1][j] = "#"
            nq.append([i+1,j])
            flag = True
        if j > 0 and A[i][j-1] == ".":
            A[i][j-1] = "#"
            nq.append([i,j-1])
            flag = True
        if j < W-1 and A[i][j+1] == ".":
            A[i][j+1] = "#"
            nq.append([i,j+1])
            flag = True
    q = nq
    if flag:
        ans += 1


print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy

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

A = []

for i in range(H):

    a = input()
    la = list(a)
    A.append(la)

flag = True
cnt = 1

#print (A)

while flag:


    flag = False

    OA = copy.deepcopy(A)

    for i in range(H):

        for j in range(W):


            if OA[i][j] != ".":

                if i-1 >= 0:
                    
                    if OA[i-1][j] == ".":
                        A[i-1][j] = cnt
                        flag = True

                if i+1 < H:
                    
                    if OA[i+1][j] == ".":
                        A[i+1][j] = cnt
                        flag = True

                if j-1 >= 0:
                    
                    if OA[i][j-1] == ".":
                        A[i][j-1] = cnt
                        flag = True

                if j+1 < W:

                    #print (j+1)
                    
                    if OA[i][j+1] == ".":
                        A[i][j+1] = cnt
                        flag = True

    if flag:
        cnt += 1
    #print (OA)
    #print (A)
    #print (cnt)


print (cnt-1)

    

                    


Fixed submission:

"""
メモ型再起？
ダイクストラじゃあきつそう
1sec!?

各点高々1回しか選ばれない工夫が必要
qにいれて少しづつ進めてく？
→各点高々4回しか選択されない
"""


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

A = []
q = []

for i in range(H):

    a = list(input())
    A.append(a)

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

ans = 0
while len(q) > 0:

    #print (q)
    
    nq = []
    flag = False

    for i,j in q:
        if i > 0 and A[i-1][j] == ".":
            A[i-1][j] = "#"
            nq.append([i-1,j])
            flag = True
        if i < H-1 and A[i+1][j] == ".":
            A[i+1][j] = "#"
            nq.append([i+1,j])
            flag = True
        if j > 0 and A[i][j-1] == ".":
            A[i][j-1] = "#"
            nq.append([i,j-1])
            flag = True
        if j < W-1 and A[i][j+1] == ".":
            A[i][j+1] = "#"
            nq.append([i,j+1])
            flag = True
    q = nq
    if flag:
        ans += 1


print (ans)