// Slower C++ solution:
#include <string>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int H, W; vector<string> s;

int main() {

	ios::sync_with_stdio(false);

	cin >> H >> W; s.resize(H);

	for (int i = 0; i < H; i++) cin >> s[i];

	vector<pair<int, int> > v;

	for (int i = 0; i < H; i++) {

		for (int j = 0; j < W; j++) {

			if (s[i][j] == '.' || s[i][j] == '9') continue;

			int cnt = 0;

			for (int k = -1; k <= 1; k++) {

				for (int l = -1; l <= 1; l++) {

					int tx = j + l, ty = i + k;

					if (0 <= tx && tx < W && 0 <= ty && ty < H && s[ty][tx] == '.') cnt++;

				}

			}

			if (cnt >= s[i][j] - 48) v.push_back(make_pair(j, i));

		}

	}

	int ret = 0;

	for (; !v.empty(); ret++) {

		for (pair<int, int> i : v) s[i.second][i.first] = '.';

		vector<pair<int, int> > w;

		for (pair<int, int> i : v) {

			int sx = i.first, sy = i.second;

			for (int j = -1; j <= 1; j++) {

				for (int k = -1; k <= 1; k++) {

					int tx = sx + k, ty = sy + j;

					if (!(0 <= tx && tx < W && 0 <= ty && ty < H)) continue;

					if (s[ty][tx] == '.' || s[ty][tx] == '9') continue;

					int cnt = 0;

					for (int l = -1; l <= 1; l++) {

						for (int m = -1; m <= 1; m++) {

							int ux = tx + m, uy = ty + l;

							if (0 <= ux && ux < W && 0 <= uy && uy < H && s[uy][ux] == '.') cnt++;

						}

					}

					if (cnt >= s[ty][tx] - 48) w.push_back(make_pair(tx, ty));

				}

			}

		}

		sort(w.begin(), w.end());

		w.resize(unique(w.begin(), w.end()) - w.begin());

		v = w;

	}

	cout << ret << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.105688245980346x):
#include <cstdio>

#include <vector>

#include <algorithm>

using namespace std;

int H, W; char s[1009][1009]; bool vis[1009][1009];

int main() {

	scanf("%d%d", &H, &W);

	for (int i = 0; i < H; i++) scanf("%s", s[i]);

	vector<pair<int, int> > v;

	for (int i = 0; i < H; i++) {

		for (int j = 0; j < W; j++) {

			if (s[i][j] == '.' || s[i][j] == '9') continue;

			int cnt = 0;

			for (int k = -1; k <= 1; k++) {

				for (int l = -1; l <= 1; l++) {

					int tx = j + l, ty = i + k;

					if (0 <= tx && tx < W && 0 <= ty && ty < H && s[ty][tx] == '.') cnt++;

				}

			}

			if (cnt >= s[i][j] - 48) v.push_back(make_pair(j, i));

		}

	}

	int ret = 0;

	for (; !v.empty(); ret++) {

		for (pair<int, int> i : v) s[i.second][i.first] = '.';

		vector<pair<int, int> > w;

		for (pair<int, int> i : v) {

			int sx = i.first, sy = i.second;

			for (int j = -1; j <= 1; j++) {

				for (int k = -1; k <= 1; k++) {

					int tx = sx + k, ty = sy + j;

					if (!(0 <= tx && tx < W && 0 <= ty && ty < H)) continue;

					if (s[ty][tx] == '.' || s[ty][tx] == '9' || vis[ty][tx]) continue;

					int cnt = 0;

					for (int l = -1; l <= 1; l++) {

						for (int m = -1; m <= 1; m++) {

							int ux = tx + m, uy = ty + l;

							if (0 <= ux && ux < W && 0 <= uy && uy < H && s[uy][ux] == '.') cnt++;

						}

					}

					if (cnt >= s[ty][tx] - 48) w.push_back(make_pair(tx, ty)), vis[ty][tx] = true;

				}

			}

		}

		for (pair<int, int> i : w) vis[i.second][i.first] = false;

		v = w;

	}

	printf("%d\n", ret);

	return 0;

}