#include "bits/stdc++.h"

using ll = long long;

using namespace std;
#define int ll

// 0 -> a-1
#define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++)
// a -> b-1
#define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
// a-1 -> 0
#define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--)
// a-1 -> b
#define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
// next_permutation(all(v))
#define PERM(v) next_permutation(all(v))
/*sort(all(v));
(v).erase(unique(all(v)), v.end())*/
#define UNIQUE(v)                                                              \
	sort(all(v));                                                              \
	(v).erase(unique(all(v)), v.end())
#define CIN(type, x)                                                           \
	type x;                                                                    \
	cin >> x
#define YES(f)                                                                 \
	if ((f)) {                                                                 \
		cout << "YES" << endl;                                                 \
	} else {                                                                   \
		cout << "NO" << endl;                                                  \
	}
#define Yes(f)                                                                 \
	if ((f)) {                                                                 \
		cout << "Yes" << endl;                                                 \
	} else {                                                                   \
		cout << "No" << endl;                                                  \
	}
#define MINV(v) min_element(all(v))
#define MAXV(v) max_element(all(v))
#define MIN3(a, b, c) min(min(a, b), c)
#define MIN4(a, b, c, d) min(MIN3(a, b, c), d)
#define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e)
#define MIN6(a, b, c, d, e, f) min(MIN5(a, b, c, d, e), f)
#define MAX3(a, b, c) max(max(a, b), c)
#define MAX4(a, b, c, d) max(MAX3(a, b, c), d)
#define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e)
#define MAX6(a, b, c, d, e, f) max(MAX5(a, b, c, d, e), f)
// b is [a, c)
#define RANGE(a, b, c) ((a) <= (b) && (b) < (c))
// c is [a, e) && d is [b, f)
#define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f)))
#define chmin(a, b) a = min(a, (b))
#define chmin3(a, b, c) a = MIN3(a, (b), (c))
#define chmin4(a, b, c, d) a = MIN4(a, (b), (c), (d))
#define chmin5(a, b, c, d, e) a = MIN5(a, (b), (c), (d), (e))
#define chmin6(a, b, c, d, e, f) a = MIN6(a, (b), (c), (d), (e), (f))
#define chmax(a, b) a = max(a, (b))
#define chmax3(a, b, c) a = MAX3(a, (b), (c))
#define chmax4(a, b, c, d) a = MAX4(a, (b), (c), (d))
#define chmax5(a, b, c, d, e) a = MAX5(a, (b), (c), (d), (e))
#define chmax6(a, b, c, d, e, f) a = MAX6(a, (b), (c), (d), (e), (f))
#define fcout cout << fixed << setprecision(12)
#define RS resize
#define CINV(v, N)                                                             \
	do {                                                                       \
		v.RS(N);                                                               \
		rep (i, N)                                                             \
			cin >> v[i];                                                       \
	} while (0);

#define RCINV(v, N)                                                            \
	do {                                                                       \
		v.RS(N);                                                               \
		rrep (i, N)                                                            \
			cin >> v[i];                                                       \
	} while (0);
#define MOD 1000000007

template <class T> inline T GET()
{
	T x;
	cin >> x;
	return x;
}

void init();
void solve();

signed main()
{
	init();
	solve();
}

struct Position_grid {
	int r;
	int c;
	static int H;
	static int W;
	Position_grid(int a, int b) : r(a), c(b) {}
	Position_grid(int a) : r(a / W), c(a % W) {}
	operator int() const {
		return r * W + c;
	}
};

using dijkstra_cost = long long;
using position_t = Position_grid;

template<class V, class E>
struct Graph
{
	int sz;
	vector<V> v;
	vector<vector<E>> e;
	Graph(int n) : sz(n), v(n), e(n) {}
	template<class... Args>
	inline void assign_vertex(position_t pos, Args... args) {
		v[pos] = V(args...);
	}
	template<class... Args>
	inline void add_edge(position_t pos, Args... args) {
		e[pos].emplace_back(args...);
	}
	inline int size() {
		return sz;
	}
};

