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: Runtime Error)

#include<iostream>
using namespace std;
#include<queue>
bool used[50][50];
string s[50];
int r,c,sx,sy,gx,gy;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int d[50][50];
int bfs(int x,int y)
{
	queue<pair<int,int> >P;
	d[x][y]=0;
	P.push(make_pair(x,y));
	while(!P.empty())
	{
		pair<int,int>p=P.front();P.pop();
		int nx=p.first,ny=p.second;
		used[nx][ny]=true;
		for(int r=0;r<4;r++)
		{
			int tx=nx+dx[r],ty=ny+dy[r];
			if(s[tx][ty]=='#'||used[tx][ty])continue;
			d[tx][ty]=d[nx][ny]+1;used[tx][ty]=true;
			P.push(make_pair(tx,ty));
		}
	}
	return d[gx-1][gy-1];
}
main()
{
	int ans=400,temp;
	cin>>r>>c;
	for(int i=0;i<r;i++)cin>>s[i];
	
	for(int i=0;i<r;i++){
		gx = i;
		for(int j=0;j<c;j++){
			gy = j;
			for(int k=i;k<r;k++){
				for(int l=j;l<c;l++){
					if(i!=k || j!=0){
						if(d[i][j] != '#' && d[k][l] != '#'){
							temp = bfs(k,l);
							if(ans > temp ) ans = temp;
						}
					}
				}
			}
		}
	}
			
			
	cout<<ans<<endl;
}