Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
State = []
for i in range(H):
    State.append(input())
starts = [(i%H,i//H) for i in range(H*W)]
goals = [(i%H,i//H) for i in range(H*W)]
from collections import deque
def min_dist(start,goal):
    visited = [[False for i in range(W)] for j in range(H)]
    dists = [[float('inf') for i in range(W)] for j in range(H)]
    dists[start[0]][start[1]]=0
    diffs = [(0,1),(1,0),(0,-1),(-1,0)]
    Q = deque([start])
    while Q:
        current_pos = Q.pop()
        for diff in diffs:
            next_pos = (current_pos[0]-diff[0],current_pos[1]-diff[1])
            if next_pos[0] < H and next_pos[0] >= 0 and next_pos[1] < W and next_pos[1] >= 0:
                if State[next_pos[0]][next_pos[1]] == '.':
                    dists[next_pos[0]][next_pos[1]] = min(dists[current_pos[0]][current_pos[1]]+1,dists[next_pos[0]][next_pos[1]])
                    if not visited[next_pos[0]][next_pos[1]]:
                        visited[next_pos[0]][next_pos[1]] = True
                        Q.appendleft(next_pos)
    return dists[goal[0]][goal[1]]
max_dist = -100000
for start in starts:
    for goal in goals:
        if State[start[0]][start[1]] == '#' or State[goal[0]][goal[1]] == '#':
            continue
        a = min_dist(start,goal)
        if a != float('inf'):
            if a > max_dist:
                max_dist = a
print(max_dist)


Fixed submission:
H,W = map(int,input().split())
State = []
for i in range(H):
    State.append(input())
starts = [(i%H,i//H) for i in range(H*W)]
goals = [(i%H,i//H) for i in range(H*W)]
from collections import deque
def min_dist(start):
    visited = [[False for i in range(W)] for j in range(H)]
    dists = [[float('inf') for i in range(W)] for j in range(H)]
    dists[start[0]][start[1]]=0
    diffs = [(0,1),(1,0),(0,-1),(-1,0)]
    Q = deque([start])
    while Q:
        current_pos = Q.pop()
        for diff in diffs:
            next_pos = (current_pos[0]-diff[0],current_pos[1]-diff[1])
            if next_pos[0] < H and next_pos[0] >= 0 and next_pos[1] < W and next_pos[1] >= 0:
                if State[next_pos[0]][next_pos[1]] == '.':
                    dists[next_pos[0]][next_pos[1]] = min(dists[current_pos[0]][current_pos[1]]+1,dists[next_pos[0]][next_pos[1]])
                    if not visited[next_pos[0]][next_pos[1]]:
                        visited[next_pos[0]][next_pos[1]] = True
                        Q.appendleft(next_pos)
    return dists
max_dist = -100000
for start in starts:
    if State[start[0]][start[1]] == '.':
        dists = min_dist(start)
        local_max_dist = -1000000
        for i in range(W):
            for j in range(H):
                if dists[j][i] != float('inf'):
                    if local_max_dist < dists[j][i]:
                        local_max_dist = dists[j][i]
        if local_max_dist > max_dist:
            max_dist = local_max_dist
print(max_dist)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
State = []
for i in range(H):
    State.append(input())
starts = [(i%H,i//H) for i in range(H*W)]
goals = [(i%H,i//H) for i in range(H*W)]
from collections import deque
def min_dist(start,goal):
    visited = [[False for i in range(W)] for j in range(H)]
    dists = [[float('inf') for i in range(W)] for j in range(H)]
    dists[start[0]][start[1]]=0
    diffs = [(0,1),(1,0),(0,-1),(-1,0)]
    Q = deque([start])
    while Q:
        current_pos = Q.pop()
        for diff in diffs:
            next_pos = (current_pos[0]-diff[0],current_pos[1]-diff[1])
            if next_pos[0] < H and next_pos[0] >= 0 and next_pos[1] < W and next_pos[1] >= 0:
                if State[next_pos[0]][next_pos[1]] == '.':
                    dists[next_pos[0]][next_pos[1]] = min(dists[current_pos[0]][current_pos[1]]+1,dists[next_pos[0]][next_pos[1]])
                    if not visited[next_pos[0]][next_pos[1]]:
                        visited[next_pos[0]][next_pos[1]] = True
                        Q.appendleft(next_pos)
    return dists[goal[0]][goal[1]]
max_dist = -100000
for start in starts:
    for goal in goals:
        a = min_dist(start,goal)
        if a != float('inf'):
            if a > max_dist:
                max_dist = a
print(max_dist)



Fixed submission:
H,W = map(int,input().split())
State = []
for i in range(H):
    State.append(input())
starts = [(i%H,i//H) for i in range(H*W)]
goals = [(i%H,i//H) for i in range(H*W)]
from collections import deque
def min_dist(start):
    visited = [[False for i in range(W)] for j in range(H)]
    dists = [[float('inf') for i in range(W)] for j in range(H)]
    dists[start[0]][start[1]]=0
    diffs = [(0,1),(1,0),(0,-1),(-1,0)]
    Q = deque([start])
    while Q:
        current_pos = Q.pop()
        for diff in diffs:
            next_pos = (current_pos[0]-diff[0],current_pos[1]-diff[1])
            if next_pos[0] < H and next_pos[0] >= 0 and next_pos[1] < W and next_pos[1] >= 0:
                if State[next_pos[0]][next_pos[1]] == '.':
                    dists[next_pos[0]][next_pos[1]] = min(dists[current_pos[0]][current_pos[1]]+1,dists[next_pos[0]][next_pos[1]])
                    if not visited[next_pos[0]][next_pos[1]]:
                        visited[next_pos[0]][next_pos[1]] = True
                        Q.appendleft(next_pos)
    return dists
max_dist = -100000
for start in starts:
    if State[start[0]][start[1]] == '.':
        dists = min_dist(start)
        local_max_dist = -1000000
        for i in range(W):
            for j in range(H):
                if dists[j][i] != float('inf'):
                    if local_max_dist < dists[j][i]:
                        local_max_dist = dists[j][i]
        if local_max_dist > max_dist:
            max_dist = local_max_dist
print(max_dist)