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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args) {
#if DEBUG
        //MyDebugger.MakeTestCase("../TextFile2.txt"); return;
        MyDebugger.Test(process, "../TextFile1.txt");
#else
        object ret = process(new StreamReader(Console.OpenStandardInput()));
        Console.WriteLine(ret);
#endif
    }

    static int h;
    static int w;
    static char[][] ss;

    static object process(TextReader input) {
        var spl   = input.ReadLine().Split(' ');
        h     = int.Parse(spl[0]);
        w     = int.Parse(spl[1]);

        ss = new char[h][];

        for(int i=0; i<h; i++) {
            ss[i] = new char[w];
            var s = input.ReadLine();
            for(int j=0; j<w; j++) {
                ss[i][j] = s[j];
            }
        }

        int max = 0;
        for(int sh=0; sh<h; sh++) {
            for(int sw=0; sw<w; sw++) {
                max = Math.Max(calc(sh, sw), max);
            }
        }

        return max;
    }

    static bool isWall(int i, int j) {
        if (i < 0 || i >= h || j < 0 || j >= w) return true;
        if (ss[i][j] == '#') return true;
        return false;
    }

    static int calc(int sh, int sw) {
        var tbl = new int[h][];
        for (int ii = 0; ii < h; ii++) {
            tbl[ii] = new int[w];
            for(int jj = 0; jj < w; jj++) {
                tbl[ii][jj] = int.MaxValue;
            }
        }

        sett(sh, sw, 0, tbl);

        int max = 0;
        for(int i=0; i<h; i++) {
            for(int j=0; j<w; j++) {
                if(tbl[i][j] != int.MaxValue) {
                    max = Math.Max(tbl[i][j], max);
                }
            }
        }
        return max;
    }

    static void sett(int i, int j, int value, int[][] tbl) {
        if (i < 0 || i >= h || j < 0 || j >= w) return;
        if (ss[i][j] == '#') return;

        if(value < tbl[i][j]) {
            tbl[i][j] = value;
            sett(i - 1, j, value + 1, tbl);
            sett(i + 1, j, value + 1, tbl);
            sett(i, j - 1, value + 1, tbl);
            sett(i, j + 1, value + 1, tbl);
        }
    }
}