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: Runtime Error)

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
int acs(const void *a, const void *b){return *(int*)a - *(int*)b;} /* 1,2,3,4.. */
int des(const void *a, const void *b){return *(int*)b - *(int*)a;} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b){return *(char*)a - *(char*)b;} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b){return strcmp(*(const char **)a, *(const char **)b);} /* aaa,aab.. */
#define min(a,b) (a < b ? a: b)
#define max(a,b) (a > b ? a: b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 100001
#define INF 1000000007

typedef long long int lli;

typedef struct
{
  int front;
  int rear;
  int data[MAX][3];
} queue;

queue que;

void initQueue(queue *p)
{
  p->front = p->rear = 0;
}

bool emptyQueue(queue *p)
{
  return p->front == p->rear;
}

bool fullQueue(queue *p)
{
  return (p->front - 1 == p->rear) || (p->front == 0 && p->rear == MAX - 1);
}

void queuePop(queue *p)
{
  if (emptyQueue(p))
  {
    printf("UnderFlow\n");
    exit(1);
  }
  else
  {
    p->front++;
    if (p->front == MAX)
      p->front = 0;
  }
}

void queuePush(queue *p, int x, int y, int dist)
{
  if (fullQueue(p))
  {
    printf("OverFlow\n");
    exit(1);
  }
  else
  {
    p->data[p->rear][0] = x;
    p->data[p->rear][1] = y;
    p->data[p->rear][2] = dist;
    p->rear++;
    if (p->rear == MAX)
      p->rear = 0;
  }
}


int main(void) {
  char s[21][21];
  int h, w;
  scanf("%d %d", &h, &w);
  rep(hi, 0, h) {
    scanf("%s", s[hi]);
  }

  int ans = 0;
  rep(hi, 0, h) {
    rep(wi, 0, w) {
      if ( s[hi][wi] == '#' ) continue;
      int visited[21][21] = {0};
      initQueue(&que);
      queuePush(&que, hi, wi, 0);
      while ( !emptyQueue(&que) ) {
        int x    = que.data[que.front][0];
        int y    = que.data[que.front][1];
        int dist = que.data[que.front][2];
        queuePop(&que);
        visited[x][y] = 1;
        ans = max(ans, dist);
        int mx[4] = { 0, 1, 0, -1 };
        int my[4] = { 1, 0, -1, 0 };
        rep(mi, 0, 4) {
          int nextX = x + mx[mi];
          int nextY = y + my[mi];
          if ( nextX < 0 || h <= nextX ) continue;
          if ( nextY < 0 || w <= nextY ) continue;
          if ( visited[nextX][nextY] ) continue;
          if ( s[nextX][nextY] == '#' )  continue;
        
          queuePush(&que, nextX, nextY, dist + 1);
        }
      }
    }
  }

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