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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    public class IPoint
    {
        public int X { get; private set; } = 0;
        public int Y { get; private set; } = 0;
        public IPoint(int x, int y)
        {
            X = x;
            Y = y;
        }

        public override bool Equals(object obj)
        {
            if(obj == null || !(obj is IPoint))
            {
                return false;
            }
            else
            {
                var tmp = obj as IPoint;
                return (this.X == tmp.X && this.Y == tmp.Y);
            }
        }
    }

    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
        var _H = input[0];
        var _W = input[1];
        var maze = new bool[_H, _W];
        for(var i = 0; i < _H; i++)
        {
            var line = Console.ReadLine();
            for(var j = 0; j < _W; j++)
            {
                maze[i, j] = (line[j] == '.');
            }
        }

        int max = 0;
        for(var i1 = 0; i1 < _H; i1++)
        {
            for(var j1 = 0; j1 < _W; j1++)
            {
                for (var i2 = 0; i2 < _H; i2++)
                {
                    for (var j2 = 0; j2 < _W; j2++)
                    {
                        var tmp = ClearMaze(maze, new IPoint(i1, j1), new IPoint(i2, j2));
                        if(max < tmp)
                        {
                            max = tmp;
                        }
                    }
                }
            }
        }
        Console.WriteLine(max);

#if DEBUG
        Console.ReadKey();
#endif
    }

    static int ClearMaze(bool[,] maze, IPoint start, IPoint end)
    {
        if (maze[start.X, start.Y] && maze[end.X, end.Y] && !start.Equals(end))
        {
            var _H = maze.GetLength(0);
            var _W = maze.GetLength(1);
            var score = new long[_H, _W];
            for(var i = 0; i < _H; i++)
            {
                for (var j = 0; j < _W; j++)
                {
                    score[i, j] = int.MaxValue;
                }
            }
            var nextStartPoint = new List<IPoint>();
            nextStartPoint.Add(start);
            for (var i = 1; i <= int.MaxValue; i++)
            {
                var tmpNextStartPoint = new List<IPoint>();
                foreach(var point in nextStartPoint)
                {
                    var tmpArray = new List<IPoint>() { new IPoint(point.X - 1, point.Y),
                                                        new IPoint(point.X + 1, point.Y), 
                                                        new IPoint(point.X,     point.Y - 1), 
                                                        new IPoint(point.X,     point.Y + 1) };
                    foreach(var tmp in tmpArray)
                    {
                        if (tmp.X >= 0 && tmp.Y>= 0 && tmp.X < _H && tmp.Y < _W && maze[tmp.X, tmp.Y] && score[tmp.X, tmp.Y] > i)
                        {
                            score[tmp.X, tmp.Y] = i;
                            tmpNextStartPoint.Add(tmp);
                            if (tmp.Equals(end))
                            {
                                return i;
                            }
                        }
                    }
                }
                nextStartPoint = tmpNextStartPoint;
            }
        }
        return -1;
    }
}