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;
#define ll long long int
#define MOD 1000000007
#define pb push_back
#define vl vector<ll>
#define pll pair<ll,ll>
#define mp make_pair
using namespace std; 
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll n,m;
void dfs1(pll st,ll vis[25][25],string grid[],ll dist[25][25])
{
	ll i = st.first;
	ll j = st.second;
	//vis[i][j]=1;
	queue<pll>q;
	q.push(st);
	while(!q.empty())
	{
		pll th = q.front();
		q.pop();
		ll i = th.first;
		ll j = th.second;
		vis[i][j]=1;
		if(i && grid[i-1][j]=='.'  && vis[i-1][j]==0)
		{
			dist[i-1][j]=dist[i][j]+1;
			q.push(mp(i-1,j));
		}
		if(j && grid[i][j-1]=='.'  && vis[i][j-1]==0)
		{
			dist[i][j-1]=dist[i][j]+1;
			q.push(mp(i,j-1));
		}
		if(j<m-1 && grid[i][j+1]=='.'  && vis[i][j+1]==0)
		{
			dist[i][j+1]=dist[i][j]+1;
			q.push(mp(i,j+1));
		}
		if(i<n-1 && grid[i+1][j]=='.'  && vis[i+1][j]==0)
		{
			dist[i+1][j]=dist[i][j]+1;
			q.push(mp(i+1,j));
		}			
	}
}
int main()
{
	fast
	cin>>n>>m;
	string grid[25];
	for (int i = 0; i < n; ++i)
	{
		cin>>grid[i];
	}
	ll dist[25][25];
	ll vis[25][25];
	ll ans=0;
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < m; ++j)
		{
			memset(vis,0,sizeof(vis));
			memset(dist,-1,sizeof(dist));
			if(grid[i][j]=='.')
			{	
				dist[i][j]=0;
				dfs1(mp(i,j),vis,grid,dist);
				ll ma = 0;
				for (int i1 = 0; i1 < n; ++i1)
				{
					for (int j1 = 0; j1 < m; ++j1)
					{
		//				cout<<dist[i1][j1]<<" ";
						ma = max(dist[i1][j1],ma);
					}
		//			cout<<endl;
				}
		//		cout<<i<<" "<<j<<" sdsds"<<endl;
				ans=max(ans,ma);
			}
		}

	}
	cout<<ans<<endl;

}