Buggy python submission (verdict: Time Limit Exceeded):
import collections
H, W = map(int, input().split())
S = [input() for _ in range(H)]

def dfs(sy, sx, gy, gx):
    q = collections.deque()
    q.append((sy, sx))
    dist = [[-1] * W for _ in range(H)]
    dist[sy][sx] = 0
    dx = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while len(q) != 0:
        h, w = q.popleft()
        d = dist[h][w]
        for x, y in dx:
            h2 = h + y
            w2 = w + x
            if h2 >= 0 and h2 < H and w2 >= 0 and w2 < W:
                if S[h2][w2] == "." and dist[h2][w2] == -1:
                    dist[h2][w2] = d+1
                    q.append((h2, w2))
    return dist[gy][gx]
ans = 0
for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if S[gy][gx] == '#' or (sy==gy and sx==sy):
                    continue
                ans = max(ans,dfs(sy,sx,gy,gx))
print(ans)

Fixed submission:
import collections
H, W = map(int, input().split())
S = [input() for _ in range(H)]

def dfs(sy, sx):
    q = collections.deque()
    q.append((sy, sx))
    dist = [[-1] * W for _ in range(H)]
    dist[sy][sx] = 0
    dx = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while len(q) != 0:
        h, w = q.popleft()
        d = dist[h][w]
        for x, y in dx:
            h2 = h + y
            w2 = w + x
            if h2 >= 0 and h2 < H and w2 >= 0 and w2 < W:
                if S[h2][w2] == "." and dist[h2][w2] == -1:
                    dist[h2][w2] = d+1
                    q.append((h2, w2))
    return dist
ans = 0
for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == '#':
            continue
        d = dfs(sy,sx)
        for i in range(H):
            for k in range(W):
                ans = max(ans,d[i][k])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
H, W = map(int, input().split())
S = [input() for _ in range(H)]

def dfs(sy, sx, gy, gx):
    q = collections.deque()
    q.append((sy, sx))
    dist = [[-1] * W for _ in range(H)]
    dist[sy][sx] = 0
    dx = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while len(q) != 0:
        h, w = q.popleft()
        d = dist[h][w]
        for x, y in dx:
            h2 = h + y
            w2 = w + x
            if h2 >= 0 and h2 < H and w2 >= 0 and w2 < W:
                if S[h2][w2] == "." and dist[h2][w2] == -1:
                    dist[h2][w2] = d+1
                    q.append((h2, w2))
    return dist[gy][gx]
ans = 0
for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == '#':
            continue
        for gy in range(H):
            for gx in range(W):
                if S[gy][gx] == '#':
                    continue
                ans = max(ans,dfs(sy,sx,gy,gx))
print(ans)

Fixed submission:
import collections
H, W = map(int, input().split())
S = [input() for _ in range(H)]

def dfs(sy, sx):
    q = collections.deque()
    q.append((sy, sx))
    dist = [[-1] * W for _ in range(H)]
    dist[sy][sx] = 0
    dx = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    while len(q) != 0:
        h, w = q.popleft()
        d = dist[h][w]
        for x, y in dx:
            h2 = h + y
            w2 = w + x
            if h2 >= 0 and h2 < H and w2 >= 0 and w2 < W:
                if S[h2][w2] == "." and dist[h2][w2] == -1:
                    dist[h2][w2] = d+1
                    q.append((h2, w2))
    return dist
ans = 0
for sy in range(H):
    for sx in range(W):
        if S[sy][sx] == '#':
            continue
        d = dfs(sy,sx)
        for i in range(H):
            for k in range(W):
                ans = max(ans,d[i][k])
print(ans)