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;

constexpr int INF = 0x3f3f3f3f;
constexpr int H = 20;
constexpr int W = 20;
constexpr int N = H * W;

int dir[2][4] = {{-1, 1, 0, 0}, {0, 0, -1, 1}};
vector<int> g[N + 1];
int dist[N + 1][N + 1];
char mat[H + 2][W + 2];
int h, w, n; // (Input)

/* O(V^3) - Returns true if there's a negative cycle in the graph. */
bool floyd_warshall() {
	// Initializing.
	memset(dist, 0x3f, sizeof(dist));

	// Filling distance matrix with the edges.
	for (int u = 1; u <= n; u++) {
		dist[u][u] = 0;

		for (int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			dist[u][v] = 1;
		}
	}

	// For every intermediate vertex k.
	for (int k = 1; k <= n; k++) {
		// For every starting vertex i.
		for (int i = 1; i <= n; i++) {
			// For every ending vertex j.
			for (int j = 1; j <= n; j++) {
				// If there is a better path using k as intermediate vertex.
				if (dist[i][k] + dist[k][j] < dist[i][j]) {
					dist[i][j] = dist[i][k] + dist[k][j];
				}
			}
		}
	}

	// Checking for negative cycles.
	for (int u = 1; u <= n; u++) {
		if (dist[u][u] < 0) {
			return true; // u belongs to a negative cycle.
		}
	}

	// No negative cycles.
	return false;
}

bool inside(int x, int y) {
	return 0 <= x and x < h and 0 <= y and y < w;
}

bool valid(int x, int y) {
	return inside(x, y) and mat[x][y] != '#';
}

int main() {
	int ans, i, j, k, u, v;

	scanf("%d%d", &h, &w);
	n = h * w;

	for (i = 0; i < h; i++) {
		scanf(" %s", mat[i]);
	}

	for (i = 0; i < h; i++) {
		for (j = 0; j < w; j++) {
			if (valid(i, j)) {
				u = i * w + j + 1;

				for (k = 0; k < 4; k++) {
					int xf = i + dir[0][k];
					int yf = j + dir[1][k];
					v = xf * w + yf + 1;

					if (valid(xf, yf)) {
						g[u].push_back(v);
						g[v].push_back(u);
					}
				}
			}
		}
	}

	floyd_warshall();
	ans = 0;

	for (u = 1; u <= n; u++) {
		for (v = 1; v <= n; v++) {
			if (dist[u][v] < INF) {
				ans = max(ans, dist[u][v]);
			}
		}
	}

	printf("%d\n", ans);

	return 0;
}