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)

/*
    STARK_BOY
*/
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repe(i,a,b) for(ll i=a;i<=b;i++)
#define bac(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long ll;
typedef vector< ll > vi;
typedef vector<pair<ll,ll> > vpii;
typedef pair<ll,ll> pii;
#define all(c) c.begin(),c.end()
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define setbitc(x) __builtin_popcount(x)
#define init(x,a) memset(x,a,sizeof(x))
#define PI           3.14159265358979323846  /* pi */
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define N 1000005
//Variables Start
ll inf=1e18,MOD=1e9+7;
ll n,m,k,ar[N],br[N],vis[20][20];
string s[20],s1;
//Variables End
void in()
{
	rep(i,0,n)rep(j,0,m)vis[i][j]=inf;
}
bool check(ll r,ll c)
{
	if(r>=0 && r<n && c>=0 && c<m && s[r][c]!='#')return true;
	else return false;
}
ll move(int r, int c, int tr, int tc)
{
	if(s[r][c]=='#'||s[tr][tc]=='#')return 0;
	in();
	queue<pii> q;
	q.push(mp(r,c));
	vis[r][c]=0;
	while(!q.empty())
	{
		pii cur=q.front();
		q.pop();
		if(cur.F==tr && cur.S==tc)break;
		//cout<<cur.F<<" "<<cur.S<<" "<<vis[cur.F][cur.S]<<endl;
		if(check(cur.F,cur.S+1))
		{
			q.push(mp(cur.F,cur.S+1));
			vis[cur.F][cur.S+1]=min(vis[cur.F][cur.S+1],vis[cur.F][cur.S]+1);
		}
		if(check(cur.F,cur.S-1))
		{
			q.push(mp(cur.F,cur.S-1));
			vis[cur.F][cur.S-1]=min(vis[cur.F][cur.S-1],vis[cur.F][cur.S]+1);
		}
		if(check(cur.F+1,cur.S))
		{
			q.push(mp(cur.F+1,cur.S));
			vis[cur.F+1][cur.S]=min(vis[cur.F+1][cur.S],vis[cur.F][cur.S]+1);
		}
		if(check(cur.F-1,cur.S))
		{
			q.push(mp(cur.F-1,cur.S));
			vis[cur.F-1][cur.S]=min(vis[cur.F-1][cur.S],vis[cur.F][cur.S]+1);
		}

	}
	//cout<<"----"<<endl;
	if(vis[tr][tc]>=inf)return 0;
	else return vis[tr][tc];
}
int main()
{
    ios
    int ti=1;
    //cin>>ti;
    while(ti--)
    {
		cin>>n>>m;
		ll ans=0;
		rep(i,0,n)cin>>s[i];
		rep(i,0,n)
			rep(j,0,m)
				rep(ii,0,n)
					rep(jj,0,m)
						ans=max(ans,move(i,j,ii,jj));
		cout<<ans<<endl;
    }
    return 0;
        
}