Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
field_dist = [[2000] * W for _ in range(H)]
move_history = [[[False] * 4 for _ in range(W)] for _ in range(H)]
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            init_h = i
            init_w = j
'''
import random
H, W = 1000, 1000
field = [[0] * W for _ in range(H)]
field_dist = [[2000] * W for _ in range(H)]
move_history = [[[False] * 4 for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        if random.randint(0, 2) % 2 == 0:
            field[i][j] = 1
            init_h = i
            init_w = j
'''
            
d = deque([[init_h, init_w]])
reverse_d = []
while len(d) > 0:
    cur_h, cur_w = d.popleft()
    cur_d = abs(init_h - cur_h) + abs(init_w - cur_w)
    
    if field[cur_h][cur_w] == 1:
        field_dist[cur_h][cur_w] = 0
    
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if move_history[cur_h][cur_w][i] == False:
            move_history[cur_h][cur_w][i] = True
            if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
                next_d = abs(init_h - (cur_h + dh)) + abs(init_w - (cur_w + dw))
                if next_d > cur_d:
                    field_dist[cur_h+dh][cur_w+dw] = min(field_dist[cur_h+dh][cur_w+dw], field_dist[cur_h][cur_w]+1)
                    d.append([cur_h+dh, cur_w+dw])
                    reverse_d.append([cur_h+dh, cur_w+dw, cur_h, cur_w])
         
max_dist = 0
while len(reverse_d) > 0:
    s_h, s_w, d_h, d_w = reverse_d.pop()
    if field_dist[s_h][s_w] + 1 < field_dist[d_h][d_w]:
        field_dist[d_h][d_w] = field_dist[s_h][s_w] + 1
    max_dist = max(max_dist, field_dist[s_h][s_w])

print(max_dist)
'''
for i in range(H):
    for j in range(W):
        print(field_dist[i][j], end=' ')
    print()
'''

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque
import numpy as np


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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)

dp = [[H + W] * W for _ in range(H)]
dp2 = [[False] * W for _ in range(H)]
lists = np.zeros((H*W, 2), dtype=int)


count2 = 0
for i in range(H):
    for j in range(W):
        if field[i][j] == 1:
            dp[i][j] = 0
            dp2[i][j] = True
            if i > 0:
                #lists.append([i-1, j])
                dp2[i-1][j] = True
                lists[count2][0] = i - 1
                lists[count2][1] = j
                count2 += 1
            if i < H - 1:
                #lists.append([i+1, j])
                dp2[i+1][j] = True
                lists[count2][0] = i + 1
                lists[count2][1] = j
                count2 += 1
            if j > 0:
                #lists.append([i, j-1])
                dp2[i][j-1] = True
                lists[count2][0] = i
                lists[count2][1] = j - 1
                count2 += 1
            if j < W - 1:
                #lists.append([i, j+1])
                dp2[i][j+1] = True
                lists[count2][0] = i
                lists[count2][1] = j + 1
                count2 += 1
      

count = 0
while True:
    if count2 <= count:
        break
    
    if count > 1000 * 1000:
        break
    
    #i, j = lists.popleft()
    #i, j = lists[count]
    i = lists[count][0]
    j = lists[count][1]
    #print(i, j, dp2, lists)
    #if dp2[i][j] == True:
     #   continue
    #else:
    #    dp2[i][j] = True
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, dp[i-1][j] + 1)
        if dp2[i-1][j] == False:
            #lists.append([i-1, j])
            dp2[i-1][j] = True
            lists[count2][0] = i - 1
            lists[count2][1] = j
            count2 += 1
    if i < H - 1:
        min_dist = min(min_dist, dp[i+1][j] + 1)
        if dp2[i+1][j] == False:
            #lists.append([i+1, j])
            dp2[i+1][j] = True
            lists[count2][0] = i + 1
            lists[count2][1] = j
            count2 += 1
    if j > 0:
        min_dist = min(min_dist, dp[i][j-1] + 1)
        if dp2[i][j-1] == False:
            #lists.append([i, j-1])
            dp2[i][j-1] = True
            lists[count2][0] = i
            lists[count2][1] = j - 1
            count2 += 1
    if j < W - 1:
        min_dist = min(min_dist, dp[i][j+1] + 1)
        if dp2[i][j+1] == False:
            #lists.append([i, j+1])
            dp2[i][j+1] = True
            lists[count2][0] = i
            lists[count2][1] = j + 1
            count2 += 1
        
    dp[i][j] = min_dist
    count += 1


        
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

from collections import deque

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)

