Problem:
Score : 400 points

Problem Statement

Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.

The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is #, and a "road" square if S_{ij} is ..

From a road square, you can move to a horizontally or vertically adjacent road square.

You cannot move out of the maze, move to a wall square, or move diagonally.

Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.

Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.

In this situation, find the maximum possible number of moves Aoki has to make.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;

#define EPS (1e-7)

// 4方向への隣接頂点への移動を表すベクトル
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};

int main() {
  // 入力
  int height, width;
  cin >> height >> width;

  vector<string> field(height);
  for (int h = 0; h < height; h++) {
    cin >> field[h];
  }
  ll totalmax = 0;

  for (int h = 0; h < height; h++) {
    for (int w = 0; w < width; w++) {
      if (field[h][w] == '#') {
        continue;
      }
      ll sx = h;
      ll sy = w;

      // 幅優先探索の初期設定

      // 各セルの最短距離(訪れていないところは -1 としておく)
      vector<vector<int>> dist(height, vector<int>(width, -1));
      dist[sx][sy] = 0;  // スタートを 0 にする

      // 「一度見た頂点」のうち「まだ訪れていない頂点」を表すキュー
      queue<pair<int, int>> que;
      que.push(make_pair(sx, sy));  // スタートを push

      // 幅優先探索を実施する
      while (!que.empty()) {
        const pair<int, int> current_pos = que.front();
        que.pop();
        const int x = current_pos.first;
        const int y = current_pos.second;

        // 隣接頂点を探索
        for (int direction = 0; direction < 4; direction++) {
          const int next_x = x + dx[direction];
          const int next_y = y + dy[direction];

          if (next_x < 0 || next_x >= height || next_y < 0 || next_y >= width) {
            // 場外アウト
            continue;
          }
          if (field[next_x][next_y] == '#') {
            // 壁はアウト
            continue;
          }

          if (dist[next_x][next_y] == -1) {
            // まだ見ていない頂点ならpush
            que.push(make_pair(next_x, next_y));
            dist[next_x][next_y] = dist[x][y] + 1;  // 距離を更新
          }
        }
      }

      // 最も数字の大きい最短距離をとる
      ll maxele = 0;
      for (const auto& x : dist) {
        for (const auto& y : x) {
          maxele = max(maxele, (ll)y);
        }
      }
      totalmax = max(maxele, totalmax);
    }
  }
  // 出力
  cout << totalmax << endl;
  return 0;
}

// 下記が入力例
/*
8 8
.#....#G
.#.#....
...#.##.
#.##...#
...###.#
.#.....#
...#.#..
S.......
*/