Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter

import sys
def input():
	return sys.stdin.readline().strip()

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

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

num = [[0]*W for _ in range(H)]
for i in range(H):
	for j in range(W):
		if A[i][j] == ".":
			num[i][j] = 1000000

dxy = [(-1, 0), (1, 0), (0, -1), (0, 1)]
def saiki(i, j, count):
	next_to = []
	#print(i, j)
	#count = copy.deepcopy(count)
	count[i][j] = 1
	for n in range(4):
		x = i + dxy[n][0]
		y = j + dxy[n][1]
		if x < H and y < W and 0 <= x and 0 <= y:
			if num[x][y] > 10000 and count[x][y] == 0:
				saiki(x, y, count)
				#print(i, j)
			next_to.append(num[x][y])
	num[i][j] = min(next_to) + 1
	#print(i, j, num[i][j], next_to,num)

count = [[0] * W for _ in range(H)]
for i in range(H):
	for j in range(W):
		if num[i][j] > 10000:

			saiki(i, j, count)

max_num = 0
for i in range(H):
	for j in range(W):
		max_num = max(max_num, num[i][j])

print(max_num)

Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

now = deque()
for i in range(H):
	for j in range(W):
		if A[i][j] == "#":
			now.append([i,j])
			dist[i][j] = 0


while len(now) > 0:
	loc = now.popleft()
	if loc[0] < H - 1 and dist[loc[0] + 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] + 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] + 1, loc[1]])
	if loc[0] > 0 and dist[loc[0] - 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] - 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] - 1, loc[1]])
	if loc[1] < W - 1 and dist[loc[0]][loc[1] + 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] + 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] + 1])
	if loc[1] > 0 and dist[loc[0]][loc[1] - 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] - 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] - 1])

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

Buggy python submission (verdict: Time Limit Exceeded):

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

times = 0
now = []
for i in range(H):
	for j in range(W):
		if A[i][j] == "#":
			now.append([i,j])
			dist[i][j] = times


while True:
	times += 1
	now2 = []
	for loc in now:
		if loc[0] < H - 1 and dist[loc[0] + 1][loc[1]] > times:
			dist[loc[0] + 1][loc[1]] = times
			now2.append([loc[0] + 1, loc[1]])
		if loc[0] > 0 and dist[loc[0] - 1][loc[1]] > times:
			dist[loc[0] - 1][loc[1]] = times
			now2.append([loc[0] - 1, loc[1]])
		if loc[1] < W - 1 and dist[loc[0]][loc[1] + 1] > times:
			dist[loc[0]][loc[1] + 1] = times
			now2.append([loc[0], loc[1] + 1])
		if loc[1] > 0 and dist[loc[0]][loc[1] - 1] > times:
			dist[loc[0]][loc[1] - 1] = times
			now2.append([loc[0], loc[1] - 1])
	if len(now2) == 0:
		break
	now = copy.deepcopy(now2)

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




Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

now = deque()
for i in range(H):
	for j in range(W):
		if A[i][j] == "#":
			now.append([i,j])
			dist[i][j] = 0


while len(now) > 0:
	loc = now.popleft()
	if loc[0] < H - 1 and dist[loc[0] + 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] + 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] + 1, loc[1]])
	if loc[0] > 0 and dist[loc[0] - 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] - 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] - 1, loc[1]])
	if loc[1] < W - 1 and dist[loc[0]][loc[1] + 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] + 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] + 1])
	if loc[1] > 0 and dist[loc[0]][loc[1] - 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] - 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] - 1])

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

Buggy python submission (verdict: Time Limit Exceeded):

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

