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.Linq;
using System.Collections.Generic;
namespace ABC151
{
    class D
    {

        static char[,] map;
        static int X;
        static int Y;

        static int dist;
        static void Main(string[] args)
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            Y = input[0];
            X = input[1];

            map = new char[X, Y];
            for (int y = 0; y < Y; y++)
            {
                string s = Console.ReadLine();
                for (int x = 0; x < X; x++)
                {
                    map[x, y] = s[x];
                }
            }

            char[,] map2 = new char[X, Y];
            for (int y = 0; y < Y; y++)
            {
                for (int x = 0; x < X; x++)
                {
                    map2[x, y] = map[x, y];
                }
            }

            int res = 0;
            for (int y = 0; y < Y; y++)
            {
                for (int x = 0; x < X; x++)
                {

                    int sx = x;
                    int sy = y;
                    dist = 0;

                    for (int y2 = 0; y2 < Y; y2++)
                    {
                        for (int x2 = 0; x2 < X; x2++)
                        {

                            for (int y3 = 0; y3 < Y; y3++)
                            {
                                for (int x3 = 0; x3 < X; x3++)
                                {
                                    map[x3, y3] = map2[x3, y3];
                                }
                            }

                            int gx = x2;
                            int gy = y2;
                            //Console.WriteLine("{0} {1} {2} {3} ", sx, sy, gx, gy);
                            BFS(sx, sy, gx, gy);
                            res = Math.Max(dist, res);
                        }
                    }
                }
            }

            Console.WriteLine(res);

        }

        static void BFS(int sx, int sy, int gx, int gy)
        {

            Queue<Tuple<int, int, int>> tq = new Queue<Tuple<int, int, int>>();
            tq.Enqueue(Tuple.Create(sx, sy, 0));
            int step = 0;
            map[sx, sy] = '#';

            int[] vx = { 0, 1, 0, -1 };
            int[] vy = { 1, 0, -1, 0 };

            while (0 < tq.Count)
            {
                var q = tq.Dequeue();
                int x = q.Item1;
                int y = q.Item2;
                step = q.Item3;

                if (x == gx && y == gy)
                {
                    dist = step;
                    //Console.WriteLine(step);
                    return;
                }

                for (int i = 0; i < 4; i++)
                {
                    int nx = x + vx[i];
                    int ny = y + vy[i];

                    if ((0 <= nx && nx < X) && (0 <= ny && ny < Y) && map[nx, ny] == '.')
                    {
                        map[nx, ny] = '#';
                        tq.Enqueue(Tuple.Create(nx, ny, step + 1));
                    }
                }
            }
        }
    }
}