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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <string>
#include <cmath>
#include <complex>
#include <numeric>
#include <cassert>

#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
using namespace std;

int main()
{
	int64 hh, ww, ans = 0, tmp;
	int64 xfil[] = {-1, 1, 0, 0}, yfil[] = {0, 0, -1, 1};
	char cc;
	cin >> hh >> ww;
	vector<vector<bool>> table(hh + 10, vector<bool> (ww + 10, false));
	for(int64 ii = 1;ii <= hh;ii++)
	{
		for(int64 jj = 1;jj <= ww;jj++)
		{
			cin >> cc;
			if(cc == '.')
			{
				table[ii][jj] = true;
			}
		}
	}
	for(int64 ii = 1;ii <= hh;ii++)
	{
		for(int64 jj = 1;jj <= ww;jj++)
		{
			if(!table[ii][jj])
			{
				continue;
			}
			tmp = 0;
			queue<pair<int64, int64>> qq;
			vector<vector<int64>> memo(hh + 10, vector<int64> (ww + 10, 1000000000));
			qq.push(make_pair(ii, jj));
			memo[ii][jj] = 0;
			while(!qq.empty())
			{
				int64 xx, yy;
				xx = (qq.front()).first;
				yy = (qq.front()).second;
				qq.pop();
				for(int64 kk = 0;kk < 4;kk++)
				{
					if(table[xx + xfil[kk]][yy + yfil[kk]] && memo[xx + xfil[kk]][yy + yfil[kk]] == 1000000000)
					{
						memo[xx + xfil[kk]][yy + yfil[kk]] = memo[xx][yy] + 1;
						qq.push(make_pair(xx + xfil[kk], yy + yfil[kk]));
						tmp = max(tmp, memo[xx][yy] + 1);
					}
				}
			}
			ans = max(ans, tmp);
		}
	}
	cout << ans << endl;
	return 0;
}