#include <iostream>
#include <string>
#include <stack>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <stdlib.h>
#include <tuple>
#include <iomanip>
#include <iterator>
#include <stdio.h>
#include <fstream>
#include <time.h>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;

struct UnionFind {
    vector<int> parent;

    UnionFind(int N) : parent(N) {
        for (int i = 0; i < N; i++) {
            parent[i] = -1;
        }
    }

    int root(int i) {
        if (parent[i] < 0) {
            return i;
        }
        return (parent[i] = root(parent[i]));
    }

    bool unite(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        if (rx != ry) {
            parent[ry] += parent[rx];
            parent[rx] = ry;

            return true;
        }
        else {
            return false;
        }
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int treeSize(int x) {
        

        return -parent[root(x)];
    }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
    if (pow == 1) {
        return base;
    }
    else if (pow == 0) {
        return 1;
    }

    if (pow % 2 == 0) {
        auto temp = modpow(base, pow / 2, mod);
        return (temp * temp) % mod;
    }
    else {
        return base * modpow(base, pow - 1, mod) % mod;
    }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
    auto fermatDiv = modpow(Y, mod - 2, mod);

    return (X * fermatDiv) % mod;
}

bool IsPrime(long long N) {
    if (N == 1) {
        return false;
    }
    for (long long i = 2; i * i <= N; i++) {
        if (N % i == 0) {
            return false;
        }
    }

    return true;
}

bool IsPE(long long z) {
    for (long long i = 2; i <= z; i++) {
        if (z % i == 0 && IsPrime(i)) {
            long long test = i;
            while (test < z) {
                test *= i;
            }
            if (test == z) {
                return true;
            }
        }
    }

    return false;
}

long long  solve(long long N) {
    long long answer = 0;
    long long divider = 2;
    
    while (N >= divider) {
        if (N % divider == 0 && IsPE(divider)) {
            //cout << divider << endl;
            answer++;
            N /= divider;
        }
        
        divider++;
    }

    return answer;
}

vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({ a, ex });
    }

    // 最後に残った数について
    if (N != 1) res.push_back({ N, 1 });
    return res;
}

int H, W;

struct Edge {
    int from;
    int to;
public: Edge(int f, int t) {
        from = f;
        to = t;
    }
};

int main() {
    cin >> H >> W;
    vector<vector<int>> nodes(H * W);
    vector<bool> maze(H * W);
    vector<Edge> edges;

    for (int i = 0; i < H * W; i++) {
        maze[i] = false;
        nodes[i] = vector<int>(H * W);
        for (int j = 0; j < H * W; j++) {
            if (i == j) {
                nodes[i][j] = 0;
            }
            else {
                nodes[i][j] = -1;
            }
        }
    }

    for (int i = 0; i < H * W; i++) {
        char c;
        cin >> c;
        if (c == '.') {
            maze[i] = true;
            if (i % W != 0 && maze[i - 1]) {
                nodes[i][i - 1] = 1;
                nodes[i - 1][i] = 1;
                edges.push_back(Edge(i, i - 1));
            }
            if ((i + 1) % W != 0 && maze[i + 1]) {
                nodes[i][i + 1] = 1;
                nodes[i + 1][i] = 1;
                edges.push_back(Edge(i, i + 1));
            }
            if (i - W >= 0 && maze[i - W]) {
                nodes[i][i - W] = 1;
                nodes[i - W][i] = 1;
                edges.push_back(Edge(i, i - W));
            }
            if (i + W < H * W && maze[i + W]) {
                nodes[i][i + W] = 1;
                nodes[i + W][i] = 1;
                edges.push_back(Edge(i, i + W));
            }
        }
    }

    int edgeCount = edges.size();

    for (int k = 0; k < H * W; k++) {
        for (int i = 0; i < H * W; i++) {
            if (maze[i]) {
                for (int j = 0; j < edgeCount; j++) {
                    Edge e = edges[j];
                    if (i != e.from && nodes[i][e.from] != -1) {
                        if (nodes[i][e.to] == -1 || nodes[i][e.to] > nodes[i][e.from] + 1) {
                            nodes[i][e.to] = nodes[i][e.from] + 1;
                        }
                    }

                    if (i != e.to && nodes[i][e.to] != -1) {
                        if (nodes[i][e.from] == -1 || nodes[i][e.from] > nodes[i][e.to] + 1) {
                            nodes[i][e.from] = nodes[i][e.to] + 1;
                        }
                    }
                }
            }
        }
    }

    int max = -1;
    for (int i = 0; i < H * W; i++) {
        for (int j = 0; j < H * W; j++) {
            if (nodes[i][j] > max) {
                max = nodes[i][j];
            }
        }
    }

    cout << max << endl;
    return 0;
}

Measured memory_bytes: 896