Buggy python submission (verdict: Time Limit Exceeded):

H, W = map(int, input().split())
edges = [[None for i in range(W)] for i in range(H)]

for i in range(H):
  s = list(map(str, input()))
  for j in range(W):
    edges[i][j] = s[j] 


def find_next(i, j):
  next = []
  if i > 0 and edges[i-1][j] == ".":
    next.append((i-1, j))
  if i < H - 1 and edges[i+1][j] == ".":
    next.append((i+1, j))
  if j > 0 and edges[i][j-1] == ".":
    next.append((i, j-1))
  if j < W - 1 and edges[i][j+1] == ".":
    next.append((i, j+1))
  return next

inf = 9999999999999999999

from collections import deque

def dfs(i, j):
  colors = [[["white"] for i in range(W)] for j in range(H)]
  queue = deque()
  queue.append((i, j))
  distance = [[inf for i in range(W)] for j in range(H)]
  distance[i][j] = 0

  result = -1
  while queue:
    i, j = map(int, queue.popleft())
    colors[i][j] = "black"
    for (x, y) in find_next(i, j):
      if colors[x][y] != "black":
        d = min(distance[i][j] + 1, distance[x][y])
        distance[x][y] = d
        result = max(result, d)
        queue.append((x, y))
  
  return result


import itertools
ans = -1
for i in range(H):
  for j in range(W):
    if edges[i][j] == "#": 
      continue

    ans = max(ans, dfs(i, j))

print(ans)

Fixed submission:

H, W = map(int, input().split())
edges = [[None for i in range(W)] for i in range(H)]

for i in range(H):
  s = list(map(str, input()))
  for j in range(W):
    edges[i][j] = s[j] 


def find_next(i, j):
  next = []
  if i > 0 and edges[i-1][j] == ".":
    next.append((i-1, j))
  if i < H - 1 and edges[i+1][j] == ".":
    next.append((i+1, j))
  if j > 0 and edges[i][j-1] == ".":
    next.append((i, j-1))
  if j < W - 1 and edges[i][j+1] == ".":
    next.append((i, j+1))
  return next

inf = 9999999999999999999

from collections import deque

def dfs(i, j):
  colors = [["white" for i in range(W)] for j in range(H)]
  queue = deque()
  queue.append((i, j))
  distance = [[inf for i in range(W)] for j in range(H)]
  distance[i][j] = 0

  result = -1
  while queue:
    i, j = map(int, queue.popleft())
    colors[i][j] = "black"
    for (x, y) in find_next(i, j):
      if colors[x][y] == "white":
        colors[x][y] = "gray"
        d = min(distance[i][j] + 1, distance[x][y])
        distance[x][y] = d
        result = max(result, d)
        queue.append((x, y))
  return result


ans = -1
for i in range(H):
  for j in range(W):
    if edges[i][j] == "#": 
      continue
    
    ans = max(ans, dfs(i, j))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

H, W = map(int, input().split())
edges = [[None for i in range(W)] for i in range(H)]

for i in range(H):
  s = list(map(str, input()))
  for j in range(W):
    edges[i][j] = s[j] 


def find_next(i, j):
  next = []
  if i > 0 and edges[i-1][j] == ".":
    next.append((i-1, j))
  if i < H - 1 and edges[i+1][j] == ".":
    next.append((i+1, j))
  if j > 0 and edges[i][j-1] == ".":
    next.append((i, j-1))
  if j < W - 1 and edges[i][j+1] == ".":
    next.append((i, j+1))
  return next

inf = 9999999999999999999

from collections import deque

def dfs(i, j):
  colors = [[["white"] for i in range(W)] for j in range(H)]
  queue = deque()
  queue.append((i, j))
  distance = [[inf for i in range(W)] for j in range(H)]
  distance[i][j] = 0

  while queue:
    i, j = map(int, queue.popleft())
    colors[i][j] = "black"
    for (x, y) in find_next(i, j):
      if colors[x][y] != "black":
        distance[x][y] = min(distance[i][j] + 1, distance[x][y])
        queue.append((x, y))
  
  return distance


import itertools
ans = -1
for i in range(H):
  for j in range(W):
    if edges[i][j] == "#": 
      continue

    L = list(itertools.chain.from_iterable(dfs(i, j)))
    m = max(list(filter(lambda x: x != inf, L)))
    ans = max(ans, m)

print(ans)

Fixed submission:

H, W = map(int, input().split())
edges = [[None for i in range(W)] for i in range(H)]

for i in range(H):
  s = list(map(str, input()))
  for j in range(W):
    edges[i][j] = s[j] 


def find_next(i, j):
  next = []
  if i > 0 and edges[i-1][j] == ".":
    next.append((i-1, j))
  if i < H - 1 and edges[i+1][j] == ".":
    next.append((i+1, j))
  if j > 0 and edges[i][j-1] == ".":
    next.append((i, j-1))
  if j < W - 1 and edges[i][j+1] == ".":
    next.append((i, j+1))
  return next

inf = 9999999999999999999

from collections import deque

def dfs(i, j):
  colors = [["white" for i in range(W)] for j in range(H)]
  queue = deque()
  queue.append((i, j))
  distance = [[inf for i in range(W)] for j in range(H)]
  distance[i][j] = 0

  result = -1
  while queue:
    i, j = map(int, queue.popleft())
    colors[i][j] = "black"
    for (x, y) in find_next(i, j):
      if colors[x][y] == "white":
        colors[x][y] = "gray"
        d = min(distance[i][j] + 1, distance[x][y])
        distance[x][y] = d
        result = max(result, d)
        queue.append((x, y))
  return result


ans = -1
for i in range(H):
  for j in range(W):
    if edges[i][j] == "#": 
      continue
    
    ans = max(ans, dfs(i, j))

print(ans)