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 (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
typedef long long int ll;
typedef unsigned long long int ull;
int asc_cmp(const void* X,const void* Y){int x=*(int*)X,y=*(int*)Y;if(x<y)return -1;else if(x==y)return 0;else return 1;}
int desc_cmp(const void* X,const void* Y){int x=*(int*)X,y=*(int*)Y;if(x<y)return 1;else if(x==y)return 0;else return -1;}

int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int h,w;
char map[100][100];


typedef struct{
	int x;
	int y;
}queue_t;

int max(int x,int y){return x>y?x:y;}
int search(int start_x,int start_y,int end_x,int end_y){
    int dist[100][100]={{}};
	int now_x=start_x,now_y=start_y;
	int flg=0;
	int ret;

	queue_t queue[200000];
	int front=0,back=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			dist[i][j]=-1;
		}
	}
	queue[back].x=start_x;
	queue[back++].y=start_y;
	dist[start_y][start_x]=0;
	while(front!=back){
		flg=0;
		for(int i=0;i<4;i++){
			now_x=queue[front].x+dx[i];
			now_y=queue[front].y+dy[i];
			if(now_x<0||w<=now_x||now_y<0||h<=now_y)continue;
			if(dist[now_y][now_x]!=-1||map[now_y][now_x]=='#')continue;
			queue[back].x=now_x;
			queue[back++].y=now_y;
			flg=1;
			dist[now_y][now_x]=dist[queue[front].y][queue[front].x]+1;
		}
		front++;
		if(!flg){
			//return -1;
		}
		if(dist[end_y][end_x]!=-1){
			//printf("==>%d\n",dist[end_y][end_x]);
			ret=dist[end_y][end_x];
			return ret;
		}
	}
}

int main(){
    scanf("%d %d",&h,&w);
    for(int i=0;i<h;i++)scanf("%s",map[i]);
    
    int cnt;
	int dist[200][200];
	int ans=0;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            for(int ii=0;ii<h;ii++){
                for(int jj=0;jj<w;jj++){
                    if(i==ii&&j==jj)continue;
					if(map[i][j]=='#'||map[ii][jj]=='#')continue;
					//printf("start x=%d y=%d end x=%d y=%d\n",j,i,jj,ii);
					cnt=search(j,i,jj,ii);
					//printf("=>%d\n",cnt);
					if(cnt>ans)ans=cnt;
                }	
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}