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 System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split(' ');
        var ySize = int.Parse(input[0]);
        var xSize = int.Parse(input[1]);


        var path = new bool[xSize, ySize];

        for (var i = 0; i < ySize; i++)
        {
            var line = Console.ReadLine().ToCharArray();
            for (var j = 0; j < xSize; j++)
            {
                var isPath = line[j] == '.' ? true : false;
                path[j, i] = isPath;
            }
        }

        var ans = 0;

        for (var i = 0; i < ySize; i++)
        {
            for (int j = 0; j < xSize; j++)
            {
                if(path[j, i])
                {
                    var count = BreadthFirstSearch.CheckExistPath(path, j, i);
                    if(count > ans)
                    {
                        ans = count;
                    }
                }
            }
        }

        Console.WriteLine(ans);
        Console.ReadLine();
    }
}

class BreadthFirstSearch
{
    public static int CheckExistPath(bool[,] maze, int x, int y)
    {
        var queue = new Queue<int[]>();

        queue.Enqueue(new int[2] { x, y });

        var xSize = maze.GetLength(0);
        var ySize = maze.GetLength(1);

        var visited = new bool[xSize, ySize];
        var count = new int[xSize, ySize];

        visited[x, y] = true;

        var finished = false;

        while (!finished)
        {
            if(queue.Count == 0)
            {
                finished = true;
                continue;
            }

            var cell = queue.Dequeue();

            var right = new int[2] { cell[0] + 1, cell[1] };
            if (cell[0] < xSize - 1) 
            {
                if (maze[right[0], right[1]] && !visited[right[0], right[1]])
                {
                    visited[right[0], right[1]] = true;
                    queue.Enqueue(new int[2] { right[0], right[1] });
                    count[right[0], right[1]] = count[cell[0], cell[1]] + 1;
                }
            }

            var down = new int[2] { cell[0], cell[1] + 1 };
            if (cell[1] < ySize - 1)
            {
                if (maze[down[0], down[1]] && !visited[down[0], down[1]])
                {
                    visited[down[0], down[1]] = true;
                    queue.Enqueue(new int[2] { down[0], down[1] });
                    count[down[0], down[1]] = count[cell[0], cell[1]] + 1;
                }
            }

            var left = new int[2] { cell[0] - 1, cell[1] };
            if (cell[0] > 0)
            {
                if (maze[left[0], left[1]] && !visited[left[0], left[1]])
                {
                    visited[left[0], left[1]] = true;
                    queue.Enqueue(new int[2] { left[0], left[1] });
                    count[left[0], left[1]] = count[cell[0], cell[1]] + 1;
                }
            }


            var up = new int[2] { cell[0], cell[1] - 1 };
            if (cell[1] > 0)
            {
                if (maze[up[0], up[1]] && !visited[up[0], up[1]])
                {
                    visited[up[0], up[1]] = true;
                    queue.Enqueue(new int[2] { up[0], up[1] });
                    count[up[0], up[1]] = count[cell[0], cell[1]] + 1;
                }
            }
        }

        var ans = 0;
        foreach (var score in count)
        {
            if (ans < score)
            {
                ans = score;
            }
        }
        return ans;
    }
}