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;
using System.Text;
using System.Text.RegularExpressions;
using static System.Console;
using static System.Math;
using static MyIO;
using static MyUtil;

public class D
{
	public static void Main() => (new Solver()).Solve();
}

public class Solver
{
	public void Solve()
	{
		int H = GetInt();
		int W = GetInt();
		char[,] grid = new char[H, W];
		for(int i = 0; i < H; i++)
		{
			string S = GetString();
			for(int j = 0; j < W; j++)
				grid[i, j] = S[j];
		}

		int V = H * W;
		var adj = Enumerable.Range(0, V).Select(_ => new List<int>()).ToArray();

		int[] di = new int[4] { -1, 0, 1, 0 };
		int[] dj = new int[4] { 0, -1, 0, 1 }; 
		for(int i = 0; i < H; i++)
		{
			for(int j = 0; j < W; j++)
			{
				if(grid[i, j] == '#')
					continue;
				
				int v = i * W + j;
				for(int k = 0; k < 4; k++)
				{
					int ni = i + di[k];
					int nj = j + dj[k];
					if(ni < 0 || ni >= H || nj < 0 || nj >= W || grid[ni, nj] == '#')
						continue;

					int u = ni * W + nj;
					adj[v].Add(u);
				}
			}
		}

		int ans = 0;
		for(int i = 0; i < V; i++)
			chmax(ref ans, bfs(V, adj, i));

		WriteLine(ans);
	}

	private int bfs(int V, List<int>[] adj, int s)
	{
		int max = 0;
		int[] d = Enumerable.Repeat(int.MaxValue / V, V).ToArray();
		bool[] visited = new bool[V];
		d[s] = 0;
		visited[s] = true;
		var queue = new Queue<int>();
		queue.Enqueue(s);
		while(queue.Count > 0)
		{
			int v = queue.Dequeue();
			foreach(int u in adj[v])
			{
				if(visited[u])
					continue;

				visited[u] = true;
				d[u] = d[v] + 1;
				chmax(ref max, d[u]);
				queue.Enqueue(u);
			}
		}
		return max;
	}


}

public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt() => int.Parse(getArg());
	public static long GetLong() => long.Parse(getArg());
	public static double GetDouble() => double.Parse(getArg());
	public static decimal GetDecimal() => decimal.Parse(getArg());
	public static string GetString() => getArg();
	public static char GetChar() => getArg()[0];
	public static int[] GetInts(long N) => Enumerable.Range(0, (int)N).Select(_ => GetInt()).ToArray();
	public static long[] GetLongs(long N) => Enumerable.Range(0, (int)N).Select(_ => GetLong()).ToArray();
	public static double[] GetDoubles(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDouble()).ToArray();
	public static decimal[] GetDecimals(long N) => Enumerable.Range(0, (int)N).Select(_ => GetDecimal()).ToArray();
	public static string[] GetStrings(long N) => Enumerable.Range(0, (int)N).Select(_ => GetString()).ToArray();
}

public static class MyUtil
{
	public static void chmax<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) < 0)
			x = y;
	}

	public static void chmin<T>(ref T x, T y) where T : IComparable<T>
	{
		if(x.CompareTo(y) > 0)
			x = y;
	}

	public static void swap<T>(ref T x, ref T y)
	{
		T tmp = x;
		x = y;
		y = tmp;
	}
}