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: Time Limit Exceeded)

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>

#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;


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

class DMazeMaster {
public:

    vector<vector<char>> G;
    int H, W;
    set<pair<P, P>> found;

    int calc(int sx, int sy, int gx, int gy) {
        if (sx == gx && sy == gy) return 0;
        if (G[sx][sy] == '#' || G[gx][gy] == '#') return 0;

        P start = {sx, sy};
        P goal = {gx, gy};
        if (start > goal) swap(start, goal);
        if (found.count({start, goal})) return 0;
        found.insert({start, goal});

        queue<pair<P, int>> q;
        set<P> used;
        q.push({{sx, sy}, 0});
        int res = 0;
        while (!q.empty()) {
            auto kv = q.front();
            q.pop();
            P pos = kv.first;
            int d = kv.second;
            if (used.count(pos)) continue;
            used.insert(pos);
            if (pos.first == gx && pos.second == gy) {
                res = d;
                break;
            }
            REP(i, 4) {
                int nx = pos.first + dx[i];
                int ny = pos.second + dy[i];
                if (nx < 0 || ny < 0 || nx >= H || ny >= W) continue;
                if (G[nx][ny] == '#') continue;
                if (used.count({nx, ny})) continue;
                q.push({{nx, ny}, d + 1});
            }
        }
        return res;
    }

    void solve(std::istream& cin, std::ostream& cout) {
        cin >> H >> W;
        G = vector<vector<char>>(H, vector<char>(W));
        REP(i, H) REP(j, W) cin >> G[i][j];

        int res = 0;
        REP(si, H) {
            REP(sj, W) {
                REP(gi, H) {
                    REP(gj, W) {
                        res = max(res, calc(si, sj, gi, gj));
                    }
                }
            }
        }
        cout << res << endl;

    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    DMazeMaster solver;
    solver.solve(in, out);
    return 0;
}