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 <bits/stdc++.h>
typedef long long ll;
#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue; 
#define LINF 1e18
#define INF 1e9
using namespace std;

#define fs first
#define sc second

using P = pair<ll,ll>;
using Pll = pair<P,ll>;
using PQ = priority_queue<P,vector<P>,greater<P>>;

const ll MOD = 1e9+7;




int main(){
    ll dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};
    ll ans = 0;
    ll h,w;
    cin >> h >> w;
    char c[h+2][w+2];
    bool is_visited[h+2][w+2];
    rep(i,0,h+2) rep(j,0,w+2) c[i][j] = '#';

    rep(i,1,h+1){
        rep(j,1,w+1){
            cin >> c[i][j];
        }
    }

    rep(i,1,h+1){
        rep(j,1,w+1){
            if(c[i][j] == '.'){
                rep(k,0,h+2) rep(l,0,w+2) is_visited[k][l] = false;
                is_visited[i][j] = true;
                
                queue<Pll> que;
                Pll p;
                p.fs.fs = i;
                p.fs.sc = j;
                p.sc = 0;
                que.push(p);

                while(!que.empty()){
                    Pll v = que.front();
                    que.pop();
                    ans = max(ans,v.sc);

                    rep(k,0,4){
                        if(!is_visited[v.fs.fs + dx[k]][v.fs.sc + dy[k]] && c[v.fs.fs + dx[k]][v.fs.sc + dy[k]] != '#'){
                            p.fs.fs = v.fs.fs + dx[k];
                            p.fs.sc = v.fs.sc + dy[k];
                            p.sc = v.sc + 1;
                            que.push(p);
                            is_visited[v.fs.fs + dx[k]][v.fs.sc + dy[k]] = true;
                        }
                    }
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}