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: Time Limit Exceeded)

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
	int h, w;
    cin >> h >> w;
    vector<string> map(h);
    vector<vector<int>> itta(h, vector<int>(w));
    vector<vector<int>> hou(4, vector<int>(2));
    hou = {{0,1},{-1,0},{0,-1},{1,0}};
    rep(i, h) cin >> map.at(i);
    int x0, y0, x1, y1;
    int cou = 0;
    int max = 0;
    int zx = 0;
    int zy = 0;
    for(int i=0; i<h*w-1; i++){
      for(int j=i+1; j<h*w; j++){
        y0 = i/w;
        x0 = i%w;
        y1 = j/w;
        x1 = j%w;
        if(map.at(y0).at(x0) == '#' || map.at(y1).at(x1) == '#') continue;
        rep(i0, h){
          rep(j0, w){
            itta.at(i0).at(j0) = 0;
          }
        }
        itta.at(y0).at(x0) = 1;
        cou = 1;
        int f = 0;
        while(itta.at(y1).at(x1) == 0){
          f = 0;
          cou++;
          rep(i0, h){
            rep(j0, w){
              if(itta.at(i0).at(j0) == cou-1){
                rep(k, 4){
                  zx = j0+hou.at(k).at(0);
                  zy = i0+hou.at(k).at(1);
                  if(!(zx<0 || zx>=w || zy<0 || zy>=h)){
                    if(map.at(zy).at(zx) == '.' && itta.at(zy).at(zx) == 0){
                      itta.at(zy).at(zx) = cou;
                      f = 1;
                    }
                  }
                }
              }
            }
          }
          if(f == 0) break;
        }
        if(max < cou && f == 1) max = cou;
      }
    }
    cout << max-1 << endl;
}