Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque


H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

def bfs(h,w):
	q=deque()
	K[h][w]=0
	q.append( (h,w) )
	while q:
		h, w=q.popleft()	# newh , neww
		for dx,dy in D:
			if 0<=h+dx<H and 0<=w+dy<W:
				if A[h+dx][w+dy]=="." and K[h+dx][w+dy]>K[h][w]:
					K[h+dx][w+dy]=min( K[h+dx][w+dy], K[h][w]+1 )
					q.append( (h+dx,w+dy) )	


for i in range(H):
	for j in range(W):
		if A[i][j]=="#":
			bfs(i,j)

ans=0
for i in range(H):
	ans=max(ans , max(K[i]))

print ans

Fixed submission:
import sys
from collections import deque

H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

q=deque()

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


while q:
	h,w=q.popleft()
	for dx,dy in D:
		if 0<=h+dx<H and 0<=w+dy<W and K[h+dx][w+dy]>K[h][w]+1:
				K[h+dx][w+dy]=K[h][w]+1
				q.append( (h+dx,w+dy) )	

ans=0
for x in K:
	ans=max(ans, max(x) )
print ans

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque


H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

def bfs(h,w):
	V=[ [ 0 for j in range(W) ] for i in range(H)  ]
	q=deque()
	V[h][w]=1
	K[h][w]=0
	q.append( (h,w) )
	while q:
		h, w=q.popleft()	# newh , neww
		for dx,dy in D:
			if 0<=h+dx<H and 0<=w+dy<W:
				if A[h+dx][w+dy]=="." and K[h+dx][w+dy]>K[h][w]:
					V[h+dx][w+dy]=1
					K[h+dx][w+dy]=min( K[h+dx][w+dy], K[h][w]+1 )
					q.append( (h+dx,w+dy) )	


for i in range(H):
	for j in range(W):
		if A[i][j]=="#":
			bfs(i,j)

ans=0
for i in range(H):
	ans=max(ans , max(K[i]))

print ans

Fixed submission:
import sys
from collections import deque

H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

q=deque()

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


while q:
	h,w=q.popleft()
	for dx,dy in D:
		if 0<=h+dx<H and 0<=w+dy<W and K[h+dx][w+dy]>K[h][w]+1:
				K[h+dx][w+dy]=K[h][w]+1
				q.append( (h+dx,w+dy) )	

ans=0
for x in K:
	ans=max(ans, max(x) )
print ans

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque


H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

def bfs(h,w):
	V=[ [ 0 for j in range(W) ] for i in range(H)  ]
	q=deque()
	V[h][w]=1
	K[h][w]=0
	q.append( (h,w) )
	while q:
		h, w=q.popleft()	# newh , neww
		for dx,dy in D:
			if 0<=h+dx<H and 0<=w+dy<W:
				if A[h+dx][w+dy]=="." and V[h+dx][w+dy]==0:
					V[h+dx][w+dy]=1
					K[h+dx][w+dy]=min( K[h+dx][w+dy], K[h][w]+1 )
					q.append( (h+dx,w+dy) )	


for i in range(H):
	for j in range(W):
		if A[i][j]=="#":
			bfs(i,j)


ans=0
for i in range(H):
	ans=max(ans , max(K[i]))

print ans

Fixed submission:
import sys
from collections import deque

H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

q=deque()

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


while q:
	h,w=q.popleft()
	for dx,dy in D:
		if 0<=h+dx<H and 0<=w+dy<W and K[h+dx][w+dy]>K[h][w]+1:
				K[h+dx][w+dy]=K[h][w]+1
				q.append( (h+dx,w+dy) )	

ans=0
for x in K:
	ans=max(ans, max(x) )
print ans

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque


H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1)]


def bfs(h,w):
	V=[ [ 0 for j in range(W) ] for i in range(H)  ]
	K=[ [ 0 for j in range(W) ] for i in range(H)  ]  #Kyori
	q=deque()
	V[h][w]=1
	q.append( (h,w) )
	while q:
		h, w=q.popleft()	# newh , neww
		for dx,dy in D:
			if 0<=h+dx<H and 0<=w+dy<W:
				if A[h+dx][w+dy]=="#":
					return K[h][w]+1
				if A[h+dx][w+dy]=="." and V[h+dx][w+dy]==0:
					V[h+dx][w+dy]=1
					K[h+dx][w+dy]=K[h][w]+1
					q.append( (h+dx,w+dy) )	

