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;

public struct Point
{
    public int X;
    public int Y;

    public Point(Point p)
    {
        X = p.X;
        Y = p.Y;
    }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public static bool operator ==(Point a, Point b)
    {
        return (a.X == b.X && a.Y == b.Y);
    }

    public static bool operator !=(Point a, Point b)
    {
        return !(a == b);
    }

    //objと自分自身が等価のときはtrueを返す
    public override bool Equals(object obj)
    {
        if (!(obj is Point))
        {
            return false;
        }

        Point p = (Point)obj;
        return (X == p.X && Y == p.Y);
    }

    //Equalsがtrueを返すときに同じ値を返す
    public override int GetHashCode()
    {
        return (X << 16) ^ Y;
    }

    public Point Clone()
    {
        return new Point(X, Y);
    }

    public void Reverse()
    {
        X = -X;
        Y = -Y;
    }

    public static Point operator +(Point a, Point b)
    {
        return new Point(a.X + b.X, a.Y + b.Y);
    }

    public static Point operator -(Point a, Point b)
    {
        return new Point(a.X - b.X, a.Y - b.Y);
    }

    public static Point operator *(Point a, int i)
    {
        return new Point(a.X * i, a.Y * i);
    }

    public static Point operator /(Point a, int i)
    {
        return new Point(a.X / i, a.Y / i);
    }
    public static Point operator +(Point a)
    {
        return new Point(+a.X, +a.Y);
    }

    public static Point operator -(Point a)
    {
        return new Point(-a.X, -a.Y);
    }

    public override string ToString()
    {
        return string.Format("({0},{1})", X, Y);
    }

    IEnumerable<Point> GetNeighbor_()
    {
        yield return new Point(X + 1, Y + 0);
        yield return new Point(X + 0, Y + 1);
        yield return new Point(X - 1, Y + 0);
        yield return new Point(X + 0, Y - 1);
    }

    public bool InRange(Point max)
    {
        return 0 <= X && X < max.X && 0 <= Y && Y < max.Y;
    }

    public IEnumerable<Point> GetNeighbor()
    {
        return GetNeighbor_().Where(p => p.InRange(Max));
    }

    public static Point Max;
}

public class Solution
{
    static int GetMaxStep(string[] maze, Point start)
    {
        int step = -1;
        if (maze[start.Y][start.X] == '#')
        {
            return step;
        }
        var visited = new HashSet<Point>();
        visited.Add(start);
        var currents = new List<Point>() { start };
        while (currents.Count > 0)
        {
            step++;
            var nexts = new List<Point>();
            foreach (var cur in currents)
            {
                foreach (var nei in cur.GetNeighbor())
                {
                    if (visited.Contains(nei) || maze[nei.Y][nei.X] == '#')
                    {
                        continue;
                    }
                    visited.Add(nei);
                    nexts.Add(nei);
                }
            }
            currents = nexts;
        }
        return step;
    }

    static int GetMaxStep(string[] maze)
    {
        var max = -1;
        for (int y = 0; y < Point.Max.Y; y++)
        {
            for (int x = 0; x < Point.Max.X; x++)
            {
                max = Math.Max(max, GetMaxStep(maze, new Point(x, y)));
            }
        }
        return max;
    }

    public static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var H = vals[0];
        var W = vals[1];
        Point.Max = new Point(W, H);
        var maze = new string[H];
        for (int i = 0; i < H; i++)
        {
            maze[i] = Console.ReadLine();
        }

        var max = GetMaxStep(maze);
        Console.WriteLine(max);
    }
}