Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))

todo = deque()
done = set()
ans = 0
for sh in range(H):
    for sw in range(W):
        if S[sh][sw] == "#":
            continue
        s = (sh, sw)
        done.clear()
        todo.append((s, 0))
        while todo:
            p, count = todo.popleft()
            ans = max(ans, count)
            done.add(p)
            ncount = count + 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh = p[0] + dh
                nw = p[1] + dw
                if S[nh][nw] == "#":
                    continue
                np = (nh, nw)
                if np in done:
                    continue
                todo.append((np, ncount))
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))

todo = deque()
done = set()
ans = 0
for sh in range(H):
    for sw in range(W):
        if S[sh][sw] == "#":
            continue
        s = (sh, sw)
        done.clear()
        done.add(s)
        todo.append((s, 0))
        while todo:
            p, count = todo.popleft()
            ncount = count + 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh, nw = p[0] + dh, p[1] + dw
                if S[nh][nw] == "#":
                    continue
                np = (nh, nw)
                if np in done:
                    continue
                done.add(np)
                ans = max(ans, ncount)
                todo.append((np, ncount))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
H, W = map(int, input().split())
ROAD = set()
for h in range(H):
    for w, c in enumerate(sys.stdin.readline()):
        if c == ".":
            ROAD.add((h, w))

todo = deque()
done = set()
ans = 0
for s in ROAD:
    done.clear()
    todo.append((s, 0))
    while todo:
        p, count = todo.popleft()
        ans = max(ans, count)
        done.add(p)
        ncount = count + 1
        for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            np = (p[0] + dh, p[1] + dw)
            if not np in ROAD or np in done:
                continue
            todo.append((np, ncount))
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))

todo = deque()
done = set()
ans = 0
for sh in range(H):
    for sw in range(W):
        if S[sh][sw] == "#":
            continue
        s = (sh, sw)
        done.clear()
        done.add(s)
        todo.append((s, 0))
        while todo:
            p, count = todo.popleft()
            ncount = count + 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh, nw = p[0] + dh, p[1] + dw
                if S[nh][nw] == "#":
                    continue
                np = (nh, nw)
                if np in done:
                    continue
                done.add(np)
                ans = max(ans, ncount)
                todo.append((np, ncount))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
H, W = map(int, input().split())
ROAD = set()
for h in range(H):
    for w, c in enumerate(sys.stdin.readline()):
        if c == ".":
            ROAD.add((h, w))

todo = deque()
done = set()
ans = 0
for s in ROAD:
    done.clear()
    todo.append((s, 0))
    while todo:
        p, count = todo.popleft()
        ans = max(ans, count)
        done.add(p)
        ncount = count + 1
        for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            np = (p[0] + dh, p[1] + dw)
            if not np in ROAD or np in done:
                continue
            todo.append((np, ncount))
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))

todo = deque()
done = set()
ans = 0
for sh in range(H):
    for sw in range(W):
        if S[sh][sw] == "#":
            continue
        s = (sh, sw)
        done.clear()
        done.add(s)
        todo.append((s, 0))
        while todo:
            p, count = todo.popleft()
            ncount = count + 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh, nw = p[0] + dh, p[1] + dw
                if S[nh][nw] == "#":
                    continue
                np = (nh, nw)
                if np in done:
                    continue
                done.add(np)
                ans = max(ans, ncount)
                todo.append((np, ncount))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))
paths = [(h, w) for h in range(H) for w in range(W) if S[h][w] == "."]

ans = 0
todo = deque()
done = [[False] * W for _ in range(H)]
for sh, sw in paths:
    done[sh][sw] = True
    for gh, gw in paths:
        if done[gh][gw]:
            continue
        todo.append((0, sh, sw))
        pathed = [[False] * W for _ in range(H)]
        while todo:
            cnt, h, w = todo.popleft()
            cnt += 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh = h + dh
                nw = w + dw
                if nh == gh and nw == gw:
                    if ans < cnt:
                        ans = cnt
                    todo.clear()
                    break
                elif S[nh][nw] == "." and not pathed[nh][nw]:
                    todo.append((cnt, nh, nw))
            pathed[h][w] = True
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))

todo = deque()
done = set()
ans = 0
for sh in range(H):
    for sw in range(W):
        if S[sh][sw] == "#":
            continue
        s = (sh, sw)
        done.clear()
        done.add(s)
        todo.append((s, 0))
        while todo:
            p, count = todo.popleft()
            ncount = count + 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh, nw = p[0] + dh, p[1] + dw
                if S[nh][nw] == "#":
                    continue
                np = (nh, nw)
                if np in done:
                    continue
                done.add(np)
                ans = max(ans, ncount)
                todo.append((np, ncount))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))
paths = [(h, w) for h in range(H) for w in range(W) if S[h][w] == "."]

ans = 0
todo = deque()
for sh, sw in paths:
    for gh, gw in paths:
        if sh == gh and sw == gw:
            continue
        todo.append((0, sh, sw))
        pathed = [[False] * W for _ in range(H)]
        while todo:
            cnt, h, w = todo.popleft()
            cnt += 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh = h + dh
                nw = w + dw
                if nh == gh and nw == gw:
                    if ans < cnt:
                        ans = cnt
                    todo.clear()
                    break
                elif S[nh][nw] == "." and not pathed[nh][nw]:
                    todo.append((cnt, nh, nw))
            pathed[h][w] = True
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
S = [input() + "#" for _ in range(H)]
S.append("#" * (W + 1))

todo = deque()
done = set()
ans = 0
for sh in range(H):
    for sw in range(W):
        if S[sh][sw] == "#":
            continue
        s = (sh, sw)
        done.clear()
        done.add(s)
        todo.append((s, 0))
        while todo:
            p, count = todo.popleft()
            ncount = count + 1
            for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
                nh, nw = p[0] + dh, p[1] + dw
                if S[nh][nw] == "#":
                    continue
                np = (nh, nw)
                if np in done:
                    continue
                done.add(np)
                ans = max(ans, ncount)
                todo.append((np, ncount))
print(ans)