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>
int h, w;
char s[22][22];
int shortest(int st, int gl)
{
    int v[410] = {};
    int q[410] = {};
    int head = 0, tail = 0;

    q[tail++] = st;
    v[st] = 1;

    while (v[gl] == 0)
    {
        int now = q[head++];
        int next;
        for (int j = 0; j < 4; j++)
        {
            next = now;
            switch (j)
            {
            case 0:
                if (next >= w)
                {
                    next -= w;
                }
                break;
            case 1:
                if (next % w != w - 1)
                {
                    next += 1;
                }
                break;
            case 2:
                if (next < w * (h - 1))
                {
                    next += w;
                }
                break;
            case 3:
                if (next % w != 0)
                {
                    next -= 1;
                }
                break;
            default:
                break;
            }
            if (next == now || next < 0 || next >= h * w)
            {
                continue;
            }
            if (s[next / w][next % w] == '.' && v[next] == 0)
            {
                q[tail++] = next;
                v[next] = v[now] + 1;
            }
        }
    }
    //printf("%d,%d  %d\n", st, gl, v[gl] - 1);
    return v[gl] - 1;
}
int main()
{
    int ans = 0;
    int t;
    scanf("%d %d", &h, &w);
    for (int i = 0; i < h; i++)
    {
        scanf("%s", &s[i]);
    }
    for (int st = 0; st < h * w; st++)
    {
        //puts("a");
        if (s[st / w][st % w] == '#')
        {
            continue;
        }
        for (int gl = st + 1; gl < h * w; gl++)
        {
            if (s[gl / w][gl % w] == '#')
            {
                continue;
            }
            t = shortest(st, gl);
            ans = (t > ans) ? t : ans;
        }
        //printf("%d %d\n", st, ans);
    }
    printf("%d", ans);
    return 0;
}