Buggy python submission (verdict: Runtime Error):
from collections import deque
sys.setrecursionlimit(10 ** 7)
def bfs():
    cnt = 0
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    while len(Q) > 0:
        q = Q.popleft()
        y,x =  q[0], q[1]
        if cnt >= H*W:
            for i in range(H):
                if len(box[i]) == 0:
                    continue
                else:
                    ans.append(max(box[i]))
            return
        for i in range(4):
            ny = y+dy[i]
            nx = x+dx[i]
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "#" and box[ny][nx] == 0:
                cnt += 1
                box[ny][nx] = -1
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "." and box[ny][nx] == 0:
                cnt += 1
                box[ny][nx] = box[y][x] + 1
                Q.append([ny,nx])


H,W = map(int,input().split())
S = [ []for _ in range(H)]
for i in range(H):
    S[i] = list(input())
ans = []
INF = 50**2
Q = deque()
#queueにスタート地点を追加
for i in range(H):
    for j in range(W):
        #各マス目に到達するために必要な移動回数
        box = [[0 for _ in range(W)] for __ in range(H)]
        box[i][j] = 0
        Q.append([i,j])
        bfs()
if len(ans)==0:
    print(0)
    exit()
print(max(ans))

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)


from collections import deque
def bfs():
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    while len(Q) > 0:
        q = Q.popleft()
        y,x =  q[0], q[1]
        if S[y][x] == "#": return
        for i in range(4):
            ny = y+dy[i]
            nx = x+dx[i]
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "#" and box[ny][nx] == -1000:
                box[ny][nx] = -1
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "." and box[ny][nx] == -1000:
                box[ny][nx] = box[y][x] + 1
                Q.append([ny,nx])
    for i in range(H):
        if len(box[i]) == 0:
            continue
        else:
            ans.append(max(box[i]))
    return


H, W = map(int,input().split())
S = [ [] for _ in range(H)]
for i in range(H):S[i] = list(input())
ans = []
Q = deque()
for i in range(H):
    for j in range(W):
        #各マス目に到達するために必要な移動回数
        box = [[-1000 for _ in range(W)] for __ in range(H)]
        Q.append([i,j])
        box[i][j] = 0
        bfs()
if len(ans)==0:
    print(0)
    exit()
print(max(ans))

Buggy python submission (verdict: Runtime Error):
from collections import deque

def bfs():
    cnt = 0
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    while len(Q) > 0:
        q = Q.popleft()
        y,x =  q[0], q[1]
        if cnt >= H*W:
            for i in range(H):
                ans.append(max(box[i]))
            return
        for i in range(4):
            ny = y+dy[i]
            nx = x+dx[i]
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "#" and box[ny][nx] == 0:
                cnt += 1
                box[ny][nx] = -1
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "." and box[ny][nx] == 0:
                cnt += 1
                box[ny][nx] = box[y][x] + 1
                Q.append([ny,nx])


H,W = map(int,input().split())
S = [ []for _ in range(H)]
for i in range(H):
    S[i] = list(input())
ans = []
INF = 50**2
Q = deque()
#queueにスタート地点を追加
for i in range(H):
    for j in range(W):
        #各マス目に到達するために必要な移動回数
        box = [[0 for _ in range(W)] for __ in range(H)]
        box[i][j] = 0
        Q.append([i,j])
        bfs()
print(max(ans))

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)


from collections import deque
def bfs():
    dx = [1,0,-1,0]
    dy = [0,1,0,-1]
    while len(Q) > 0:
        q = Q.popleft()
        y,x =  q[0], q[1]
        if S[y][x] == "#": return
        for i in range(4):
            ny = y+dy[i]
            nx = x+dx[i]
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "#" and box[ny][nx] == -1000:
                box[ny][nx] = -1
            if ny >= 0 and ny < H and nx >= 0 and nx < W and S[ny][nx] == "." and box[ny][nx] == -1000:
                box[ny][nx] = box[y][x] + 1
                Q.append([ny,nx])
    for i in range(H):
        if len(box[i]) == 0:
            continue
        else:
            ans.append(max(box[i]))
    return


H, W = map(int,input().split())
S = [ [] for _ in range(H)]
for i in range(H):S[i] = list(input())
ans = []
Q = deque()
for i in range(H):
    for j in range(W):
        #各マス目に到達するために必要な移動回数
        box = [[-1000 for _ in range(W)] for __ in range(H)]
        Q.append([i,j])
        box[i][j] = 0
        bfs()
if len(ans)==0:
    print(0)
    exit()
print(max(ans))