Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
import copy

h, w = map(int, input().split())

maze = [list("#"+input()+"#") for _ in range(h)]
res = [i for i in "#"*(w+2)]
maze.insert(0, res)
maze.append(res)




p = []
for y, l in enumerate(maze):
    for x, m in enumerate(l):
        if m == ".":
            p.append([y, x])


ans = []

for c in combinations(p, 2):
    sy, sx = c[0][0], c[0][1]
    gy, gx = c[1][0], c[1][1]

    temp_maze = copy.deepcopy(maze)
    temp_maze[sy][sx] = 0

    loc = [[sy, sx]]

    for k in range(1, (h+2)*(w+2)):
        next_loc = []
        for y, x in loc:
            for i, j in ([1,0], [-1,0], [0,1],[0,-1]):
                    if temp_maze[y+i][x+j] == '.':
                        temp_maze[y+i][x+j] = k
                        next_loc.append([y+i, x+j])
        loc = next_loc
        if [gy, gx] in loc:
            ans.append(k)
            

print(max(ans))

Fixed submission:
from collections import deque

h, w = map(int, input().split())
h += 2; w += 2
maze = "#" * w
for _ in range(h-2):
    maze += "#" + input() + "#"
maze += "#" * w

def bfs(start):
    INF = h*w+1
    dist = [INF] * (h*w)
    dist[start] = 0
    q = deque([start])
    while q:
        cur_loc = q.popleft()
        cur_val = dist[cur_loc]
        for move in (-1,1,w,-w):
            next_loc = cur_loc + move
            if maze[next_loc] == "#":
                continue
            next_val = cur_val + 1
            if next_val >= dist[next_loc]:
                continue
            dist[next_loc] = next_val
            q.append(next_loc)
    return max(x for x in dist if x < INF)

ans = max(bfs(start) for start in range(h*w) if maze[start] != "#")
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
import copy

h, w = map(int, input().split())

maze = [list("#"+input()+"#") for _ in range(h)]
res = [i for i in "#"*(w+2)]
maze.insert(0, res)
maze.append(res)




p = []
for y, l in enumerate(maze):
    for x, m in enumerate(l):
        if m == ".":
            p.append([y, x])


ans = []

for c in combinations(p, 2):
    sy, sx = c[0][0], c[0][1]
    gy, gx = c[1][0], c[1][1]

    temp_maze = copy.deepcopy(maze)
    temp_maze[sy][sx] = 0

    loc = [[sy, sx]]

    for k in range(1, (h+2)*(w+2)):
        next_loc = []
        for y, x in loc:
            for i, j in ([1,0], [-1,0], [0,1],[0,-1]):
                    if temp_maze[y+i][x+j] == '.':
                        temp_maze[y+i][x+j] = k
                        next_loc.append([y+i, x+j])
        loc = next_loc
        if [gy, gx] in loc:
            ans.append(k)
            

print(max(ans))

Fixed submission:
from collections import deque

h, w = map(int, input().split())
h += 2; w += 2
maze = "#" * w
for _ in range(h-2):
    maze += "#" + input() + "#"
maze += "#" * w

def bfs(start):
    INF = h*w+1
    dist = [INF] * (h*w)
    dist[start] = 0
    q = deque([start])
    while q:
        cur_loc = q.popleft()
        cur_val = dist[cur_loc]
        for move in (-1,1,w,-w):
            next_loc = cur_loc + move
            if maze[next_loc] == "#":
                continue
            next_val = cur_val + 1
            if next_val >= dist[next_loc]:
                continue
            dist[next_loc] = next_val
            q.append(next_loc)
    return max(x for x in dist if x < INF)

ans = max(bfs(start) for start in range(h*w) if maze[start] != "#")
print(ans)