Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-


def calc_dist(s, start_i, start_j, goal_i, goal_j, h, w):
    from collections import deque

    result = 0
    d = deque()
    d.append((start_i, start_j))
    dist = [[0 for _ in range(w)] for _ in range(h)]
    visited = [[False for _ in range(w)] for _ in range(h)]
    visited[start_i][start_j] = True
    dxy = [(1, 0), (-1, 0), (0, -1), (0, 1)]

    while d:
        hd, wd = d.popleft()

        for dx, dy in dxy:
            nx = wd + dx
            ny = hd + dy

            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue

            if s[ny][nx] == '#':
                continue

            if visited[ny][nx]:
                continue

            visited[ny][nx] = True
            dist[ny][nx] = dist[hd][wd] + 1
            d.append((ny, nx))
            result = max(result, dist[ny][nx])

            if nx == goal_j and ny == goal_i:
                return result

    return result


def main():
    h, w = map(int, input().split())
    s = [list(input()) for _ in range(h)]
    ans = 0

    for start_i in range(h):
        for start_j in range(w):
            for goal_i in range(h):
                for goal_j in range(w):
                    if s[start_i][start_j] == '#':
                        continue

                    if s[goal_i][goal_j] == '#':
                        continue

                    ans = max(ans, calc_dist(s, start_i, start_j, goal_i, goal_j, h, w))

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
# -*- coding: utf-8 -*-


def calc_dist(s, start_i, start_j, h, w):
    from collections import deque

    result = 0
    d = deque()
    d.append((start_i, start_j))
    dist = [[0 for _ in range(w)] for _ in range(h)]
    visited = [[False for _ in range(w)] for _ in range(h)]
    visited[start_i][start_j] = True
    dxy = [(1, 0), (-1, 0), (0, -1), (0, 1)]

    while d:
        hd, wd = d.popleft()

        for dx, dy in dxy:
            nx = wd + dx
            ny = hd + dy

            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue

            if s[ny][nx] == '#':
                continue

            if visited[ny][nx]:
                continue

            visited[ny][nx] = True
            dist[ny][nx] = dist[hd][wd] + 1
            d.append((ny, nx))
            result = max(result, dist[ny][nx])

    return result


def main():
    h, w = map(int, input().split())
    s = [list(input()) for _ in range(h)]
    ans = 0

    # KeyInsight
    # 始点のみを全探索(終点は全探索する必要はない)
    # 距離はBFSで計算
    for start_i in range(h):
        for start_j in range(w):
            if s[start_i][start_j] == '#':
                continue

            ans = max(ans, calc_dist(s, start_i, start_j, h, w))

    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-


def calc_dist(s, start_i, start_j, goal_i, goal_j, h, w):
    from collections import deque

    result = 0
    d = deque()
    d.append((start_i, start_j))
    dist = [[0 for _ in range(w)] for _ in range(h)]
    visited = [[False for _ in range(w)] for _ in range(h)]
    visited[start_i][start_j] = True
    dxy = [(1, 0), (-1, 0), (0, -1), (0, 1)]

    while d:
        hd, wd = d.popleft()

        for dx, dy in dxy:
            nx = wd + dx
            ny = hd + dy

            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue

            if s[ny][nx] == '#':
                continue

            if visited[ny][nx]:
                continue

            visited[ny][nx] = True
            dist[ny][nx] = dist[hd][wd] + 1
            d.append((ny, nx))

            if nx == goal_j and ny == goal_i:
                for i in range(h):
                    for j in range(w):
                        result = max(result, dist[i][j])

                return result

    return result


def main():
    h, w = map(int, input().split())
    s = [list(input()) for _ in range(h)]
    ans = 0

    for start_i in range(h):
        for start_j in range(w):
            for goal_i in range(h):
                for goal_j in range(w):
                    if s[start_i][start_j] == '#':
                        continue

                    if s[goal_i][goal_j] == '#':
                        continue

                    ans = max(ans, calc_dist(s, start_i, start_j, goal_i, goal_j, h, w))

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
# -*- coding: utf-8 -*-


def calc_dist(s, start_i, start_j, h, w):
    from collections import deque

    result = 0
    d = deque()
    d.append((start_i, start_j))
    dist = [[0 for _ in range(w)] for _ in range(h)]
    visited = [[False for _ in range(w)] for _ in range(h)]
    visited[start_i][start_j] = True
    dxy = [(1, 0), (-1, 0), (0, -1), (0, 1)]

    while d:
        hd, wd = d.popleft()

        for dx, dy in dxy:
            nx = wd + dx
            ny = hd + dy

            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue

            if s[ny][nx] == '#':
                continue

            if visited[ny][nx]:
                continue

            visited[ny][nx] = True
            dist[ny][nx] = dist[hd][wd] + 1
            d.append((ny, nx))
            result = max(result, dist[ny][nx])

    return result


def main():
    h, w = map(int, input().split())
    s = [list(input()) for _ in range(h)]
    ans = 0

    # KeyInsight
    # 始点のみを全探索(終点は全探索する必要はない)
    # 距離はBFSで計算
    for start_i in range(h):
        for start_j in range(w):
            if s[start_i][start_j] == '#':
                continue

            ans = max(ans, calc_dist(s, start_i, start_j, h, w))

    print(ans)


if __name__ == '__main__':
    main()