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;
#define REP(i, m, n) for(int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define dup(x, y) (((x) + (y)-1) / (y))
#define PI 3.14159265359
typedef long long LL;
const LL MOD = 1e9 + 7;
const LL LLINF = 1LL << 60;
const int INF = 1 << 30;

class UnionFind {
  public:
    // rank:それぞれの木の深さ
    vector<int> rank;
    // p:ポインタ的な配列
    vector<int> p;
    UnionFind() {}
    UnionFind(int size) {
        rank.resize(size, 0);
        p.resize(size, 0);

        //最初は、全ての値は別の木に属するようにする
        //{0},{1},{2},...といった数字一つのみを持つ木が作られる
        for(int i = 0; i < size; i++) {
            makeSet(i);
        }
    };

    // ただ一つの要素xを持つ木を作成する
    void makeSet(int x) {
        p[x] = x;
        rank[x] = 0;
    }

    // 要素xが属する木のルートノードの値を求める
    int findSet(int x) {
        // xがルートノードでないならポインタがさしている先を再帰的に探索する
        //ルートノードならreturnする
        if(x != p[x]) {
            //ここで最終的に返ってくるのはルートノードの値であるため、探索中に経由したすべてのノードのポインタはルートノードへとつながる。
            p[x] = findSet(p[x]);
        }
        return p[x];
    }

    // xとyが同じ木に属しているか判定する
    bool isSame(int x, int y) { return findSet(x) == findSet(y); }

    // xを要素に持つ木とyを要素に持つ木を統合する
    void unite(int x, int y) { link(findSet(x), findSet(y)); }

  private:
    // xを根に持つ木とyを根に持つ木を統合する
    void link(int x, int y) {
        //なるべく木が深くならないように、rankが小さい方をrankが大きい方へ統合する
        if(rank[x] > rank[y]) {
            p[y] = x;
        } else {
            p[x] = y;
            //木の深さが同じだったらどうしても一段深くなってしまう
            if(rank[x] == rank[y]) {
                rank[y]++;
            }
        }
    }
};

const int MOD_NUM = 2;
// base : 9973
class RollingHash {
    vector<LL> hash[MOD_NUM];
    vector<LL> mod = {999999937LL, 1000000007LL};
    LL base = 9973;

  public:
    RollingHash(string s) { initialize(s); }

    vector<LL> getHashNum(int left, int right) {
        vector<LL> res(MOD_NUM);
        for(int i = 0; i < MOD_NUM; i++) {
            res[i] =
                ((hash[i][right] -
                  hash[i][left] * this->mpower(base, right - left, mod[i])) %
                     mod[i] +
                 mod[i]) %
                mod[i];
        }

        return res;
    }

  private:
    void initialize(string s) {
        for(int i = 0; i < MOD_NUM; i++) {
            hash[i].assign(s.size() + 1, 0);
            for(int j = 0; j < s.size(); j++) {
                hash[i][j + 1] = (hash[i][j] * base + s[j]) % mod[i];
            }
        }
    }

    LL mpower(LL x, LL n, LL mod) {
        if(n == 0) {
            return 1;
        } else if(n % 2 == 0) {
            return mpower(x * x % mod, n / 2, mod);
        } else {
            return x * mpower(x, n - 1, mod) % mod;
        }
    }
};

template <class T> inline void chmax(T &a, T b) {
    if(a < b) {
        a = b;
    }
}
template <class T> inline void chmin(T &a, T b) {
    if(a > b) {
        a = b;
    }
}
template <class T> inline T gcd(T x, T y) {
    if(y == 0) {
        return x;
    } else if(x == 0) {
        return y;
    }
    return gcd(y, x % y);
}
template <class T> inline T lcm(T x, T y) { return x / gcd(x, y) * y; }
template <class T> inline void print_vector(vector<T> vec) {
    for(int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    cout << endl;
}
template <class T> inline T mpower(T x, T n) {
    if(n == 0) {
        return 1;
    } else if(n % 2 == 0) {
        return mpower(x * x % MOD, n / 2);
    } else {
        return x * mpower(x, n - 1) % MOD;
    }
}

LL mfrac(LL x) {
    LL res = 1;
    for(LL i = x; i >= 1; i--) {
        res *= i;
        res %= MOD;
    }
    return res;
}
LL mcomb(LL n, LL r) {

    if(n == 0 and r == 0) {
        return 1;
    } else if(n < r || n < 0) {
        return 0;
    }

    LL res = mfrac(n) * mpower(mfrac(r), MOD - 2) % MOD;
    res *= mpower(mfrac(n - r), MOD - 2) % MOD;

    return res;
}

vector<LL> make_divisor(LL x) {
    vector<LL> res;
    for(LL i = 1; i * i <= x; i++) {
        if(x % i == 0) {
            res.push_back(i);
            if(i * i != x) {
                res.push_back(x / i);
            }
        }
    }
    return res;
}

template <class T> vector<T> eratosthenes(T x) {
    vector<T> is_prime_number(x + 1, 1);
    is_prime_number[0] = 0;
    is_prime_number[1] = 0;

    for(T i = 2; i * i <= x; i++) {
        for(T j = 2; i * j <= x; j++) {
            is_prime_number[i * j] = 0;
        }
    }

    vector<T> prime_numbers;
    for(T i = 0; i < is_prime_number.size(); i++) {
        if(is_prime_number[i]) {
            prime_numbers.push_back(i);
        }
    }

    return prime_numbers;
}

const int MAX = 450;
// dpテーブル
vector<vector<int>> dp(MAX, vector<int>(MAX, INF));

//引数:n = 頂点の数
void warshall_floyd(int n) {
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            if(dp[i][k] == INF)
                continue;
            for(int j = 1; j <= n; j++) {
                if(dp[k][j] == INF)
                    continue;
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }
    }
}

int main(void) {
    int H, W;
    cin >> H >> W;

    vector<vector<char>> grid(H + 2, vector<char>(W + 2));
    for(int y = 1; y <= H; y++) {
        string L;
        cin >> L;
        for(int x = 1; x <= W; x++) {
            grid[y][x] = L[x - 1];
        }
    }

    for(int y = 1; y <= H; y++) {
        for(int x = 1; x <= W; x++) {
            if(grid[y][x] == '#'){
                continue;
            }

            int current_node = W * (y - 1) + x;
            dp[current_node][current_node] = 0;
            if(grid[y - 1][x] == '.') {
                dp[current_node][current_node - W] = 1;
            }
            if(grid[y + 1][x] == '.') {
                dp[current_node][current_node + W] = 1;
            }
            if(grid[y][x - 1] == '.') {
                dp[current_node][current_node - 1] = 1;
            }
            if(grid[y][x + 1] == '.') {
                dp[current_node][current_node + 1] = 1;
            }
        }
    }

    warshall_floyd(H * W);

    int ans = 0;
    rep(i, MAX) {
        rep(j, MAX) {
            if(dp[i][j] == INF)
                continue;
            else
                chmax(ans, dp[i][j]);
        }
    }
    cout << ans << endl;

    return 0;
}