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 <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <numeric>
#include <deque>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <limits>
#include <cassert>
#include <bitset>

#define for0(i, N)    for (int i = 0  ; i <  (N); ++i)
#define for1n(i, N)   for (int i = 1  ; i <= (N); ++i)
#define for1(i, N)    for (int i = 1  ; i <  (N); ++i)
#define forn(i, N)    for (int i = 0  ; i <= (N); ++i)
#define forx(i, N, x) for (int i = (x); i <  (N); ++i)
#define forl(i, N, x, a) for (int i = (x), a = 0; a < (N);  \
                              i = (i + 1) % (N), ++a)

template<size_t i>
std::bitset<i> operator++(std::bitset<i> b, int){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    return b = (unsigned long long)b + 1;
}

template<size_t i>
std::bitset<i> operator++(std::bitset<i> b){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    auto temp = b;
    b++;
    return temp;
}

template<size_t i>
std::bitset<i> operator--(std::bitset<i> b, int){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    return b = (unsigned long long)b - 1;
}

template<size_t i>
std::bitset<i> operator--(std::bitset<i> b){
    static_assert(i <= 64, "Cannot cast 64bit int.");
    auto temp = b;
    b--;
    return temp;
}

using namespace std;

typedef long long ll;
typedef long double ld;
typedef priority_queue<ll> pq;
typedef priority_queue<ll,vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef set<ll> sll;
typedef unordered_set<ll> usll;
typedef vector<ll> vll;
typedef vector<ld> vld;

struct arc{
    int to;
    int cost;
};

template<typename T>
struct node{
    T value;
    vector<arc> arcs;
    node(T v) : value(v){}

    node& operator= (T const& a){
        value = a;
        return *this;
    }
    operator T() const {
        return value;
    }

};

template<typename T>
class graph{
protected:
    vector<node<T>> nodes;
    int n;
public:

    graph(int N, T value) : nodes(N, value), n(N){};

    void addArc(int from, int to, int cost){
        assert(0 <= from && from <= n - 1 && 0 <= to && to <= n - 1);
        nodes[from].arcs.push_back({to,cost});
    }
    void addEdge(int a, int b, int cost){
        addArc(a, b, cost);
        addArc(b, a, cost);
    }
    node<T>& operator[](int index) {
        return nodes[index];
    }

    auto begin() { return nodes.begin(); }
    auto end()   { return nodes.end();   }

    auto const& getInstance(){
        return nodes;
    }

};

class bellmanford : public graph<int>{
    int N;
    int changed;

    void r(unordered_set<int> froms, int termination){
        bool c = false;
        unordered_set<int> f;
        for (auto& from : froms){
            for (auto& i : nodes[from].arcs) {
                if (nodes[i.to].value > i.cost + nodes[from].value) {
                    nodes[i.to].value = i.cost + nodes[from].value;
                    f.insert(i.to);
                    c = true;
                }
            }
        }
        // Terminated
        if (!c) return;

        // Negative loop?
        if(!termination){
            changed = c;
            return;
        }
        // continue
        r(move(f), termination-1);
    }

public:

    bellmanford(int N): graph::graph(N, std::numeric_limits<int>::max()),
                        N(N), changed(false){}

    bool run(int start){
        nodes[start].value = 0;
        r({start}, N-1);
        return changed;
    }

    int const& operator[] (int index){
        return nodes[index].value;
    }
    void reset(){
        for(auto &x : nodes){
            x.value = std::numeric_limits<int>::max();
        }
    }

};


int main(void){
    cin.tie(0); ios::sync_with_stdio(false);

    int h, w;

    cin >> h >> w;

    vector<vector<int>> meiro(h, vector<int>(w, -1));

    int roadcount = 0;
    for0(i, h)for0(j, w){
    newline:
        char c;
        cin >> c;
        if (c == '\n') { goto newline; }

        if (c == '.')
          meiro[i][j] = roadcount++;
    }

    bellmanford b(roadcount);

    for0(i, h)for0(j, w){
        auto &x = meiro[i][j];
        if (x == -1) continue;

        // up
        auto &up = meiro[i - 1][j];
        if (i - 1 >= 0 && up != -1) { b.addEdge(x, up, 1); }

        // down
        auto &down = meiro[i + 1][j];
        if (i + 1 < h && down != -1) { b.addEdge(x, down, 1); }

        // left
        auto &left = meiro[i][j - 1];
        if (j - 1 >= 0 && left != -1) { b.addEdge(x, left, 1); }

        // right
        auto &right = meiro[i][j + 1];
        if (j + 1 < w && right != -1) { b.addEdge(x, right, 1); }
    }

    int result = 0;
    for0(i, roadcount){
        b.run(i);
        for0(j, roadcount){
            result = max(result, b.getInstance()[j].value);
        }
        b.reset();
    }

    cout << result;

    return 0;
}