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 <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <iomanip>
//#include <unordered_map>
//#include <unordered_set>
using namespace std;
#define fi first
#define se second
#define ll long long
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int INF = 0x3f3f3f3f;

struct node{
	int x,y,step;
};
struct node2{
	int x,y;
}a[444];
char d[22][22];
int n,m;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};

bool judge(int x,int y){
	if(x>=n||y>=m||x<0||y<0||d[x][y]=='#')
		return false;
	else
		return true;
}

int bfs(int x,int y,int fx,int fy){
	queue<node>q;
	node a;
	a.x=x;
	a.y=y;
	a.step=0;
	q.push(a);
	while(!q.empty()){
		node now=q.front();
		q.pop();
		//printf("%d %d %d\n",now.x,now.y,now.step);
		if(now.x==fx&&now.y==fy)
			return now.step;
		for(int i=0;i<4;i++){
			node ne;
			ne.x=now.x+dx[i];
			ne.y=now.y+dy[i];
			ne.step=now.step+1;
			if(judge(ne.x,ne.y)){
				q.push(ne);
			}
		}
	}
	return 0;
}

int main() {
	cin>>n>>m;
	int k=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>d[i][j];
			if(d[i][j]!='#'){
				a[++k].x=i;
				a[k].y=j;
			}
		}
	}
	int ans=0;
	for(int i=1;i<=k;i++){
		for(int j=1;j<=k;j++){
			if(i==j)continue;
			ans=max(ans,bfs(a[i].x,a[i].y,a[j].x,a[j].y));
		//	printf("%d %d %d %d %d %d %d\n",i,j,a[i].x,a[i].y,a[j].x,a[j].y,bfs(a[i].x,a[i].y,a[j].x,a[j].y));
		}
	}
	cout<<ans<<endl;
}