Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
maze = [input() for i in range(h)]
graph = {}

for i, s_i in enumerate(maze):
  for j, s_ij in enumerate(s_i):
    if s_ij == ".":
      graph[i * w + j] = []
      if i > 0 and maze[i - 1][j] == ".":
        graph[i * w + j].append((i - 1) * w + j)
      if j > 0 and maze[i][j - 1] == ".":
        graph[i * w + j].append(i * w + j - 1)
      if j < w - 1 and maze[i][j + 1] == ".":
        graph[i * w + j].append(i * w + j + 1)
      if i < h - 1 and maze[i + 1][j] == ".":
        graph[i * w + j].append((i + 1) * w + j)

ans = 0
for start in graph.keys():
  dist = {}
  c = 0
  dist[start] = c
  hoge = {start}
  for _ in graph.keys():
    c += 1
    fuga = set()
    for h in hoge:
      fuga |= set(graph[h])
    fuga -= set(dist.keys())
    for f in fuga:
      dist[f] = c  
    hoge = fuga
  ans = max(ans, max(dist.values()))
  
print(ans)



Fixed submission:
h, w = map(int, input().split())
maze = [input() for i in range(h)]
graph = {}

for i, s_i in enumerate(maze):
  for j, s_ij in enumerate(s_i):
    if s_ij == ".":
      graph[i * w + j] = []
      if i > 0 and maze[i - 1][j] == ".":
        graph[i * w + j].append((i - 1) * w + j)
      if j > 0 and maze[i][j - 1] == ".":
        graph[i * w + j].append(i * w + j - 1)
      if j < w - 1 and maze[i][j + 1] == ".":
        graph[i * w + j].append(i * w + j + 1)
      if i < h - 1 and maze[i + 1][j] == ".":
        graph[i * w + j].append((i + 1) * w + j)

ans = 0
for start in graph.keys():
  dist = {}
  c = 0
  dist[start] = c
  hoge = {start}
  for _ in graph.keys():
    c += 1
    fuga = set()
    for h in hoge:
      fuga |= set(graph[h])
    fuga -= set(dist.keys())
    for f in fuga:
      dist[f] = c
    hoge = fuga
    if len(dist) == len(graph):
      break
  ans = max(ans, max(dist.values()))
  
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h, w = map(int, input().split())
maze = [input() for i in range(h)]
graph = {}

for i, s_i in enumerate(maze):
  for j, s_ij in enumerate(s_i):
    if s_ij == ".":
      graph[i * w + j] = []
      if i > 0 and maze[i - 1][j] == ".":
        graph[i * w + j].append((i - 1) * w + j)
      if j > 0 and maze[i][j - 1] == ".":
        graph[i * w + j].append(i * w + j - 1)
      if j < w - 1 and maze[i][j + 1] == ".":
        graph[i * w + j].append(i * w + j + 1)
      if i < h - 1 and maze[i + 1][j] == ".":
        graph[i * w + j].append((i + 1) * w + j)


def solve_dist(d, s, c):
  new_place = [n for n in graph[s] if n not in d.keys()]
  for n in new_place :
    d[n] = c + 1
  for n in new_place :
    solve_dist(d, n, d[n])


ans = 0
for start in graph.keys():
  dist = {}
  c = 0
  dist[start] = c
  hoge = {start}
  for _ in graph.keys():
    c += 1
    fuga = set()
    for h in hoge:
      fuga |= set(graph[h])
    fuga -= set(dist.keys())
    for f in fuga:
      dist[f] = c  
    hoge = fuga
    solve_dist(dist, start, 0)
  ans = max(ans, max(dist.values()))
  
print(ans)



Fixed submission:
h, w = map(int, input().split())
maze = [input() for i in range(h)]
graph = {}

for i, s_i in enumerate(maze):
  for j, s_ij in enumerate(s_i):
    if s_ij == ".":
      graph[i * w + j] = []
      if i > 0 and maze[i - 1][j] == ".":
        graph[i * w + j].append((i - 1) * w + j)
      if j > 0 and maze[i][j - 1] == ".":
        graph[i * w + j].append(i * w + j - 1)
      if j < w - 1 and maze[i][j + 1] == ".":
        graph[i * w + j].append(i * w + j + 1)
      if i < h - 1 and maze[i + 1][j] == ".":
        graph[i * w + j].append((i + 1) * w + j)

ans = 0
for start in graph.keys():
  dist = {}
  c = 0
  dist[start] = c
  hoge = {start}
  for _ in graph.keys():
    c += 1
    fuga = set()
    for h in hoge:
      fuga |= set(graph[h])
    fuga -= set(dist.keys())
    for f in fuga:
      dist[f] = c
    hoge = fuga
    if len(dist) == len(graph):
      break
  ans = max(ans, max(dist.values()))
  
print(ans)