#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <queue>
#include <ctime>
using namespace std;
template <class T> void checkmin(T &t,T x){if (x < t) t = x;}
template <class T> void checkmax(T &t,T x){if (x > t) t = x;}
template <class T> void _checkmin(T &t, T x){if (t == -1) t = x; if (x < t) t = x;}
template <class T> void _checkmax(T &t, T x){if (t == -1) t = x; if (x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long lld;
#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin();it != (v).end();it++)
#define DEBUG(a) cout << #a" = " << (a) << endl;
#define DEBUGARR(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }

const int N = 15;
const int px[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int py[9] = {-1,  0,  1,-1, 0, 1,-1, 0, 1};
int m, n;
lld Pow[16];
lld addup[N][N];
int num[N][N];
int ind[N][N];
bool useless[N][N];
char mat[N][N];
map <lld, int> f, g;
vector <PII> digit;

bool inRange(int x, int y) {
	return 0 <= x && x < m && 0 <= y && y < n;
}

bool valid(int x, int y, lld mask) {
	foreach (it, digit) {
		int w = mask / Pow[ind[it->first][it->second]] % 11;
		if (w > num[it->first][it->second]) return 0;
		if (w < num[it->first][it->second] && 
			(x > min(it->first + 1, m - 1) || 
			 (x == min(it->first + 1, m - 1) && y >= min(it->second + 1, n - 1)))) {
			return 0;
		}
	}
	return 1;
}

void update(lld mask, int x) {
	if (g.count(mask)) {
		checkmin(g[mask], x);
	} else {
		g[mask] = x;
	}
}

int main(){
#ifdef cwj
	freopen("in", "r", stdin);
#endif
	Pow[0] = 1;
	for (int i = 1; i < 16; i++) Pow[i] = Pow[i - 1] * 11;
	while (cin >> m >> n && m + n) {
		memset(num, 0xFF, sizeof(num));
		int cnt = 0;
		digit.clear();
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				cin >> mat[i][j];
				if (mat[i][j] >= '0' && mat[i][j] <= '9') {
					num[i][j] = mat[i][j] - '0';
					ind[i][j] = cnt++;
					digit.push_back(make_pair(i, j));
				}
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (mat[i][j] != '.') {
					useless[i][j] = 1;
					addup[i][j] = 0;
					for (int o = 0; o < 9; o++) {
						if (inRange(i + px[o], j + py[o])) {
							if (num[i + px[o]][j + py[o]] > 0) {
								useless[i][j] = 0;
								addup[i][j] += Pow[ind[i + px[o]][j + py[o]]];
							} else if (num[i + px[o]][j + py[o]] == 0) {
								useless[i][j] = 1;
								break;
							}
						}
					}
				} else {
					useless[i][j] = 1;
				}
			}
		}
		f.clear();
		f[0] = 0;
		if (!useless[0][0]) {
			if (valid(0, 0, addup[0][0])) {
				f[addup[0][0]] = 1;
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (i + 1 == m && j + 1 == n) continue;
				g.clear();
				int _i = j + 1 == n ? i + 1 : i;
				int _j = j + 1 == n ? 0 : j + 1;
				foreach (it, f) {
					if (valid(_i, _j, it->first)) {
						update(it->first, it->second);
					}
					if (!useless[_i][_j]) {
						if (valid(_i, _j, it->first + addup[_i][_j])) {
							update(it->first + addup[_i][_j], it->second + 1);
						}
					}
				}
				f = g;
			}
		}
		long long goal = 0;
		foreach (it, digit) {
			goal += num[it->first][it->second] * Pow[ind[it->first][it->second]];
		}
		printf("%d\n", f[goal]);
	}
}
