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.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.D
{
    public class Program
    {
        public static long H { get; private set; }
        public static long W { get; private set; }
        public static char[,] S { get; private set; }

        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var inputs = ReadLongs();
            H = inputs[0];
            W = inputs[1];
            S = new char[H, W];
            for (int i = 0; i < H; i++)
            {
                var w = ReadText();
                for (int j = 0; j < W; j++)
                {
                    S[i, j] = w[j];
                }
            }

            var maxDist = 0L;
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    if (S[i, j] == '#') continue;

                    var dist = GetDistTable();
                    var queue = new Queue<Point>();
                    var startPoint = new Point(i, j);
                    queue.Enqueue(startPoint);
                    dist[i, j] = 0;

                    while (queue.Count > 0)
                    {
                        Search_BFS(dist, queue);
                    }

                    maxDist = Math.Max(maxDist, GetMax(dist));
                }
            }

            return maxDist;
        }

        private static void Search_BFS(long[,] dist, Queue<Point> queue)
        {
            var p = queue.Dequeue();

            var nexts = new List<Point>
            {
                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),
            };

            foreach (var next in nexts)
            {
                // 迷路の壁 or 外
                if (next.H < 0 || next.H >= H) continue;
                if (next.W < 0 || next.W >= W) continue;
                if (S[next.H, next.W] == '#') continue;

                // 到達済み
                if (dist[next.H, next.W] > -1) continue;

                // 距離の記録
                dist[next.H, next.W] = dist[p.H, p.W] + 1;

                queue.Enqueue(next);
            }
        }

        private static long[,] GetDistTable()
        {
            var copy = new long[H, W];
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    copy[i, j] = -1;
                }
            }
            return copy;
        }
        private static long GetMax(long[,] depth)
        {
            var max = 0L;
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    max = Math.Max(max, depth[i, j]);
                }
            }
            return max;
        }

        #region Console

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }

    internal class Point
    {
        public int H { get; set; }
        public int W { get; set; }

        public Point(int h, int w)
        {
            H = h;
            W = w;
        }
    }
}