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 <bits/stdc++.h>

using namespace std;

#define fastio ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define t_times int t; cin >> t; while(t--)
#define _parr(a, n) for(int __i = 0; __i < (int )n; __i++) cout << a[__i] << ' '; cout << '\n'
#define fr(i, p, n) for(int i = (int )p; i < (int )n; i++)
#define frn(i, p, n) for(int i = (int )p; i <= (int )n; i++)
#define rev(i, n, p) for(int i = (int )n; i >= (int )p; i--)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).begin(), (c).end()
#define sci(n) scanf("%d", &n)
#define scl(n) scanf("%lld", &n)
#define prin(n) printf("%d\n", n)
#define prln(n) printf("%lld\n", n)
#define mp make_pair
#define F first
#define S second
#define pb push_back

typedef long long int LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef pair< int, int> ii;
typedef vector< int> vi;

const int mod = 1e9 + 7;
const LL INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 2e5 + 22;
const double eps = 1e-10;

int n, m;
bool vis[25][25];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

bool _ok(int i, int j) {
	return (i >= 0 && i < n && j >= 0 && j < m);
}
int _bfs(vector< string> &v, int i, int j) {
	if(!_ok(i, j)) return 0;
	if(vis[i][j] || v[i][j] == '#') return 0;
	queue< pair< ii, int>> q;
	q.push({{i, j}, 0});
	int res = 0;
	while(!q.empty()) {
		auto tp = q.front();
		q.pop();
		int x = tp.F.F, y = tp.F.S, dis = tp.S;
		if(!_ok(x, y)) continue;
		if(vis[x][y] || v[x][y] == '#') continue;
		res = max(res, dis);
		vis[x][y] = 1;
		fr(k, 0, 4) q.push({{x + dx[k], y + dy[k]}, dis + 1});
	}
	//cout << res << '\n';
	return res;
}
void _solve() {
	cin >> n >> m;
	vector< string> v(n);
	fr(i, 0, n) cin >> v[i];
	int ans = 0;
	fr(i, 0, n) {
		fr(j, 0, m) {
			memset(vis, 0, sizeof vis);
			ans = max(ans, _bfs(v, i, j));
		}
	}
	cout << ans;
}

int main() {
    fastio;
    //t_times
    _solve();
    return 0;
}