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

/* #include */
#include"bits/stdc++.h"
#include <sstream>
/* 名前空間 */
using namespace std;

/* #define */
// リピート
#define rpt0(i, n) for(ll i = 0; i < (n); i++)  // リピート 0からn - 1まで
#define rpt1(i, n) for(ll i = 1; i <= (n); i++)  // リピート 1からnまで
#define rptb0(i ,n) for(ll i = (n) - 1; i >= 0; --i) // バックするリピート n - 1から0まで
#define rptb1(i ,n) for(ll i = (n); i >= 1; --i) // バックするリピート nから1まで
// 実用省略
#define pb push_back
#define all(v) v.begin(),v.end()
// 数学
#define PI (acos(-1))  // 円周率pi

/* typedef */
// long系
typedef long long ll;
typedef long double ld;
// vector
typedef vector<int> vint; // intのvector
typedef vector<double> vdouble; // doubleのvector
typedef vector<ll> vll; // llのvector
typedef vector<ld> vld; // ldのvector
typedef vector<pair<ll, ll>> vl_l; // l_lのvector
typedef vector<pair<int, int>> vi_i; // i_iのvector
typedef vector<pair<double, double>> vd_d; // d_dのvector
// pair系
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef pair<double, double> d_d;

/* 入出力 read, write */
/* 変数入力 */
// 1変数
template <typename T>
void read(T& x) {
	cin >> x;
}
// 2変数
template <typename T, typename T0>
void read(T& x, T0& y) {
	cin >> x >> y;
}
// 3変数
template <typename T, typename T0, typename T1>
void read(T& x, T0& y, T1& z) {
	cin >> x >> y >> z;
}
// 4変数
template <typename T, typename T0, typename T1, typename T2>
void read(T& x, T0& y, T1& z, T2& w) {
	cin >> x >> y >> z >> w;
}

/* vector, 配列, pair入力 */
// vector
template <typename T>
void read(vector< T >& oneD, int n) {
	ll n1 = (ll)n;
	rpt0(i, n1)
	{
		ll x;
		read(x);
		oneD.push_back(x);
	}
}
template <typename T>
void read(vector< T >& oneD, ll n) {
	rpt0(i, n)
	{
		ll x;
		read(x);
		oneD.push_back(x);
	}
}
// 配列
template <typename T>
void read(T oneD[], int n) {
	ll n1 = (ll)n;
	rpt1(i, n1) {
		read(oneD[i]);
	}
}
template <typename T>
void read(T oneD[], ll n) {
	rpt1(i, n) {
		read(oneD[i]);
	}
}
// pair
template <typename T, typename T0>
void read(pair< T, T0 >& p) {
	cin >> p.first >> p.second;
}

/* 変数出力 */
// 1変数
template <typename T>
void write(T& x) {

	cout << x << endl;
}
// 2変数
template <typename T, typename T0>
void write(T& x, T0& y) {
	cout << x << " " << y << "\n";
}
// 3変数
template <typename T, typename T0, typename T1>
void write(T& x, T0& y, T1& z) {
	cout << x << " " << y << " " << z << "\n";
}
// 4変数
template <typename T, typename T0, typename T1, typename T2>
void write(T& x, T0& y, T1& z, T2& w) {
	cout << x << " " << y << " " << z << " " << w << "\n";
}

/* vector, 配列, pair出力 */
// vector
template <typename T>
void write(vector< T >& oneD, int n) {
	ll n1 = (ll)n;
	rpt0(i, n1) {
		cout << oneD[i] << " ";
	}
	cout << endl;
}
template <typename T>
void write(vector< T >& oneD, ll n) {
	rpt0(i, n) {
		cout << oneD[i] << " ";
	}
	cout << endl;
}
// 配列
template <typename T>
void write(T oneD[], int n) {
	ll n1 = (ll)n;
	rpt1(i, n1) {
		write(oneD[i]);
	}
	cout << endl;
}
template <typename T>
void write(T oneD[], ll n) {
	rpt1(i, n) {
		write(oneD[i]);
	}
	cout << endl;
}
// pair
template <typename T, typename T0>
void write(pair< T, T0 >& p) {
	cout << p.first << " " << p.second << " ";
	cout << endl;
}


// 八方向への移動ベクトル
const int dx[8] = { 1, 0, -1, 0, 1, 1, -1, -1 };
const int dy[8] = { 0, 1, 0, -1, 1, -1, 1, -1 };
// 右、上、左、下、右上、右下、左上、左下

// 入力
int fh, fw;   // フィールドのfh->縦、fw->横
vector<string> field;   // フィールド

int main(void) {
	// 入力受け取り
	read(fh, fw);
	field.resize(fh);
	for (int h = 0; h < fh; ++h) cin >> field[h];
	int s[2], g[2];
	int max = 0;
	for (int h = 0; h < fh; ++h) {
		for (int w = 0; w < fw; ++w) {
			if (field[h][w] == '#') continue;
			s[0] = h; s[1] = w;
			for (int gh = 0; gh < fh; ++gh) {
				for (int gw = 0; gw < fw; ++gw) {
					if (field[gh][gw] == '#') continue;
					g[0] = gh; g[1] = gw;
					// 探索開始
					// BFS のためのデータ構造
					int dist[50][50];
					memset(dist, -1, sizeof(dist)); // dist 配列全体を -1 に初期化
					queue<i_i> que; // 訪問保留中の頂点集合

					// 初期条件 (頂点 0 を初期ノードとする)
					dist[s[0]][s[1]] = 0;
					i_i sp(s[0], s[1]);	// スタートの座標をキューにセット
					que.push(sp); // スタートを保留にする

					// BFS 開始 (キューが空になるまで探索を行う)
					while (!que.empty()) {
						i_i v = que.front(); // キューから先頭頂点を取り出す
						que.pop();  // 保留にする

						// v から辿れる頂点をすべて調べる
						for (int dir = 0; dir < 4; ++dir) {     // 方向選択
						// dir = 0 -> 右、1 -> 上、2 -> 左、 3 -> 下
							int nh = v.first + dx[dir];
							int nw = v.second + dy[dir];
							if (nh < 0 || nh >= fh || nw < 0 || nw >= fw) continue;     // 場外アウトの場合
							else if (field[nh][nw] == '#') continue;     // 移動先が壁の場合
							else if (dist[nh][nw] != -1) continue;    // 移動先が探索済みの場合

							// 新たな探索していない頂点 nv について距離情報を更新してキューに追加する
							dist[nh][nw] = dist[v.first][v.second] + 1;
							i_i np(nh, nw);
							que.push(np);
						}
					}
					// 結果出力 (各頂点の頂点 0 からの距離を見る)
					if (max < dist[g[0]][g[1]]) max = dist[g[0]][g[1]];
				}
			}
		}
	}
	write(max);

	return 0;
}