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: Runtime Error)

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <string>
#include <set>
#include <bitset>
#include <unordered_map>

using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
template <class T>
using Vvec = vector<vector<T> >;

class Edge{
public:
    int from, to;
    long long cost;
    Edge(): from(0), to(0), cost(0){};
    Edge(int u, int v, long long w): from(u), to(v), cost(w){};
    ~Edge(){};
};

void Warshall_Floyd(std::vector<Edge> &edge, int V,
    std::vector<std::vector<long long> > &d)
{
    int E = edge.size();
    const long long infty = 1e+16;

    // initialize
    for(int i=0; i<V; i++){
        for(int j=0; j<V; j++) d[i][j] = infty;
    }
    for(int i=0; i<E; i++){
        d[edge[i].from][edge[i].to] = edge[i].cost;
    }
    for(int i=0; i<V; i++) d[i][i] = 0;

    // dp
    for(int k=0; k<V; k++){
        for(int i=0; i<V; i++){
            for(int j=0; j<V; j++) d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]);
        }
    }
}

int main()
{
    int H, W;
    cin >> H >> W;
    Vvec<char> S(H, vector<char>(W));
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++) cin >> S[i][j];
    }

    vector<Edge> edge;
    for(int i=0; i<H; i++){
        for(int j=0; j<W-1; j++){
            if(S[i][j]=='.' && S[i][j+1]=='.'){
                edge.emplace_back(i*H+j, i*H+j+1, 1);
                edge.emplace_back(i*H+j+1, i*H+j, 1);
            }
        }
    }
    for(int i=0; i<H-1; i++){
        for(int j=0; j<W; j++){
            if(S[i][j]=='.' && S[i+1][j]=='.'){
                edge.emplace_back(i*H+j, (i+1)*H+j, 1);
                edge.emplace_back((i+1)*H+j, i*H+j, 1);
            }
        }
    }

    Vvec<ll> dist(H*W, vector<ll>(H*W));
    Warshall_Floyd(edge, H*W, dist);

    ll ans = 0;
    for(int i=0; i<H*W; i++){
        for(int j=0; j<H*W; j++){
            if(dist[i][j] < 1e9) ans = max(ans, dist[i][j]);
        }
    }

    cout << ans << endl;
}