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 main(void) {
    int H, W, M, x, y, i, j, k;
    int T, t, h, w, a, b, A[25][25];
    int AB, C;
    char S[25][25];
    scanf("%d%d", &H, &W);
    T=H*W;
    for (i=0; i<H; i++) {
        scanf("%s", S[i]);
        for (j=0; j<W; j++) {
            if (S[i][j]=='#') T--;
        }
        // for (i=0; i<H; i++) printf("%s\n", S[i]);
    }
    // printf("%d %d", H, W);
    M=0;
    for (i=0; i<H; i++) {
        for (j=0; j<W; j++) {
            for (k=0; k<H*W; k++) {
                if (S[k/W][k%W]=='.') A[k/W][k%W]=1000;
                else A[k/W][k%W]=1001;
                // printf("%d: %d %d\n", k, k/W, k%W);
            }
            if (S[i][j]=='#') continue;
            A[i][j]=0;
            t=T-1;
            AB=0;
            while (t>0) {
                for (k=0; k<H*W; k++) {
                    h=k/W; w=k%W;
                    if (h==0) a=-1;
                    else if (h==H-1) a=1;
                    else a=0;
                    if (w==0) b=-1;
                    else if (w==W-1) b=1;
                    else b=0;
                    y=A[h][w];
                    C=0;
                    if (a!=-1) {
                        if (y<A[h-1][w]) {
                            if (A[h-1][w]==1001) C=1;
                            if (A[h-1][w]==1000 && C==0) t--;
                            if (A[h-1][w]>y+1 && C==0) A[h-1][w]=y+1;
                        }
                    }
                    C=0;
                    if (a!=1) {
                        if (y<A[h+1][w]) {
                            if (A[h+1][w]==1001) C=1;
                            if (A[h+1][w]==1000 && C==0) t--;
                            if (A[h+1][w]>y+1 && C==0) A[h+1][w]=y+1;
                        }
                    }
                    C=0;
                    if (b!=-1) {
                        if (y<A[h][w-1]) {
                            if (A[h][w-1]==1001) C=1;
                            if (A[h][w-1]==1000 && C==0) t--;
                            if (A[h][w-1]>y+1 && C==0) A[h][w-1]=y+1;
                        }
                    }
                    C=0;
                    if (b!=1) {
                        if (y<A[h][w+1]) {
                            if (A[h][w+1]==1001) C=1;
                            if (A[h][w+1]==1000 && C==0) t--;
                            if (A[h][w+1]>y+1 && C==0) A[h][w+1]=y+1;
                        }
                    }
                    // printf("(%d %d %d): %d\n", AB, h, w, t);
                }
                // AB++;
                // if (AB>=10) break;
            }
            /*
            for (k=0; k<H*W; k++) {
                h=k/W; w=k%W;
                printf("%4d", A[h][w]);
                if (w!=W-1) printf(" ");
                else printf("\n");
            }
            */
            for (k=0; k<H*W; k++) {
                h=k/W; w=k%W;
                if (A[h][w]==1001) continue;
                if (A[h][w]>M) M=A[h][w];
            }
        }
    }
    printf("%d", M);
    return 0;
}