dp = [[H + W] * W for _ in range(H)]
dp2 = [[False] * W for _ in range(H)]
lists = deque()
for i in range(H):
    for j in range(W):
        if field[i][j] == 1:
            dp[i][j] = 0
            dp2[i][j] = True
            if i > 0:
                lists.append([i-1, j])
            if i < H - 1:
                lists.append([i+1, j])
            if j > 0:
                lists.append([i, j-1])
            if j < W - 1:
                lists.append([i, j+1])
           
count = 0
while True:
    if len(lists) == 0:
        break
    
    i, j = lists.popleft()
    if dp2[i][j] == True:
        continue
    else:
        dp2[i][j] = True
        
        min_dist = H + W
        if i > 0:
            min_dist = min(min_dist, dp[i-1][j] + 1)
            if dp2[i-1][j] == False:
                lists.append([i-1, j])
        if i < H - 1:
            min_dist = min(min_dist, dp[i+1][j] + 1)
            if dp2[i+1][j] == False:
                lists.append([i+1, j])
        if j > 0:
            min_dist = min(min_dist, dp[i][j-1] + 1)
            if dp2[i][j-1] == False:
                lists.append([i, j-1])
        if j < W - 1:
            min_dist = min(min_dist, dp[i][j+1] + 1)
            if dp2[i][j+1] == False:
                lists.append([i, j+1])
            
        dp[i][j] = min_dist
        
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!


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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)

dp = [[H + W] * W for _ in range(H)]
dp2 = [[False] * W for _ in range(H)]
lists = []
for i in range(H):
    for j in range(W):
        if field[i][j] == 1:
            dp[i][j] = 0
            dp2[i][j] = True
            if i > 0:
                lists.append([i-1, j])
            if i < H - 1:
                lists.append([i+1, j])
            if j > 0:
                lists.append([i, j-1])
            if j < W - 1:
                lists.append([i, j+1])
           
count = 0
while True:
    if len(lists) == 0:
        break
    
    i, j = lists.pop(0)
    if dp2[i][j] == True:
        continue
    else:
        dp2[i][j] = True
        
        min_dist = H + W
        if i > 0:
            min_dist = min(min_dist, dp[i-1][j] + 1)
            if dp2[i-1][j] == False:
                lists.append([i-1, j])
        if i < H - 1:
            min_dist = min(min_dist, dp[i+1][j] + 1)
            if dp2[i+1][j] == False:
                lists.append([i+1, j])
        if j > 0:
            min_dist = min(min_dist, dp[i][j-1] + 1)
            if dp2[i][j-1] == False:
                lists.append([i, j-1])
        if j < W - 1:
            min_dist = min(min_dist, dp[i][j+1] + 1)
            if dp2[i][j+1] == False:
                lists.append([i, j+1])
            
        dp[i][j] = min_dist
        
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(1000000)

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[0] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != 0:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10000)

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[0] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != 0:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10000)

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[-1] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != -1:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

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

field = []
for _ in range(H):
    temp_field = []
    for s in input():
        if s == '.':
            temp_field.append(0)
        else:
            temp_field.append(1)
    field.append(temp_field)
    
dp = [[-1] * W for _ in range(H)]

def rec(i, j):
    if field[i][j] == 1:
        return 0
    
    if dp[i][j] != -1:
        return dp[i][j]
        
    dp[i][j] = H + W
        
    min_dist = H + W
    if i > 0:
        min_dist = min(min_dist, rec(i-1, j) + 1)
    if i < H - 1:
        min_dist = min(min_dist, rec(i+1, j) + 1)
    if j > 0:
        min_dist = min(min_dist, rec(i, j-1) + 1)
    if j < W - 1:
        min_dist = min(min_dist, rec(i, j+1) + 1)
        
    dp[i][j] = min_dist
    
    return min_dist
    
    
for i in range(H):
    for j in range(W):
        rec(i, j)
     
print(max([max(temp_dp) for temp_dp in dp]))

Fixed submission:
# coding: utf-8
# Your code here!

from collections import deque

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

field = [[0] * W for _ in range(H)]
d = deque()
for i in range(H):
    s = input()
    for j in range(W):
        if s[j] == '#':
            field[i][j] = 1
            d.append([i, j, 0])

max_dist = 0
while len(d) > 0:
    cur_h, cur_w, dist = d.popleft()
    for i, (dh, dw) in enumerate([[0, 1], [0, -1], [1, 0], [-1, 0]]):
        if (cur_h + dh) >= 0 and (cur_h + dh) < H and (cur_w + dw) >= 0 and (cur_w + dw) < W:
            if field[cur_h+dh][cur_w+dw] == 0:
                field[cur_h+dh][cur_w+dw] = 1
                d.append([cur_h+dh, cur_w+dw, dist+1])
                max_dist = max(max_dist, dist+1)

print(max_dist)