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: Wrong Answer)

#include "bits/stdc++.h"
 
using namespace std;
 
#define sp(x) cout<<setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX*0.99
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define yes "Yes"
#define no "No"
#define divs 1000000007
#define dive 1000000009
#define pi 3.141592653589793238
 
typedef pair<int, int> P;

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

int N, M;  // 迷路の縦，横
int s_y, s_x, g_y, g_x;  // スタート，ゴールの座標
int y, x, depth;  // キューから取り出した座標
vector<string> vec;  // 座標を受け取る配列
queue<Corr> q;
bool seen[21][21];

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

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

        // 停止条件：現在の座標がゴールなら深さを返す
        if (y == g_y && x == g_x) return depth;

        // 上下左右 4 方向を探索．進むことができればキューに格納する．
        REP(i,4) {
            Corr next = {y + y_vec[i], x + x_vec[i], depth + 1};
            if (0 <= next.y && next.y <= N-1 && 0 <= next.x && next.x <= M-1 && seen[next.y][next.x] == false) {
                seen[next.y][next.x] = true;  // 同じ場所を探索しないようにする
                q.push(next);
            }
        }
    }
    return 0;
}

signed main(){
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
    // Failed to predict input format
    cin>>N>>M;
    vector<string> s(N);
    REP(i,N) cin>>s[i];
    int ans = 0;
    REP(i,N){
        REP(j,M){
            REP(k,N){
                REP(l,M){
                    s_x=i; s_y=j; g_x=k; g_y=l;
                    Corr start = {s_y, s_x, 0};  // 初期位置の深さ(depth)は 0
                    q.emplace(start);// 初期位置をキューに格納
                    memset(seen, 0, sizeof(seen));
                    seen[i][j] = true;   
                    ans = max(bfs(),ans);
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}