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 int X;
        static int Y;
        static void Main(string[] args)
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            Y = input[0];
            X = input[1];
            char[,] map = new char[X, Y];
            for (int y = 0; y < Y; y++)
            {
                var s = Console.ReadLine();
                for (int x = 0; x < X; x++)
                {
                    map[x, y] = s[x];
                }
            }

            // restore
            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];
                }
            }

            // tupleで組み合わせを作成
            int res = 0;
            List<Tuple<int, int, int, int>> t = new List<Tuple<int, int, int, int>>();
            for (int sy = 0; sy < Y; sy++)
            {
                for (int sx = 0; sx < X; sx++)
                {
                    for (int gy = 0; gy < Y; gy++)
                    {
                        for (int gx = 0; gx < X; gx++)
                        {
                            t.Add(Tuple.Create(sx, sy, gx, gy));
                        }
                    }
                }
            }

            foreach (var i in t)
            {

                int sx = i.Item1;
                int sy = i.Item2;
                int gx = i.Item3;
                int gy = i.Item4;
                if (map[sx, sy] != '#' && map[gx, gy] != '#')
                {
                    for (int y = 0; y < Y; y++)
                    {
                        for (int x = 0; x < X; x++)
                        {
                            map[x, y] = map2[x, y];
                        }
                    }
                    res = Math.Max(BFS(sx, sy, gx, gy, map), res);
                }
            }

            Console.WriteLine(res);
        }

        static int BFS(int sx, int sy, int gx, int gy, char[,] map)
        {
            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;

                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));
                    }
                }
            }
            return step;
        }
    }
}