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>
#define max(x,y) (x>y?x:y)
int dx[]={1,0,-1,0};
int dy[]={0,-1,0,1};
typedef struct{
    int x;
    int y;
}data_t;
typedef struct{
    int head;
    int tail;
    data_t data[200000];
}queue_t;
queue_t queue;
void initqueue(){queue.head=queue.tail=0;}
void enqueue(data_t data){queue.data[queue.tail++]=data;}
data_t dequeue(){return queue.data[queue.head++];}

int h,w;
char map[1000][1000];

int bfs(data_t start,data_t goal){
    int dist[1000][1000];
    data_t now,move;

    initqueue();
    enqueue(start);
    
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            dist[i][j]=-1;
        }
    }
    dist[start.y][start.x]=0;

    int ret=-1;

    while(queue.head!=queue.tail){   
        now=dequeue();
        for(int i=0;i<4;i++){
            move.x=now.x+dx[i];
            move.y=now.y+dy[i];
            if(move.x<0||w<=move.x||move.y<0||h<=move.y)continue;
            if(dist[move.y][move.x]!=-1||map[move.y][move.x]=='#')continue;
            enqueue(move);
            dist[move.y][move.x]=dist[now.y][now.x]+1;
            ret=max(ret,dist[move.y][move.x]);
        }
    }
    return ret;
}
int main(){
    scanf("%d %d",&h,&w);
    for(int i=0;i<h;i++)scanf("%s",map[i]);
    int ans=-1;
    data_t start,end;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(map[i][j]=='#')continue;
            start.x=j;
            start.y=i;
            ans=max(ans,bfs(start,end));
        }
    }
    printf("%d\n",ans);

    return 0;
}