Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
import fractions
from functools import reduce
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

from collections import deque


def bfs(sx, sy, gx, gy, H, W):
    d = [[-1 for i in range(W)] for j in range(H)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0

    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]
            #print(nx, ny)
            if 0 <= nx < W and 0 <= ny < H and maze[ny][nx] != "#" and d[ny][nx] == -1:
                que.append((ny, nx))
                #print(p[0], p[1], ny, nx)
                d[ny][nx] = d[p[0]][p[1]] + 1
    return d[gy][gx]


H, W = na()
ans = 0
maze = [input() for i in range(H)]
fin = []
for sy in range(H):
    for sx in range(W):
        if maze[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if maze[gy][gx] == '#':
                    continue
                if (gx, gy, sx, sy) in fin:
                    continue
                else:
                    #print(sx, sy, gx, gy)
                    fin.append((sx, sy, gx, gy))
                    ans = max(ans, bfs(sx, sy, gx, gy, H, W))
                    #print(ans)
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
s = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]
ans = 0

for y in range(1, H+1):
    for x in range(1, W+1):
        if s[y][x] == '#':
            continue
        visited = [[0]*(W+2) for _ in range(H+2)]
        visited[y][x] = 1
        max_s = 0
        dq = deque([(y, x, 0)])

        while dq:
            cy, cx, steps = dq.popleft()
            if steps > max_s:
                max_s = steps
            steps += 1

            for dy, dx in ((cy+1, cx), (cy-1, cx), (cy, cx+1), (cy, cx-1)):
                if s[dy][dx] == '#' or visited[dy][dx]:
                    continue
                visited[dy][dx] = 1
                dq.append((dy, dx, steps))

        ans = max(ans, max_s)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
import fractions
from functools import reduce
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

from collections import deque


def bfs(sx, sy, gx, gy, H, W):
    d = [[-1 for i in range(W)] for j in range(H)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0

    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]
            #print(nx, ny)
            if 0 <= nx < W and 0 <= ny < H and maze[ny][nx] != "#" and d[ny][nx] == -1:
                que.append((ny, nx))
                #print(p[0], p[1], ny, nx)
                d[ny][nx] = d[p[0]][p[1]] + 1
    return d[gy][gx]


H, W = na()
ans = 0
maze = [input() for i in range(H)]
fin = []
for sy in range(H):
    for sx in range(W):
        if maze[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if maze[gy][gx] == '#':
                    continue
                if (gx, gy, sx, sy) in fin:
                    continue
                else:
                    #print(sx, sy, gx, gy)
                    fin.append((sx, sy, gx, gy))
                    ans = max(ans, bfs(sx, sy, gx, gy, H, W))
                    #print(ans)
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
s = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]
ans = 0

for y in range(1, H+1):
    for x in range(1, W+1):
        if s[y][x] == '#':
            continue
        visited = [[0]*(W+2) for _ in range(H+2)]
        visited[y][x] = 1
        max_s = 0
        dq = deque([(y, x, 0)])

        while dq:
            cy, cx, steps = dq.popleft()
            if steps > max_s:
                max_s = steps
            steps += 1

            for dy, dx in ((cy+1, cx), (cy-1, cx), (cy, cx+1), (cy, cx-1)):
                if s[dy][dx] == '#' or visited[dy][dx]:
                    continue
                visited[dy][dx] = 1
                dq.append((dy, dx, steps))

        ans = max(ans, max_s)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
import fractions
from functools import reduce
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

from collections import deque


def bfs(sx, sy, gx, gy, H, W):
    d = [[-1 for i in range(W)] for j in range(H)]

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sy, sx))
    d[sy][sx] = 0

    while que:
        p = que.popleft()
        if p[0] == gy and p[1] == gx:
            break
        for i in range(4):
            ny = p[0] + dy[i]
            nx = p[1] + dx[i]
            #print(nx, ny)
            if 0 <= nx < W and 0 <= ny < H and maze[ny][nx] != "#" and d[ny][nx] == -1:
                que.append((ny, nx))
                #print(p[0], p[1], ny, nx)
                d[ny][nx] = d[p[0]][p[1]] + 1
    return d[gy][gx]


H, W = na()
ans = 0
maze = [input() for i in range(H)]
for sy in range(H):
    for sx in range(W):
        if maze[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if maze[gy][gx] == '#':
                    continue
                #print(sx, sy, gx, gy)
                ans = max(ans, bfs(sx, sy, gx, gy, H, W))
                #print(ans)
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
s = ['#'*(W+2)] + ['#'+input()+'#' for _ in range(H)] + ['#'*(W+2)]
ans = 0

for y in range(1, H+1):
    for x in range(1, W+1):
        if s[y][x] == '#':
            continue
        visited = [[0]*(W+2) for _ in range(H+2)]
        visited[y][x] = 1
        max_s = 0
        dq = deque([(y, x, 0)])

        while dq:
            cy, cx, steps = dq.popleft()
            if steps > max_s:
                max_s = steps
            steps += 1

            for dy, dx in ((cy+1, cx), (cy-1, cx), (cy, cx+1), (cy, cx-1)):
                if s[dy][dx] == '#' or visited[dy][dx]:
                    continue
                visited[dy][dx] = 1
                dq.append((dy, dx, steps))

        ans = max(ans, max_s)

print(ans)