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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
typedef pair<int,int> pii;
#define mp make_pair
inline int gi()
{
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=42;
int h,w,ans,d[N][N],vis[N][N];
char a[N][N];
queue<int>qx,qy;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
void bfs(int x,int y)
{
	memset(d,0,sizeof(d));memset(vis,0,sizeof(vis));
	qx.push(x);qy.push(y);vis[x][y]=1;
	while(!qx.empty())
	{
		int nx=qx.front(),ny=qy.front();qx.pop();qy.pop();
		for(int i=0;i<4;i++)
		{
			int xx=nx+dx[i],yy=ny+dy[i];
			if(xx<1||yy<1||xx>h||yy>w||vis[xx][yy]||a[xx][yy]=='#')continue;
			d[xx][yy]=d[nx][ny]+1;qx.push(xx);qy.push(yy);vis[xx][yy]=1;
			ans=max(ans,d[xx][yy]);
		}
	}
}
int main()
{
	h=gi();w=gi();
	for(int i=1;i<=h;i++)scanf("%s",a[i]+1);
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			if(a[i][j])bfs(i,j);
	printf("%d\n",ans);
	return 0;
}