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

H, W = map(int, input().split())
C = [['#' for j in range(W + 2)] for i in range(H + 2)]

for i in range(H):
    s = input()
    for j in range(W):
        C[i + 1][j + 1] = s[j]
ans = 0

SS = set([])
for sy in range(1, H + 1):
    for sx in range(1, W + 1):
        for gy in range(1, H + 1):
            for gx in range(1, W + 1):
                if (sy,sx, gy, gx) in SS:
                    continue
                else:
                    SS.add((gy, gx, sy, sx))
                q = deque()
                q.append((sy, sx, 0))
                S = set([])
                r = 0
                while len(q) > 0:
                    t = q.popleft()
                    y, x, d = t[0], t[1], t[2]
                    if C[y][x] == '#':
                        continue
                    if (y, x) in S:
                        continue
                    else:
                        S.add((y, x))

                    if (y, x) == (gy, gx):
                        r = d
                        break
                    q.append((y + 1, x , d + 1))
                    q.append((y - 1, x, d + 1))
                    q.append((y, x + 1, d + 1))
                    q.append((y, x - 1, d + 1))

                ans = max(ans, r)


print(ans)




Fixed submission:
from collections import deque

H, W = map(int, input().split())
C = [['#' for j in range(W + 2)] for i in range(H + 2)]

for i in range(H):
    s = input()
    for j in range(W):
        C[i + 1][j + 1] = s[j]
ans = 0

for sy in range(1, H + 1):
    for sx in range(1, W + 1):

        q = deque()
        q.append((sy, sx, 0))
        S = set([])
        while len(q) > 0:
            t = q.popleft()
            y, x, d = t[0], t[1], t[2]
            ans = max(d, ans)
            if C[y][x] == '#':
                continue
            if (y, x) in S:
                continue
            else:
                S.add((y, x))

            q.append((y + 1, x , d + 1))
            q.append((y - 1, x, d + 1))
            q.append((y, x + 1, d + 1))
            q.append((y, x - 1, d + 1))




print(ans - 1)

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

H, W = map(int, input().split())
C = [['#' for j in range(W + 2)] for i in range(H + 2)]

for i in range(H):
    s = input()
    for j in range(W):
        C[i + 1][j + 1] = s[j]
ans = 0
for sy in range(1, H + 1):
    for sx in range(1, W + 1):
        for gy in range(1, H + 1):
            for gx in range(1, W + 1):
                q = deque()
                q.append((sy, sx, 0))
                S = set([])
                r = 0
                while len(q) > 0:
                    t = q.popleft()
                    y, x, d = t[0], t[1], t[2]
                    if C[y][x] == '#':
                        continue
                    if (y, x) in S:
                        continue
                    else:
                        S.add((y, x))

                    if (y, x) == (gy, gx):
                        r = d
                        break
                    q.append((y + 1, x , d + 1))
                    q.append((y - 1, x, d + 1))
                    q.append((y, x + 1, d + 1))
                    q.append((y, x - 1, d + 1))

                ans = max(ans, r)


print(ans)




Fixed submission:
from collections import deque

H, W = map(int, input().split())
C = [['#' for j in range(W + 2)] for i in range(H + 2)]

for i in range(H):
    s = input()
    for j in range(W):
        C[i + 1][j + 1] = s[j]
ans = 0

for sy in range(1, H + 1):
    for sx in range(1, W + 1):

        q = deque()
        q.append((sy, sx, 0))
        S = set([])
        while len(q) > 0:
            t = q.popleft()
            y, x, d = t[0], t[1], t[2]
            ans = max(d, ans)
            if C[y][x] == '#':
                continue
            if (y, x) in S:
                continue
            else:
                S.add((y, x))

            q.append((y + 1, x , d + 1))
            q.append((y - 1, x, d + 1))
            q.append((y, x + 1, d + 1))
            q.append((y, x - 1, d + 1))




print(ans - 1)