def bfs(sx, sy, gx, gy, maze):
  INF = 10000
  distance = [[INF for i in range(W)] for j in range(H)]
  queue = []
  queue.insert(0, (sx, sy))
  distance[sy][sx] = 0
  while len(queue):
    x, y = queue.pop()
    if x == gx and y == gy:
      break
    for i in range(0, 4):
      nx, ny = x + [1, 0, -1, 0][i], y + [0, 1, 0, -1][i]
      if (0 <= nx and nx < W and 0 <= ny and ny < H and maze[ny][nx] != '#' and distance[ny][nx] == INF):
        queue.insert(0, (nx, ny))
        distance[ny][nx] = distance[y][x] + 1
  return distance[gy][gx]

H,W = (int(x) for x in input().split())
maze = ['']*H
max_distance = 0
maze = [list(input()) for _ in range(H)]

dic = {}
for i in range(H*W):
  dic[i] = [i % W,i//W,0]
  
for i in range(H*W):
  sx,sy = dic[i][0],dic[i][1]
  s = maze[sy][sx]
  if s == '#':
    continue
  for j in range(H*W):
    gx,gy = dic[j][0],dic[j][1]
    g = maze[gy][gx]
    if i == j or g == '#' or dic[j][2] == 1:
        continue
    tmp_s = s
    tmp_g = g
    maze[sy][sx] = 'S'
    maze[gy][gx] = 'G'
    max_distance = max(max_distance,bfs(sx, sy, gx, gy, maze))
    maze[sy][sx] = tmp_s
    maze[gy][gx] = tmp_g
  dic[i][2] = 1
        
print(max_distance)    

    