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;

namespace abc151_d
{
    class Program
    {
        static void Main(string[] args)
        {

            var HW = GetIntList();
            var H = HW[0];
            var W = HW[1];

            var map = new int[H + 2, W + 2];

            for (int i = 1; i <= H; i++)
            {
                var l = GetStr();
                for (int j = 1; j <= W; j++)
                    if (l[j - 1] == '.') map[i, j] = 1;
            }

            int r = 0;

            //for (int i = 0; i <= H + 1; i++)
            //{
            //    for (int j = 0; j <= W + 1; j++)
            //        Console.Write(map[i, j]);
            //    Console.WriteLine();
            //}

            for (int sy = 1; sy <= H; sy++)
            {
                for (int sx = 1; sx <= W; sx++)
                {
                    r = Math.Max(r, ResolveMaze(map, new int[] { sy, sx }));
                }
            }
            
            Console.WriteLine(r);
        }

        /// <summary>
        /// 迷路最短経路探索（幅優先）
        /// </summary>
        /// <param name="map"></param>
        /// <param name="S">スタート y, x</param>
        /// <returns></returns>
        public static int ResolveMaze(int[,] map, int[] S) 
        {
            var mapr = new int[map.GetLength(0), map.GetLength(1)];

            var Q = new Queue<int[]>();

            Q.Enqueue(S.ToArray());

            mapr[S[0], S[1]] = 1;

            var offsetList = new List<int[]>();

            offsetList.Add(new int[] { -1, 0 });
            offsetList.Add(new int[] { 1, 0 });
            offsetList.Add(new int[] { 0, -1 });
            offsetList.Add(new int[] { 0, 1 });

            int r = 0;

            while (Q.Count != 0)
            {
                var n = Q.Dequeue();

                foreach (var offset in offsetList)
                    if (map[n[0] + offset[0], n[1] + offset[1]] == 1 && mapr[n[0] + offset[0], n[1] + offset[1]] == 0)
                    {
                        Q.Enqueue(new int[] { n[0] + offset[0], n[1] + offset[1] });
                        mapr[n[0] + offset[0], n[1] + offset[1]] = mapr[n[0], n[1]] + 1;
                        r = Math.Max(mapr[n[0], n[1]] + 1, r);
                    }

                //for (int i = 0; i < map.GetLength(0); i++)
                //{
                //    for (int j = 0; j < map.GetLength(1); j++)
                //        Console.Write(mapr[i, j].ToString("|00"));
                //    Console.WriteLine();
                //}
                //Console.ReadKey();
            }

            return r - 1;
        }


        public static string GetStr() => Console.ReadLine();
        public static int GetInt() => int.Parse(Console.ReadLine());
        public static long GetLong() => long.Parse(Console.ReadLine());
        public static double GetDouble() => double.Parse(Console.ReadLine());
        public static decimal GetDecimal() => decimal.Parse(Console.ReadLine());

        public static List<string> GetStrList() => Console.ReadLine().Split(' ').ToList();
        public static List<int> GetIntList() => Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
        public static List<long> GetLongList() => Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList();
        public static List<double> GetDoubleList() => Console.ReadLine().Split(' ').Select(x => double.Parse(x)).ToList();
        public static List<decimal> GetDecimalList() => Console.ReadLine().Split(' ').Select(x => decimal.Parse(x)).ToList();



    }
}