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>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define F first
#define S second
#define nl cout<<"\n"
//cout<<fixed<<setprecision(11);
const ll MOD=1e9+7;
const ll ARR_MAX = 23;
const ll VAL_MAX = 1e5+1;
const ll INF=1e14;

string s[ARR_MAX];
int h,w;

bool ck(int x,int y){
    if(x>=0 && x<h && y>=0 && y<w && s[x][y]=='.'){
        return true;
    }
    return false;
}

void solve(){
    // int h,w;
    cin>>h>>w;
    // string s[h];
    for(int i=0;i<h;i++){
        cin>>s[i];
    }
    int mx=0;
    int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
    for(int i=0;i<h*w;i++){
        // int xi=get_x(i),yi=get_y(i);
        if(s[i/w][i%w]=='#') continue;
        priority_queue<pii> pq;
        pq.push({0,i});
        int n=h*w;
        int vis[n]={};
        int dist[n];
        while(!pq.empty()){
            int d=-pq.top().F;
            int node=pq.top().S;
            pq.pop();
            if(vis[node]){
                dist[node]=min(dist[node],d);
            }
            else{
                dist[node]=d;
            }
            vis[node]=1;
            int x=node/w,y=node%w;
            for(int j=0;j<4;j++){
                int xx=x+dx[j],yy=y+dy[j];
                int nxt=xx*w+yy;
                if(ck(xx,yy) && !vis[nxt]){
                    pq.push({-1*(d+1),nxt});
                }   
            }
        }
        for(int i=0;i<n;i++){
            // cout<<i/h<<" "<<i%h<<" "<<vis[i]<<" "<<dist[i],nl;
            if(vis[i])
                mx=max(mx,dist[i]);
        }
        // return;
        // cout<<i/h<<" "<<i%h<<" "<<mx,nl;
    }
    cout<<mx,nl;
}

int main(){
    fastio();
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}