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>
typedef struct {
    int x;
    int y;
} data_t;
typedef struct {
    int head;
    int tail;
    data_t data[200000];
} queue_t;
queue_t queue;
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 dist[100][100];
int h, w;
char s[100][100];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int bfs(data_t start, data_t end) {
    init_queue();
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            dist[i][j] = -1;
        }
    }
    dist[start.y][start.x] = 0;
    enqueue(start);
    while (queue.tail != queue.head) {
        data_t now = dequeue();
        for (int i = 0; i < 4; i++) {
            data_t move;
            move.x = now.x + dx[i];
            move.y = now.y + dy[i];
            if (move.x < 0 || move.x >= w || move.y >= h || move.y < 0) {
                continue;
            }
            if (s[move.y][move.x] == '#' || dist[move.y][move.x] != -1) {
                continue;
            }

            enqueue(move);
            dist[move.y][move.x] = dist[now.y][now.x] + 1;
        }
        if (dist[end.y][end.x] != -1) {
            return dist[end.y][end.x];
        }
    }
    return -1;
}
int main(void) {
    scanf("%d %d", &h, &w);
    int max = 0;
    for (int i = 0; i < h; i++) {
        scanf("%s", s[i]);
    }

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