def clear_maze(sx, sy, gx, gy, maze):

    INF = 100000000

    distance = [[INF for i in range(W)] for j in range(H)]
    #    distance = [[None]*field_x_length]*field_y_length

    def bfs():
        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]

    return bfs()

H,W = (int(x) for x in input().split())
maze = ['']*H
max_distance = 0
for i in range(H):
  maze[i] = list(input())
for sy in range(H):
  for sx in range(W):
    if maze[sy][sx] == '#':
      continue
    for gy in range(H):
      for gx in range(W):
        if (sx == gx and sy == gy) or maze[gy][gx] == '#':
          continue
        tmp_s = maze[sy][sx]
        tmp_g = maze[gy][gx]
        maze[sy][sx] = 'S'
        maze[gy][gx] = 'G'
        
        max_distance = max(max_distance,clear_maze(sx, sy, gx, gy, maze))
        maze[sy][sx] = tmp_s
        maze[gy][gx] = tmp_g
        
print(max_distance)
    

    