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: Wrong Answer)

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

namespace AtCoder
{
    class Program
    {
        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }

        struct Point
        {
            public readonly int h;
            public readonly int w;
            public Point(int h, int w)
            {
                this.h = h;
                this.w = w;
            }
        }

        const int INF = int.MaxValue / 2;

        static Point Empty(int H, int W, string[] lines)
        {
            for (int h = 0; h < H; ++h)
            {
                for (int w = 0; w < W; ++w)
                {
                    if (lines[h][w] == '.')
                    {
                        return new Point(h, w);
                    }
                }
            }
            return new Point(0, 0);
        }

        static Point Search(Point S, int H, int W, string[] lines, int[][] memo)
        {
            var queue = new Queue<Point>();
            memo[S.h][S.w] = 0;
            queue.Enqueue(S);
            while (queue.Count > 0)
            {
                var p = queue.Dequeue();
                foreach (var q in new[] { new Point(p.h - 1, p.w),
                                          new Point(p.h + 1, p.w),
                                          new Point(p.h, p.w - 1),
                                          new Point(p.h, p.w + 1) })
                {
                    if (0 <= q.h && q.h < H && 0 <= q.w && q.w < W && lines[q.h][q.w] == '.')
                    {
                        if (memo[q.h][q.w] > memo[p.h][p.w] + 1)
                        {
                            memo[q.h][q.w] = memo[p.h][p.w] + 1;
                            queue.Enqueue(q);
                        }
                    }
                }
            }
            var max = S;
            for (int h = 0; h < H; ++h)
            {
                for (int w = 0; w < W; ++w)
                {
                    if (lines[h][w] == '.' && memo[h][w] > memo[max.h][max.w])
                    {
                        max = new Point(h, w);
                    }
                }
            }
            return max;
        }

        static int Solve(int H, int W, string[] lines)
        {
            var P0 = Empty(H, W, lines);

            var memo1 = new int[H][];
            for (int i = 0; i < H; ++i) { memo1[i] = Enumerable.Repeat(INF, W).ToArray(); }
            var P1 = Search(P0, H, W, lines, memo1);

            var memo2 = new int[H][];
            for (int i = 0; i < H; ++i) { memo2[i] = Enumerable.Repeat(INF, W).ToArray(); }
            var P2 = Search(P1, H, W, lines, memo2);

            return memo2[P2.h][P2.w];
        }

        static void Main(string[] args)
        {
            var V = ReadInts();
            int H = V[0], W = V[1];
            var lines = new string[H];
            for (int i = 0; i < H; ++i) { lines[i] = Read(); }
            Console.WriteLine(Solve(H, W, lines));
        }
    }
}