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 (Clang 3.8.0) (judge verdict: Runtime Error)

//RE
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <stdbool.h>
//#include <limits.h>
//#include <math.h>

#define max(a,b) (a > b ? a : b)

typedef struct queue{
  int a;
  int b;
  struct queue *next;
} queue;

char s[25][25];
int t[25][25];

void grid_init(int h) {
  for (int i=0; i<h; i++) scanf("%s", s[i]);
  for (int i=0; i<25; i++) {
    for (int j=0; j<25; j++) {
      if (s[i][j] == '.') continue;
      else s[i][j] = '#';
    }
  }
  return ;
}

//first element
queue *queue_init() {
  queue *q = malloc(sizeof(queue));
  q->next = NULL;
  return q;
}

queue *queue_create(int a, int b) {
  queue *q = malloc(sizeof(queue));
  q->a = a;
  q->b = b;
  q->next = NULL;
  return q;
}

void queue_free(queue *q) {
  if (q != NULL) free(q);
  return ;
}

bool queue_empty(queue *q) {
  if (q->next == NULL) return true;
  return false;
}

queue *queue_back(queue *q) {
  if (queue_empty(q)) return q;
  return queue_back(q->next);
}

queue *queue_front(queue *q) {
  if (queue_empty(q)) return NULL;
  return q->next;
}

void queue_pop(queue *q) {
  if (queue_empty(q)) return ;
  queue *qnext = q->next;
  q->next = qnext->next;
  queue_free(qnext);
  return ;
}

void queue_push(queue *q, int a, int b) {
  queue_back(q)->next = queue_create(a, b);
  return ;
}

void bfs_init() {
  for (int i=0; i<25; i++)
    for (int j=0; j<25; j++) t[i][j] = -1;
  return ;
}

void enqueue(queue *q, int a, int b)
{
  if (a > 0 && s[a - 1][b] == '.' && t[a - 1][b] == -1)
  {
    queue_push(q, a - 1, b);
    t[a - 1][b] = t[a][b] + 1;
  }
  if (s[a + 1][b] == '.' && t[a + 1][b] == -1)
  {
    queue_push(q, a + 1, b);
    t[a + 1][b] = t[a][b] + 1;
  }
  if (b > 0 && s[a][b - 1] == '.' && t[a][b - 1] == -1)
  {
    queue_push(q, a, b - 1);
    t[a][b - 1] = t[a][b] + 1;
  }
  if (s[a][b + 1] == '.' && t[a][b + 1] == -1)
  {
    queue_push(q, a, b + 1);
    t[a][b + 1] = t[a][b] + 1;
  }
  return ;
}

void bfs(queue *q ,int a,  int b) {
  enqueue(q, a, b);
  while (!queue_empty(q)) {
    queue *front = queue_front(q);
    queue_pop(q);
    int fa = front->a;
    int fb = front->b;
    enqueue(q, fa, fb);
  }
  return ;
}

int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  grid_init(h);
  /*
  for (int i=0; i<25; i++) {
    for (int j=0; j<25; j++) printf("%c", s[i][j]);
    printf("\n");
  }
  */
  int ans = 0;
  queue *q = queue_init();
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      if (s[i][j] != '.') continue;
      bfs_init();
      t[i][j] = 0;
      bfs(q, i, j);
      for (int k=0; k<h; k++) {
        for (int l=0; l<w; l++) {
          ans = max(ans, t[k][l]);
          //printf("%d ", t[k][l]);
        }
        //printf("\n");
      }
      //printf("\n");
    }
  }
  queue_free(q);
  printf("%d\n", ans);
  return 0;
}