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)

//g++ .cpp && ./a.out
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

typedef pair<int, int> p;
typedef long long ll;

const int mod = 1000000007;
const int inf = -1;

char maze[20][20];
int d[20][20];

int ans[20][20];

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int bfs(int w, int h, int sx, int sy)
{
    queue<p> que;
    rep(i, 20)
        rep(j, 20)
            d[i][j] = inf;
    d[sx][sy] = 0;
    que.push(p(sx, sy));
    while (que.size())
    {
        p pos = que.front();
        que.pop();
        rep(i, 4)
        {
            int nx = pos.first + dx[i];
            int ny = pos.second + dy[i];
            if (nx >= 0 && nx < w && ny >= 0 && ny < h && maze[nx][ny] != '#' && d[nx][ny] == inf)
            {
                que.push(p(nx, ny));
                d[nx][ny] = d[pos.first][pos.second] + 1;
            }
        }
    }
    int max = 0;
    rep(i, h)
    {
        rep(j, w)
        {
            if (max < d[j][i])
            {
                max = d[j][i];
            }
            //cout << d[i][j];
        }
        //cout << endl;
    }
    return max;
}

int main()
{
    int n, m;
    cin >> n >> m;
    rep(i, 20)
    {
        rep(j, 20)
        {
            ans[i][j] = -1;
            maze[i][j] = '#';
        }
    }
    rep(i, n)
    {
        rep(j, m)
        {
            char c;
            cin >> c;
            maze[j][i] = c;
        }
    }
    int res = 0;
    rep(i, n)
    {
        rep(j, m)
        {
            if (maze[j][i] == '#')
            {
            }
            else
            {
                ans[j][i] = bfs(m, n, j, i);
            }
            if (res < ans[j][i])
            {
                res = ans[j][i];
            }
            //cout << ans[i][j] << endl;
        }
    }
    cout << res << endl;
}
/*
012-1-1
1-1-1-1-1
234-1-1

123-1-1
0-1-1-1-1
123-1-1

234-1-1
1-1-1-1-1
012-1-1

345-1-1
2-1-1-1-1
123-1-1

8910-1-1
7-1-1-1-1
654-1-1
101-1-1
2-1-1-1-1
345-1-1
212-1-1
10-1-1-1
212-1-1
345-1-1
2-1-1-1-1
101-1-1
456-1-1
3-1-1-1-1
212-1-1
789-1-1
6-1-1-1-1
543-1-1
210-1-1
3-1-1-1-1
456-1-1
321-1-1
4-10-1-1
321-1-1
456-1-1
3-1-1-1-1
210-1-1
567-1-1
4-1-1-1-1
321-1-1
678-1-1
5-1-1-1-1
432-1-1
*/