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

def BFS(x,y):
    dist = 1
    visited = deque()
    visited.append((x,y))
    destination = deque()
    search = [(1,0),(-1,0),(0,1),(0,-1)]
    for (dx,dy) in search:
        if maze[y+dy][x+dx] == ".":
            destination.append((x+dx,y+dy))
    same_dist = len(destination)
    moved = 0
    x = destination[0][0]
    y = destination[0][1]
    while destination:
        visited.append((x,y))
        destination.popleft()
        for (dx,dy) in search:
            if maze[y+dy][x+dx] == ".":
                if (x+dx,y+dy) not in destination and (x+dx,y+dy) not in visited:
                    destination.append((x+dx,y+dy))
        if len(destination) >= 1:
            x = destination[0][0]
            y = destination[0][1]
            moved += 1
            if moved == same_dist:
                dist += 1
                moved = 0
                same_dist = len(destination)
    return dist

H,W = map(int,input().split())
maze = [list("#"*(W+2))] + [list("#" + input() + "#") for i in range(H)] + [list("#"*(W+2))]
ans = 0

for y in range(1,H+1):
    for x in range(1,W+1):
        if maze[y][x] == ".":
            ans = max(ans,BFS(x,y))

print(ans)

Fixed submission:
from collections import deque

def BFS(x,y):
    dist = 1
    visited = deque()
    visited.append((x,y))
    destination = deque()
    search = [(1,0),(-1,0),(0,1),(0,-1)]
    for (dx,dy) in search:
        if maze[y+dy][x+dx] == ".":
            destination.append((x+dx,y+dy))
    same_dist = len(destination)
    moved = 0
    x = destination[0][0]
    y = destination[0][1]
    while destination:
        visited.append((x,y))
        destination.popleft()
        for (dx,dy) in search:
            if maze[y+dy][x+dx] == ".":
                if (x+dx,y+dy) not in destination and (x+dx,y+dy) not in visited:
                    destination.append((x+dx,y+dy))
        if len(destination) >= 1:
            x = destination[0][0]
            y = destination[0][1]
            moved += 1
            if moved == same_dist:
                dist += 1
                moved = 0
                same_dist = len(destination)
    return dist

H,W = map(int,input().split())
maze = [list("#"*(W+2))] + [list("#" + input() + "#") for i in range(H)] + [list("#"*(W+2))]
ans = 0

for y in range(1,H+1):
    for x in range(1,W+1):
        if maze[y][x] == ".":
            ans = max(ans,BFS(x,y))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
maze = []
for i in range(H):
    s = list(input())
    maze.append(s)

def clear_maze(sx, sy, gx, gy, maze):
    INF = 100000000
    distance = [[INF for i in range(W)] for j in range(H)]
    #    distance = [[None]*field_x_length]*field_y_length

    def bfs():
        queue = []
        queue.insert(0, (sx, sy))
        distance[sx][sy] = 0

        while len(queue):
            x, y = queue.pop()

            if x == gx and y == gy:
                break

            for i in range(0, 4):
                nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]

                if 0 <= nx and nx < H and 0 <= ny and ny < W and maze[nx][ny] != '#' and distance[nx][ny] == INF:
                        queue.insert(0, (nx, ny))
                        distance[nx][ny] = distance[x][y] + 1


        return distance[gx][gy]

    return bfs()

ans = 0
for sx in range(H):
    for sy in range(W):
        if maze[sx][sy] == ".":
            for gx in range(H):
                for gy in range(W):
                    if maze[gx][gy] == ".":
                        if clear_maze(sx,sy,gx,gy,maze) > ans:
                            ans = clear_maze(sx,sy,gx,gy,maze)

print(ans)

Fixed submission:
from collections import deque

def BFS(x,y):
    dist = 1
    visited = deque()
    visited.append((x,y))
    destination = deque()
    search = [(1,0),(-1,0),(0,1),(0,-1)]
    for (dx,dy) in search:
        if maze[y+dy][x+dx] == ".":
            destination.append((x+dx,y+dy))
    same_dist = len(destination)
    moved = 0
    x = destination[0][0]
    y = destination[0][1]
    while destination:
        visited.append((x,y))
        destination.popleft()
        for (dx,dy) in search:
            if maze[y+dy][x+dx] == ".":
                if (x+dx,y+dy) not in destination and (x+dx,y+dy) not in visited:
                    destination.append((x+dx,y+dy))
        if len(destination) >= 1:
            x = destination[0][0]
            y = destination[0][1]
            moved += 1
            if moved == same_dist:
                dist += 1
                moved = 0
                same_dist = len(destination)
    return dist

H,W = map(int,input().split())
maze = [list("#"*(W+2))] + [list("#" + input() + "#") for i in range(H)] + [list("#"*(W+2))]
ans = 0

for y in range(1,H+1):
    for x in range(1,W+1):
        if maze[y][x] == ".":
            ans = max(ans,BFS(x,y))

print(ans)