ans=0	
for i in range(H):
	for j in range(W):
		ans=max(ans, bfs(i,j) )


print ans


Fixed submission:
import sys
from collections import deque

H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

q=deque()

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


while q:
	h,w=q.popleft()
	for dx,dy in D:
		if 0<=h+dx<H and 0<=w+dy<W and K[h+dx][w+dy]>K[h][w]+1:
				K[h+dx][w+dy]=K[h][w]+1
				q.append( (h+dx,w+dy) )	

ans=0
for x in K:
	ans=max(ans, max(x) )
print ans

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

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


def bfs(h,w):
	V=[ [ 0 for j in range(W) ] for i in range(W)  ]
	K=[ [ 0 for j in range(W) ] for i in range(W)  ]  #Kyori
	D=[ (-1,0),(1,0),(0,1),(0,-1)]

	q=deque()
	V[h][w]=1
	q.append( (h,w) )
	while q:
	#for i in range(5):
		h, w=q.popleft()	# newh , neww
		for dx,dy in D:
			if 0<=h+dx<H and 0<=w+dy<W:
				if A[h+dx][w+dy]=="#":
					return K[h][w]+1
				if A[h+dx][w+dy]=="." and V[h+dx][w+dy]==0:
					V[h+dx][w+dy]=1
					K[h+dx][w+dy]=K[h][w]+1
					q.append( (h+dx,w+dy) )	

ans=0	
for i in range(H):
	for j in range(W):
		ans=max(ans, bfs(i,j) )

print ans


Fixed submission:
import sys
from collections import deque

H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

q=deque()

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


while q:
	h,w=q.popleft()
	for dx,dy in D:
		if 0<=h+dx<H and 0<=w+dy<W and K[h+dx][w+dy]>K[h][w]+1:
				K[h+dx][w+dy]=K[h][w]+1
				q.append( (h+dx,w+dy) )	

ans=0
for x in K:
	ans=max(ans, max(x) )
print ans

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

A=[ [] for i in range(H) ]
for i in range(H):
	a=raw_input()
	for x in a:
		if x==".":
			A[i].append(0)
		else:
			A[i].append(float("inf"))


D=[ (-1,0),(1,0),(0,1),(0,-1)]


flag=False
cnt=1
for h in range(H):
	for w in range(W):
		u=A[h][w]	#point
		if u==float("inf"):
			for dx,dy in D:
				if 0<=h+dx<H and 0<=w+dy<W and A[h+dx][w+dy]==0:
					flag=True
					A[h+dx][w+dy]=cnt
else:
	if flag!=True:
		cnt-=1
		print cnt
		quit()



while True:
	flag=False
	cnt+=1
	for h in range(H):
		for w in range(W):
			u=A[h][w]	#point
			if u==cnt-1:
				for dx,dy in D:
					if 0<=h+dx<H and 0<=w+dy<W and A[h+dx][w+dy]==0:
						flag=True
						A[h+dx][w+dy]=cnt
	else:
		if flag!=True:
			cnt-=1
			print cnt
			quit()



Fixed submission:
import sys
from collections import deque

H,W=map(int, raw_input().split())
A=[ list( sys.stdin.readline().strip())  for i in range(H) ]


D=[ (-1,0),(1,0),(0,1),(0,-1) ]
K=[ [ float("inf") for j in range(W) ] for i in range(H)  ]  #Kyori

q=deque()

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


while q:
	h,w=q.popleft()
	for dx,dy in D:
		if 0<=h+dx<H and 0<=w+dy<W and K[h+dx][w+dy]>K[h][w]+1:
				K[h+dx][w+dy]=K[h][w]+1
				q.append( (h+dx,w+dy) )	

ans=0
for x in K:
	ans=max(ans, max(x) )
print ans