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<math.h>
#include<stdlib.h>
#include<string.h>
#define max(x,y)((x)>(y)?(x):(y))
#define min(x,y)((x)>(y)?(y):(x))
#define MOD (int)1e9+7
int dx4[]={1,0,-1,0};
int dy4[]={0,-1,0,1};
typedef long long int ll;
typedef unsigned long long int ull;
ll gcd(ll x,ll y){if(!y)return x;return gcd(y,x%y);}
ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
void swap(ll *x,ll *y){ll tmp=*x;*x=*y;*y=tmp;}
int asc_cmp(const void* X,const void* Y){ll 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){ll x=*(int*)X,y=*(int*)Y;if(x<y)return 1;else if(x==y)return 0;else return -1;}

typedef struct data_{
   int x;
   int y;
}data_t;
typedef struct queue_{
    int n,f,t;
    data_t data[200000];
    int (*size)();
    int (*front)();
    int (*tail)();
    void (*enqueue)(data_t);
    data_t (*dequeue)(); 
    void (*init)();
    int (*isEmpty)();
}queue_t;
queue_t queue;
int size(){return queue.n;}
int front(){return queue.f;}
int tail(){return queue.t;}
void enqueue(data_t data){queue.n++;queue.data[queue.t++]=data;}
data_t dequeue(){queue.n--;return queue.data[queue.f++];}
void init(){queue.f=queue.t=queue.n=0;}
int isEmpty(){return queue.n?0:1;}
void setQueue(){
    queue.size=size;
    queue.front=front;
    queue.tail=tail;
    queue.enqueue=enqueue;
    queue.dequeue=dequeue;
    queue.init=init;   
    queue.isEmpty=isEmpty;
}
int h,w;
char s[40][40];
int dist[40][40];
int bfs(int x,int y){
    if(s[y][x]=='#')return -1;
    int max=-1;
    dist[y][x]=0;
    data_t data,now;
    data.x=x;data.y=y;
    queue.enqueue(data);
    while(!queue.isEmpty()){
        now=queue.dequeue();
        for(int i=0;i<4;i++){
            data.x=now.x+dx4[i];data.y=now.y+dy4[i];
            if(s[data.y][data.x]=='.'&&dist[data.y][data.x]==-1&&(0<=data.x&&data.x<w)&&(0<=data.y&&data.y<h)){
                queue.enqueue(data);
                dist[data.y][data.x]=dist[now.y][now.x]+1;
                max=max(max,dist[data.y][data.x]);
            }
        }
    }
    return max;
}

void initdist(){
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            dist[i][j]=-1;
        }
    }
}

int main(){
    setQueue();
    queue.init();

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

    int ans=-1,x;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            initdist();
            x=bfs(j,i);
            ans=max(x,ans);
        }
    }

    printf("%d\n",ans);
}