template<class T>
struct Vertex_Cost
{
	T cost;
	Vertex_Cost(T v1) : cost(v1) {}
	Vertex_Cost() {}
	inline bool operator<(const Vertex_Cost r) const {
		return cost < r.cost;
	}
	inline bool operator>(const Vertex_Cost r) const {
		return r < *this;
	}
	inline bool operator>=(const Vertex_Cost r) const {
		return !(*this < r);
	}
	inline bool operator<=(const Vertex_Cost r) const {
		return !(r < *this);
	}
	inline bool operator==(const Vertex_Cost r) const {
		return !(r < *this) && !(*this < r);
	}
	inline bool operator!=(const Vertex_Cost r) const {
		return (r < *this) || (*this < r);
	}
};
using vertex = Vertex_Cost<dijkstra_cost>;

template<class T>
struct Edge_Cost
{
	position_t to;
	T cost;
	Edge_Cost(position_t t, T c) : to(t), cost(c) {}
	Edge_Cost(int t, T c) : to(t), cost(c) {}
	Edge_Cost() {}
	inline bool operator<(const Edge_Cost rhs) const {
		return cost < rhs.cost;
	}
	inline bool operator>(const Edge_Cost rhs) const {
		return rhs < *this;
	}
	inline bool operator>=(const Edge_Cost rhs) const {
		return !(*this < rhs);
	}
	inline bool operator<=(const Edge_Cost rhs) const {
		return !(*this > rhs);
	}
	inline bool operator==(const Edge_Cost rhs) const {
		return *this <= rhs && *this >= rhs;
	}
	inline bool operator!=(const Edge_Cost rhs) const {
		return !(*this == rhs);
	}
};
using edge = Edge_Cost<dijkstra_cost>;
using graph = Graph<vertex, edge>;

template<class T, class V, class E>
void Dijkstra(Graph<V, E>& G, int s, T INF_COST)
{
	auto &v = G.v;
	auto &e = G.e;
	priority_queue<E, vector<E>, greater<E>> q;
	rep(i, G.size()) {
		if (G.v[i].cost == 0) {
			q.emplace(position_t(i), 0);
			G.v[i] = INF_COST;
		}
	}
	while (!q.empty()) {
		auto a = q.top();
		q.pop();
		if (a.cost >= v[a.to].cost) continue;
		v[a.to].cost = a.cost;
		for (auto& p : e[a.to]) {
			if (p.cost == INF_COST) continue;
			if (a.cost + p.cost < v[p.to].cost) q.emplace(p.to, a.cost + p.cost);
		}
	}
}

int Position_grid::H;
int Position_grid::W;

void init()
{
}

const int INF = 1e9;

void solve()
{
	Position_grid ps(2, 4);
	int H, W;
	cin >> H >> W;
	ps.H = H;
	ps.W = W;
	graph G(ps.H * ps.W);
	rep(i, H) {
		string s;
		cin >> s;
		rep(j, W) {
			if (s[j] == '#') {
				G.assign_vertex(Position_grid(i, j), 0);
			} else {
				G.assign_vertex(Position_grid(i, j), INF);
			}
			int dr[] = {-1, 0, 1, 0};
			int dc[] = { 0, 1, 0,-1};
			rep(k, 4) {
				int nr = i + dr[k];
				int nc = j + dc[k];
				if (RANGE2D(0, 0, nr, nc, H, W)) {
					G.add_edge(Position_grid(i, j), Position_grid(nr, nc), 1);
					G.add_edge(Position_grid(nr, nc), Position_grid(i, j), 1);
				}
			}
		}
	}
	Dijkstra(G, 0, INF);
	int res = 0;
	for (auto c : G.v) chmax(res, c.cost);
	cout << res << endl;
}



Measured memory_bytes: 333920