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 ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var input1 = Console.ReadLine().Split().Select(long.Parse).ToList();
            var H = input1[0];
            var W = input1[1];

            var maze = new int[H, W];
            for (int i = 0; i < H; i++)
            {
                var S = Console.ReadLine().Select(s => s == '.' ? 0 : -1).ToList();
                for (int j = 0; j < W; j++)
                {
                    maze[i, j] = S[j];
                }
            }

            var ans = 0;
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    var count = Count(Copy(maze), i, j);
                    if (ans < count) ans = count;
                }
            }

            Console.WriteLine(ans);
        }

        private static int[,] Copy(int[,] source)
        {
            var ret = new int[source.GetLength(0), source.GetLength(1)];
            Array.Copy(source, ret, source.Length);
            return ret;
        }

        private static int Count(int[,] maze, int x, int y)
        {
            var list = new List<Tuple<int, int>>() { new Tuple<int, int>(x, y) };
            maze[x, y] = 1;

            var count = -1;
            while (list.Any())
            {
                count++;
                list = FillAround(maze, list);
            }

            return count;
        }

        private static List<Tuple<int, int>> FillAround(int[,] maze, List<Tuple<int, int>> list)
        {
            var nextList = new List<Tuple<int, int>>();
            list.ForEach(s => nextList.AddRange(FillAround(maze, s)));
            return nextList;
        }

        private static List<Tuple<int, int>> FillAround(int[,] maze, Tuple<int, int> item)
        {
            var nextList = new List<Tuple<int, int>>();
            var x = item.Item1;
            var y = item.Item2;

            Fill(maze, nextList, x - 1, y);
            Fill(maze, nextList, x + 1, y);
            Fill(maze, nextList, x, y - 1);
            Fill(maze, nextList, x, y + 1);

            return nextList;
        }

        private static void Fill(int[,] maze, List<Tuple<int, int>> list, int x, int y)
        {
            if (x < 0 || x >= maze.GetLength(0) || y < 0 || y >= maze.GetLength(1)) return;
            if (maze[x, y] == 0)
            {
                maze[x, y] = 1;
                list.Add(new Tuple<int, int>(x, y));
            }
        }
    }
}