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: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#define max(x,y)(x>y?x:y)

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

typedef struct{
    int head;
    int tail;
    data_t data[10^5];
}queue_t;


queue_t queue;
int h,w;
char map[20][21];
int dist[20][20];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

void init_queue() {queue.head = queue.tail = 0;}
void enqueue(data_t data) {queue.data[queue.tail++]=data;}
data_t dequeue() {return queue.data[queue.head++];}

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

    /*queue ni atai ga aru aida kurikaesu*/
    while(queue.head!=queue.tail){
        data_t now=dequeue();
        data_t move;

        for(int i=0;i<4;i++){
            /*idousaki ga hannigai ka douka*/
            move.x=now.x+dx[i];
            move.y=now.y+dy[i];
            if(move.y<0||move.x<0||move.y>=h||move.x>=w)continue;
            if(dist[move.y][move.x]!=-1||map[move.y][move.x]=='#')continue;

            /*idousaki tosite mitomeru*/
            enqueue(move);
            /*dist ni kakikomu*/
            dist[move.y][move.x]=dist[now.y][now.x]+1;
        }

        

        /*end ni atai ga attara kaesu*/
        if(dist[end.y][end.x]!=-1) return dist[end.y][end.x];
    }
    return -1;
}


int main(void)
{
    scanf("%d %d",&h,&w);
    for(int i=0;i<h;i++){
        scanf("%s",map[i]);
    }
    int max=INT_MIN;

    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(map[i][j]=='#')continue;
            for(int ii=0;ii<h;ii++){
                for(int jj=0;jj<w;jj++){
                    if(map[ii][jj]=='#') continue;
                    if(i==ii&&j==jj)continue;
                    data_t start,end;
                    start.y=i;
                    start.x=j;
                    end.y=ii;
                    end.x=jj;
                   
                    max=max(bfs(start,end),max);
                }
            }
        }
    }
    printf("%d\n",max);
    return 0;
}