Buggy python submission (verdict: Time Limit Exceeded):
import queue
import copy

def replace(s, n, c):
    if n < 0 or n >= len(s):
        return s
    return s[:n]+c[0]+s[n+1:]

h, w = map(int, input().split())
l = [input() for _ in range(h)]

def dist(m, x1, y1, x2, y2):
    q = queue.Queue()
    q.put((x1, y1, 0))
    checked=[[False for _ in range(w)] for __ in range(h)]
    while not q.empty():
        x, y, count = q.get()
        if (x, y) == (x2, y2):
            return count
        if x > 0 and m[y][x-1] == "." and checked[y][x-1] == False:
            checked[y][x-1] = True
            q.put((x-1, y, count+1))
        if y > 0 and m[y-1][x] == "." and checked[y-1][x] == False:
            checked[y-1][x] = True
            q.put((x, y-1, count+1))
        if x < w-1 and m[y][x+1] == "." and checked[y][x+1] == False:
            checked[y][x+1] = True
            q.put((x+1, y, count+1))
        if y < h-1 and m[y+1][x] == "." and checked[y+1][x] == False:
            checked[y+1][x] = True
            q.put((x, y+1, count+1))

ans = 0
for i in range(h):
    for j in range(w):
        for _i in range(h):
            for _j in range(w):
                if l[i][j] == "." and l[_i][_j] == ".":
                    ans = max(ans, dist(l, j, i, _j, _i))
print(ans)

Fixed submission:
import queue
import copy

h, w = map(int, input().split())
l = [input() for _ in range(h)]

def dist(m, x1, y1):
    q = queue.Queue()
    q.put((x1, y1, 0))
    checked=[[False for _ in range(w)] for __ in range(h)]
    checked[y1][x1] = True
    while not q.empty():
        x, y, count = q.get()
        if x > 0 and m[y][x-1] == "." and checked[y][x-1] == False:
            checked[y][x-1] = True
            q.put((x-1, y, count+1))
        if y > 0 and m[y-1][x] == "." and checked[y-1][x] == False:
            checked[y-1][x] = True
            q.put((x, y-1, count+1))
        if x < w-1 and m[y][x+1] == "." and checked[y][x+1] == False:
            checked[y][x+1] = True
            q.put((x+1, y, count+1))
        if y < h-1 and m[y+1][x] == "." and checked[y+1][x] == False:
            checked[y+1][x] = True
            q.put((x, y+1, count+1))
    return count

ans = 0
for i in range(h):
    for j in range(w):
        if l[i][j] == ".":
            ans = max(ans, dist(l, j, i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import queue
import copy

def replace(s, n, c):
    if n < 0 or n >= len(s):
        return s
    return s[:n]+c[0]+s[n+1:]

h, w = map(int, input().split())
l = [input() for _ in range(h)]

def dist(m, x1, y1, x2, y2):
    q = queue.Queue()
    q.put((x1, y1, 0))
    while not q.empty():
        x, y, count = q.get()
        if (x, y) == (x2, y2):
            return count
        if x > 0 and m[y][x-1] == ".":
            m[y] = replace(m[y], x-1, "#")
            q.put((x-1, y, count+1))
        if y > 0 and m[y-1][x] == ".":
            m[y-1] = replace(m[y-1], x, "#")
            q.put((x, y-1, count+1))
        if x < w-1 and m[y][x+1] == ".":
            m[y] = replace(m[y], x+1, "#")
            q.put((x+1, y, count+1))
        if y < h-1 and m[y+1][x] == ".":
            m[y+1] = replace(m[y+1], x, "#")
            q.put((x, y+1, count+1))

ans = 0
for i in range(h):
    for j in range(w):
        for _i in range(h):
            for _j in range(w):
                if l[i][j] == "." and l[_i][_j] == ".":
                    ans = max(ans, dist(copy.copy(l), j, i, _j, _i))
print(ans)

Fixed submission:
import queue
import copy

h, w = map(int, input().split())
l = [input() for _ in range(h)]

def dist(m, x1, y1):
    q = queue.Queue()
    q.put((x1, y1, 0))
    checked=[[False for _ in range(w)] for __ in range(h)]
    checked[y1][x1] = True
    while not q.empty():
        x, y, count = q.get()
        if x > 0 and m[y][x-1] == "." and checked[y][x-1] == False:
            checked[y][x-1] = True
            q.put((x-1, y, count+1))
        if y > 0 and m[y-1][x] == "." and checked[y-1][x] == False:
            checked[y-1][x] = True
            q.put((x, y-1, count+1))
        if x < w-1 and m[y][x+1] == "." and checked[y][x+1] == False:
            checked[y][x+1] = True
            q.put((x+1, y, count+1))
        if y < h-1 and m[y+1][x] == "." and checked[y+1][x] == False:
            checked[y+1][x] = True
            q.put((x, y+1, count+1))
    return count

ans = 0
for i in range(h):
    for j in range(w):
        if l[i][j] == ".":
            ans = max(ans, dist(l, j, i))
print(ans)