#pragma GCC diagnostic ignored "-Wunused-variable" 
#include <bits/stdc++.h>
using namespace std;

#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/range/adaptor/reversed.hpp>
using namespace boost;
using ml = boost::multiprecision::cpp_int;
using md = boost::multiprecision::cpp_dec_float_100;
#define forir(i, ...) if(ll i=(ll)v.size())for(__VA_ARGS__)if(i--,1)
#define eachr(i, e, c) forir(i, const auto &e: adaptors::reverse(c))
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutd cout << fixed << setprecision(10)  // cout double
#define output(x) do{cout<<x; return 0;}while(1) // cout return
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...) if (ll i = -1) for(__VA_ARGS__) if (i++, 1)
#define each(i, e, c) fori (i, auto& e: c)       // for each
/***** const value *****/
#define llong_max 9223372036854775807            // 9 * 10^18
#define ldbl_max 1.79769e+308                    // 1.7 * 10^308
#define pi 3.1415926535897932                    // 3.14 ...
/***** lambda *****/
auto Ceil = [] // if (a % b != 0) return a / b + 1;
(auto x) { return (ll)ceil(x); };
auto Count = [] // long long count value
(auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
(auto c) { return (ll)c - (ll)'0'; };
auto LtoC = [] // number to char
(auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
(auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
(auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
(auto n) { return (ll)pow(10, n); };
auto Size = []  // long long collection size
(auto& c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
(auto b, auto e) { return accumulate(b, e, 0LL); };

/***** template *****/
template <class T> void MakeVVT
(ll ys, ll xs, vvt<T>& v, T fill = T())
{	// vector<vector<T>> resize + fill
	v.resize(ys); rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T> void MakeVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fill = T())
{	// vector<vector<vector<T>>> resize + fill
	v.resize(zs); rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T> void InputVT
(ll xs, vt<T>& v, T fix = T())
{	// input vector<T> (T != struct) + fix
	v.resize(xs); rep(i, 0, xs) { cin >> v[i]; v[i] += fix; }
}
template <class T> void InputVVT
(ll ys, ll xs, vvt<T>& v, T fix = T())
{	// input vector<vector<T>> (T != struct) + fix
	MakeVVT(ys, xs, v, fix);
	rep(y, 0, ys) rep(x, 0, xs) { cin >> v[y][x]; v[y][x] += fix; }
}
template <class T> void InputVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fix = T())
{	// input vector<vector<vector<T>>> (T != struct) + fix
	v.resize(zs); rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget
{
	/***** グリッドグラフ変換ライブラリ *****/

	struct GT_NyaaGridGraph
	{
		// 頂点indexに対応する座標を格納する配列
		vector<pair<long long, long long>> gtable;
		// 座標に対応する頂点index格納する配列(初期化は-1)
		vector<vector<long long>> vtable;

		/**
		@brief 上下左右4マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param mg 隣接行列
		@param wall 移動不可のグリッド値
		@note
		 上下左右4マスのグリッドで値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert4
		(const vector<vector<T>>& grid, vector<vector<long long>>& mg, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			mg.clear();
			mg.resize(vmax);
			for (auto& e : mg) e.resize(vmax, LLONG_MAX);
			vector<vector<long long>> graph(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					mg[vtable[y][x]][vtable[y][x]] = 0;
					if (0 <= y - 1 && vtable[y - 1][x] != -1)
						mg[vtable[y][x]][vtable[y - 1][x]] = 1;
					if (x + 1 < (long long)grid[y].size() && vtable[y][x + 1] != -1)
						mg[vtable[y][x]][vtable[y][x+1]] = 1;
					if (y + 1 < (long long)grid.size() && vtable[y + 1][x] != -1)
						mg[vtable[y][x]][vtable[y+1][x]] = 1;
					if (0 <= x - 1 && vtable[y][x - 1] != -1)
						mg[vtable[y][x]][vtable[y][x - 1]] = 1;
				}
			}
		}

		/**
		@brief 隣接9マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param graph 出力グラフ
		@param wall 移動不可のグリッド値
		@note
		 中央を含む隣接9マスの座標で値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert9
		(const vector<vector<T>>& grid, vector<vector<long long>>& graph, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			graph.clear();
			graph.resize(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					for (long long y3 = y - 1; y3 < y + 2; y3++)
					{
						if (y3 < 0 || (long long)grid.size() <= y3) continue;
						for (long long x3 = x - 1; x3 < x + 2; x3++)
						{
							if (x3 < 0 || (long long)grid[y3].size() <= x3) continue;
							if (vtable[y3][x3] == -1) continue;
							graph[vtable[y][x]].push_back(vtable[y3][x3]);
						}
					}
				}
			}
		}

		/**
		@brief 隣接9マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param wlg 重み付き隣接リスト
		@param wall 移動不可のグリッド値
		@note
		 中央を含む隣接9マスの座標で値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert9
		(const vector<vector<T>>& grid, vector<vector<pair<long long, long long>>>& wlg, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			vector<vector<long long>> graph(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					for (long long y3 = y - 1; y3 < y + 2; y3++)
					{
						if (y3 < 0 || (long long)grid.size() <= y3) continue;
						for (long long x3 = x - 1; x3 < x + 2; x3++)
						{
							if (x3 < 0 || (long long)grid[y3].size() <= x3) continue;
							if (vtable[y3][x3] == -1) continue;
							graph[vtable[y][x]].push_back(vtable[y3][x3]);
						}
					}
				}
			}

			wlg.clear();
			wlg.resize(graph.size());
			for (long long i = 0; i < (long long)graph.size(); i++)
			{
				for (long long j = 0; j < (long long)graph[i].size(); j++)
				{
					wlg[i].push_back({ graph[i][j],1 });
				}
			}
		}

		/**
		@brief 上下左右5マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param graph 出力グラフ
		@param wall 移動不可のグリッド値
		@note
		 中央を含む上下左右5マスのグリッドで値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert5
		(const vector<vector<T>>& grid, vector<vector<long long>>& graph, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			graph.clear();
			graph.resize(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					if (0 <= y - 1 && vtable[y - 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y - 1][x]);
					if (x + 1 < (long long)grid[y].size() && vtable[y][x + 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x + 1]);
					if (y + 1 < (long long)grid.size() && vtable[y + 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y + 1][x]);
					if (0 <= x - 1 && vtable[y][x - 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x - 1]);
				}
			}
		}

		/**
		@brief 上下左右5マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param lg 重み付き出力グラフ
		@param wall 移動不可のグリッド値
		@note
		 中央を含む上下左右5マスのグリッドで値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert5
		(const vector<vector<T>>& grid, vector<vector<pair<long long, long long>>>& lg, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			vector<vector<long long>> graph(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					if (0 <= y - 1 && vtable[y - 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y - 1][x]);
					if (x + 1 < (long long)grid[y].size() && vtable[y][x + 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x + 1]);
					if (y + 1 < (long long)grid.size() && vtable[y + 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y + 1][x]);
					if (0 <= x - 1 && vtable[y][x - 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x - 1]);
				}
			}

			lg.clear();
			lg.resize(graph.size());
			for (long long i = 0; i < (long long)graph.size(); i++)
			{
				for (long long j = 0; j < (long long)graph[i].size(); j++)
				{
					lg[i].push_back({ graph[i][j],1 });
				}
			}
		}

		// 頂点vと対応する座標を取得する関数
		pair<long long, long long> GetGrid(long long v) { return gtable[v]; }
		// 座標(y,x)と対応する頂点indexを取得する関数
		long long GetVertex(long long y, long long x)
		{	// 頂点が存在しない(移動不可座標)またはグリッド範囲外のとき-1を返す
			if (y < 0 || (long long)vtable.size() <= y || x < 0 || (long long)vtable[y].size() <= x) return -1;
			return vtable[y][x];
		}
	};
}

namespace NyaGadget
{
	/***** ワーシャルフロイド法 *****/

	struct GT_WarshallFloyd
	{
		/**
		@brief 全点対間最短路を求める
		@param mg 重み付きグラフ(隣接行列)
		@param res 最小距離
		@note
		 負の閉路がある場合falseを返す。計算量 O(V^3)
		**/
		bool Run
		(const vector<vector<long long>>& mg, vector<vector<long long>>& res)
		{
			res = mg;
			long long vmax = (long long)mg.size();
			const long long inf = LLONG_MAX;
			for (long long k = 0; k < vmax; k++)
			{
				for (long long i = 0; i < vmax; i++)
				{
					if (res[i][k] == inf) continue;
					for (long long j = 0; j < vmax; j++)
					{
						if (res[k][j] == inf) continue;
						res[i][j] = min(res[i][j], res[i][k] + res[k][j]);
					}
				}
			}
			for (long long i = 0; i < vmax; i++)
				if (res[i][i] < 0) return false;
			return true;
		}
	};
}

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
//using mll = NT_ModLL< 1000000007 >;
//using mll = NT_ModLL< 998244353 >;

int main(void)
{
	ll H, W; cin >> H >> W;
	vvt<char> grid; InputVVT(H, W, grid);

	vector<vector<long long>> mg;
	GT_NyaaGridGraph gg; gg.Convert4(grid, mg);

	vector<vector<long long>> res;
	GT_WarshallFloyd wf;
	wf.Run(mg, res);

	ll ans = 0;
	rep(y, 0, Size(res)) rep(x, 0, Size(res))
	{
		ans = max(ans, res[y][x]);
	}
	cout << ans;
	return 0;
}

Measured memory_bytes: 2816