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 AtCoderBeginnerContest151.Questions;
using AtCoderBeginnerContest151.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace AtCoderBeginnerContest151.Questions
{
    public class QuestionD : AtCoderQuestionBase
    {
        bool[,] canEnter;
        int h;
        int w;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var hw = inputStream.ReadIntArray();
            h = hw[0];
            w = hw[1];

            canEnter = new bool[h + 2, w + 2];

            for (int i = 0; i < h; i++)
            {
                var s = inputStream.ReadLine().Select(c => c == '.').ToArray();

                for (int j = 0; j < s.Length; j++)
                {
                    canEnter[i + 1, j + 1] = s[j];
                }
            }

            var max = int.MinValue;
            for (int i = 1; i <= h; i++)
            {
                for (int j = 1; j <= w; j++)
                {
                    if (canEnter[i, j])
                    {
                        max = Math.Max(max, GetMaxDistance(i, j));
                    }
                }
            }

            yield return max;
        }

        private int GetMaxDistance(int startH, int startW)
        {
            var seen = new bool[h + 2, w + 2];
            var distances = new int[h + 2, w + 2];
            var todo = new Queue<Coordinate>();

            todo.Enqueue(new Coordinate(startH, startW));
            seen[startH, startW] = true;

            while (todo.Any())
            {
                var current = todo.Dequeue();
                var currentDistance = distances[current.H, current.W];

                CheckCanGoAndQueue(current.H + 1, current.W, currentDistance, seen, todo, distances);
                CheckCanGoAndQueue(current.H - 1, current.W, currentDistance, seen, todo, distances);
                CheckCanGoAndQueue(current.H, current.W + 1, currentDistance, seen, todo, distances);
                CheckCanGoAndQueue(current.H, current.W - 1, currentDistance, seen, todo, distances);
            }

            var max = 0;

            for (int i = 1; i <= h; i++)
            {
                for (int j = 1; j <= w; j++)
                {
                    max = Math.Max(max, distances[i, j]);
                }
            }

            return max;
        }

        private void CheckCanGoAndQueue(int nextH, int nextW, int currentDistance, bool[,] seen, Queue<Coordinate> todo, int[,] distances)
        {
            if (canEnter[nextH, nextW] && !seen[nextH, nextW])
            {
                todo.Enqueue(new Coordinate(nextH, nextW));
                seen[nextH, nextW] = true;
                distances[nextH, nextW] = currentDistance + 1;
            }
        }

    }

    struct Coordinate
    {
        public int H { get; }
        public int W { get; }

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


namespace AtCoderBeginnerContest151
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionD();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
        }
    }
}

#region Base Classes

namespace AtCoderBeginnerContest151.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }

}

#endregion

#region Extensions

namespace AtCoderBeginnerContest151.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);
    }
}

#endregion