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: Runtime Error)

#include <bits/stdc++.h>

using namespace std;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i)
const int MOD = 1e9 + 7;
typedef long long ll;
const long long INF = 1LL << 60;
using Graph = vector<vector<int>>;

// 座標 corrdinate と深さを保持するための構造体
struct Corr {
  ll y;
  ll x;
  ll depth;
};

ll H, W;             // 迷路の縦，横
ll sh, sw, gh, gw;   // スタート，ゴールの座標
int y, x, depth;     // キューから取り出した座標
vector<string> vec;  // 座標を受け取る配列
queue<Corr> q;
bool seen[510][510];  // seen[h][w] := マス (h, w) が検知済みかどうか

vector<ll> y_vec = {0, 0, -1,
                    +1};  // 進む方向の簡便化，テクニックとして覚えておく．
vector<ll> x_vec = {
    -1, +1, 0, 0};  // ループで回すことで上下左右に移動できるようになっている

void bfs() {
  while (!q.empty()) {
    Corr now = q.front();
    q.pop();  // キューの先頭を取り出す
    y = now.y, x = now.x,
    depth = now.depth;  // 行，列が y, x になっているので注意

    // 上下左右 4 方向を探索．進むことができればキューに格納する．
    for (ll i = 0; i < 4; ++i) {
      Corr next = {y + y_vec[i], x + x_vec[i], depth + 1};
      if (seen[next.y][next.x]) continue;
      if (0 <= next.y && next.y <= H - 1 && 0 <= next.x && next.x <= W - 1 &&
          vec[next.y][next.x] == '.') {
        seen[next.y][next.x] = true;

        q.push(next);
      }
    }
  }
}

int main() {
  // input
  cin >> H >> W;
  vec.resize(H);
  for (ll i = 0; i < H; ++i) cin >> vec[i];

  int sh, sw, gh, gw;
  int ans = 0;

  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      memset(seen, 0, sizeof(seen));  // seen 配列全体を false に初期化
      if (vec[h][w] == '.') sh = h, sw = w;
      // calculation
      Corr start = {sh, sw, 0};  // 初期位置の深さ(depth)は 0
      q.emplace(start);          // 初期位置をキューに格納

      bfs();
      // 結果
      ans = max(depth, ans);
      depth = 0;
    }
  }

  cout << ans << endl;

  return 0;
}