for i in range(H):
	for j in range(W):
		que = deque()
		if A[i][j] == '#':
			dist[i][j] = 0
			que.append([i, j])
			while len(que) > 0:
				loc = que.popleft()
				if loc[0] < H - 1 and A[loc[0] + 1][loc[1]] == "." and dist[loc[0] + 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
					dist[loc[0] + 1][loc[1]] = dist[loc[0]][loc[1]] + 1
					que.append([loc[0] + 1, loc[1]])
				if loc[0] > 0 and A[loc[0] - 1][loc[1]] == "." and dist[loc[0] - 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
					dist[loc[0] - 1][loc[1]] = dist[loc[0]][loc[1]] + 1
					que.append([loc[0] - 1, loc[1]])
				if loc[1] < W - 1 and A[loc[0]][loc[1] + 1] == "." and dist[loc[0]][loc[1] + 1] > dist[loc[0]][loc[1]] + 1:
					dist[loc[0]][loc[1] + 1] = dist[loc[0]][loc[1]] + 1
					que.append([loc[0], loc[1] + 1])
				if loc[1] > 0 and A[loc[0]][loc[1] - 1] == "." and dist[loc[0]][loc[1] - 1] > dist[loc[0]][loc[1]] + 1:
					dist[loc[0]][loc[1] - 1] = dist[loc[0]][loc[1]] + 1
					que.append([loc[0], loc[1] - 1])

num = 0
for i in range(H):
	for j in range(W):
		num = max(num, dist[i][j])

print(num)







Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

now = deque()
for i in range(H):
	for j in range(W):
		if A[i][j] == "#":
			now.append([i,j])
			dist[i][j] = 0


while len(now) > 0:
	loc = now.popleft()
	if loc[0] < H - 1 and dist[loc[0] + 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] + 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] + 1, loc[1]])
	if loc[0] > 0 and dist[loc[0] - 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] - 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] - 1, loc[1]])
	if loc[1] < W - 1 and dist[loc[0]][loc[1] + 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] + 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] + 1])
	if loc[1] > 0 and dist[loc[0]][loc[1] - 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] - 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] - 1])

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

Buggy python submission (verdict: Time Limit Exceeded):

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

for i in range(H):
	for j in range(W):
		que = deque()
		if A[i][j] == '#':
			dist[i][j] = 0
			que.append([i, j])
			while len(que) > 0:
				loc = que.popleft()
				try:
					if loc[0] < H - 1 and A[loc[0] + 1][loc[1]] == "." and dist[loc[0] + 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
						dist[loc[0] + 1][loc[1]] = dist[loc[0]][loc[1]] + 1
						que.append([loc[0] + 1, loc[1]])
				except:
					pass
				try:
					if loc[0] > 0 and A[loc[0] - 1][loc[1]] == "." and dist[loc[0] - 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
						dist[loc[0] - 1][loc[1]] = dist[loc[0]][loc[1]] + 1
						que.append([loc[0] - 1, loc[1]])
				except:
					pass
				try:
					if loc[1] < W - 1 and A[loc[0]][loc[1] + 1] == "." and dist[loc[0]][loc[1] + 1] > dist[loc[0]][loc[1]] + 1:
						dist[loc[0]][loc[1] + 1] = dist[loc[0]][loc[1]] + 1
						que.append([loc[0], loc[1] + 1])
				except:
					pass
				try:
					if loc[1] > 0 and A[loc[0]][loc[1] - 1] == "." and dist[loc[0]][loc[1] - 1] > dist[loc[0]][loc[1]] + 1:
						dist[loc[0]][loc[1] - 1] = dist[loc[0]][loc[1]] + 1
						que.append([loc[0], loc[1] - 1])
				except:
					pass

num = 0
for i in range(H):
	for j in range(W):
		num = max(num, dist[i][j])

print(num)

Fixed submission:

import sys
from collections import deque, defaultdict
import copy
import bisect
#input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

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

dist = [[10000000 for i in range(W)] for j in range(H)]

now = deque()
for i in range(H):
	for j in range(W):
		if A[i][j] == "#":
			now.append([i,j])
			dist[i][j] = 0


while len(now) > 0:
	loc = now.popleft()
	if loc[0] < H - 1 and dist[loc[0] + 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] + 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] + 1, loc[1]])
	if loc[0] > 0 and dist[loc[0] - 1][loc[1]] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0] - 1][loc[1]] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0] - 1, loc[1]])
	if loc[1] < W - 1 and dist[loc[0]][loc[1] + 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] + 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] + 1])
	if loc[1] > 0 and dist[loc[0]][loc[1] - 1] > dist[loc[0]][loc[1]] + 1:
		dist[loc[0]][loc[1] - 1] = dist[loc[0]][loc[1]] + 1
		now.append([loc[0], loc[1] - 1])

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