Buggy python submission (verdict: Time Limit Exceeded):
# D
from collections import deque
import numpy as np
INF = float('inf')
h, w = map(int, input().split())
maps = []

for _ in range(h):
    lst = list(input())
    maps.append(lst)
maps = np.array(maps)

def bfs(start, maps, distance, height, width):
    d = deque()
    d.append(start)
    sx, sy = start
    distance[sx,sy] = 0
    
    while d:
        now = d.pop()
        nowx, nowy = now
        
        for i in range(0, 4):
            nx, ny = nowx + [1, 0, -1, 0][i], nowy + [0, 1, 0, -1][i]
            if (0<=nx<height)and(0<=ny<width):
                if (maps[nx,ny] == '.'):
                    if (distance[nx,ny] == -1) or \
                        (distance[nowx,nowy]+1<distance[nx,ny]):
                        d.append((nx, ny))
                        distance[nx,ny] = distance[nowx,nowy] + 1
    #print(distance)
    return distance.max()

cand = []
for sx in range(h):
    for sy in range(w):
        if maps[sx,sy] == '#':
            continue
        distance = np.full((h, w), -1)
        cand.append(bfs((sx, sy), maps, distance, h, w))
print(int(max(cand)))

Fixed submission:
from collections import deque

def bfs(maze, visited, H, W, sx, sy):
    que = deque()
    que.append([sx, sy])
    visited[sx][sy] = 0
    ml = 0
    
    while que:
        x, y = que.popleft()
        
        for px, py in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            nx, ny = x+px, y+py
            if (0<=nx<H)and(0<=ny<W):
                if (maze[nx][ny] == '.')and(visited[nx][ny]==-1):
                    que.append([nx, ny])
                    dist = visited[x][y] + 1
                    visited[nx][ny] = dist
                    ml = max(ml, dist)
    return ml
                    

H, W = map(int, input().split())
maze = [input() for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            continue
        else:
            visited = [[-1]*W for _ in range(H)]
            ans = max(ans, bfs(maze, visited, H, W, i, j))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# D
from collections import deque
INF = float('inf')
h, w = map(int, input().split())
maps = []

for _ in range(h):
    lst = list(input())
    maps.append(lst)

def bfs(start, maps, distance, height, width):
    d = deque()
    d.append(start)
    sx, sy = start
    distance[sx][sy] = 0
    
    while d:
        now = d.pop()
        nowx, nowy = now
        
        for i in range(0, 4):
            nx, ny = nowx + [1, 0, -1, 0][i], nowy + [0, 1, 0, -1][i]
            if (0<=nx<height)and(0<=ny<width):
                if (maps[nx][ny] == '.'):
                    if (distance[nx][ny] == -1) or \
                        (distance[nowx][nowy]+1<distance[nx][ny]):
                        d.append((nx, ny))
                        distance[nx][ny] = distance[nowx][nowy] + 1
    #print(distance)
    max_ = -1
    for row in distance:
        for val in row:
            if val > max_:
                max_ = val
    return max_

cand = []
for sx in range(h):
    for sy in range(w):
        if maps[sx][sy] == '#':
            continue
        distance = [[-1 for _ in range(w)] for _ in range(h)]
        cand.append(bfs((sx, sy), maps, distance, h, w))
print(max(cand))

Fixed submission:
from collections import deque

def bfs(maze, visited, H, W, sx, sy):
    que = deque()
    que.append([sx, sy])
    visited[sx][sy] = 0
    ml = 0
    
    while que:
        x, y = que.popleft()
        
        for px, py in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            nx, ny = x+px, y+py
            if (0<=nx<H)and(0<=ny<W):
                if (maze[nx][ny] == '.')and(visited[nx][ny]==-1):
                    que.append([nx, ny])
                    dist = visited[x][y] + 1
                    visited[nx][ny] = dist
                    ml = max(ml, dist)
    return ml
                    

H, W = map(int, input().split())
maze = [input() for _ in range(H)]
ans = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            continue
        else:
            visited = [[-1]*W for _ in range(H)]
            ans = max(ans, bfs(maze, visited, H, W, i, j))
print(ans)