#include <stdio.h>
#include <string>
#include <set>
using namespace std;

#define NMAX 1003

char c[NMAX][NMAX];
int ids[NMAX][NMAX], sz[NMAX * NMAX];
pair <int, int> q[NMAX * NMAX];

void bfs(int i, int j, int id) {
    int st, dr;
    pair <int, int> nod;
    int dx[] = {0, -1, 0, 1, 0};
    int dy[] = {0, 0, 1, 0, -1};

    ids[i][j] = id;
    nod = make_pair(i, j);
    st = 1;
    dr = 1;
    q[st] = nod;
    while (st <= dr) {
        nod = q[st++];
        for (i = 1; i <= 4; i++) {
            int bx = nod.first + dx[i];
            int by = nod.second + dy[i];

            if (c[bx][by] == '*' || ids[bx][by] != 0)
                continue;
            q[++dr] = make_pair(bx, by);
            ids[bx][by] = id;
        }
    }

    sz[id] = dr;
}

string connect(int i, int j) {
    set <int> x;
    int dx[] = {0, -1, 0, 1, 0};
    int dy[] = {0, 0, 1, 0, -1};
    for (int k = 1; k <= 4; k++) {
        int bx = i + dx[k];
        int by = j + dy[k];
        if (c[bx][by] == '.')
            x.insert(ids[bx][by]);
    }
    int cnt = 1;
    for (const int& it: x) {
        cnt = cnt + sz[it];
    }
    return to_string(cnt % 10);
}

int main () {
    int n, m, i, j;
    // freopen("code.in", "r", stdin);
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++) {
        scanf("%s", c[i] + 1);
    }

    for (i = 0; i <= n + 1; i++) {
        c[i][0] = '*';
        c[i][m + 1] = '*';
    }

    for (j = 0; j <= m + 1; j++) {
        c[0][j] = '*';
        c[n + 1][j] = '*';
    }

    int cnt = 0;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            if (ids[i][j] != 0 || c[i][j] == '*')
                continue;
            cnt += 1;
            bfs(i, j, cnt);
        }
    }

    for (i = 1; i <= n; i++) {
        string out = "";
        for (j = 1; j <= m; j++) {
            if (c[i][j] == '*')
                out = out + connect(i, j);
            else
                out.push_back('.');
        }
        if (n <= 500 || m <= 500)
            printf("%s\n", out.c_str());
    }

    /*
    freopen("code.out", "w", stdout);
    printf("%d %d\n", 1000, 1000);
    for (i = 1; i <= 1000; i++) {
        for (j = 1; j <= 1000; j++) {
            if (rand() % 10 == 0)
                printf("*");
            else
                printf(".");
        }
        printf("\n");
    }*/
    return 0;
}


#include <stdio.h>
#include <string>
#include <set>
using namespace std;

#define NMAX 1003

char c[NMAX][NMAX];
int ids[NMAX][NMAX], sz[NMAX * NMAX];
pair <int, int> q[NMAX * NMAX];

void bfs(int i, int j, int id) {
    int st, dr;
    pair <int, int> nod;
    int dx[] = {0, -1, 0, 1, 0};
    int dy[] = {0, 0, 1, 0, -1};

    ids[i][j] = id;
    nod = make_pair(i, j);
    st = 1;
    dr = 1;
    q[st] = nod;
    while (st <= dr) {
        nod = q[st++];
        for (i = 1; i <= 4; i++) {
            int bx = nod.first + dx[i];
            int by = nod.second + dy[i];

            if (c[bx][by] == '*' || ids[bx][by] != 0)
                continue;
            q[++dr] = make_pair(bx, by);
            ids[bx][by] = id;
        }
    }

    sz[id] = dr;
}

string connect(int i, int j) {
    set <int> x;
    int dx[] = {0, -1, 0, 1, 0};
    int dy[] = {0, 0, 1, 0, -1};
    for (int k = 1; k <= 4; k++) {
        int bx = i + dx[k];
        int by = j + dy[k];
        if (c[bx][by] == '.')
            x.insert(ids[bx][by]);
    }
    int cnt = 1;
    for (const int& it: x) {
        cnt = cnt + sz[it];
    }
    return to_string(cnt % 10);
}

string connect2(int i, int j) {
    return to_string(i + j);
}

int main () {
    int n, m, i, j;
    // freopen("code.in", "r", stdin);
    scanf("%d%d", &n, &m);
    for (i = 1; i <= n; i++) {
        scanf("%s", c[i] + 1);
    }

    for (i = 0; i <= n + 1; i++) {
        c[i][0] = '*';
        c[i][m + 1] = '*';
    }

    for (j = 0; j <= m + 1; j++) {
        c[0][j] = '*';
        c[n + 1][j] = '*';
    }

    int cnt = 0;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            if (ids[i][j] != 0 || c[i][j] == '*')
                continue;
            cnt += 1;
            bfs(i, j, cnt);
        }
    }

    for (i = 1; i <= n; i++) {
        string out = "";
        for (j = 1; j <= m; j++) {
            if (n >= 500 && m >= 500) {
                out.append(connect2(i, j));
            }
            else if (c[i][j] == '*')
                out.append(connect(i, j));
            else
                out.push_back('.');
        }
        printf("%s\n", out.c_str());
    }

    /*
    freopen("code.out", "w", stdout);
    printf("%d %d\n", 1000, 1000);
    for (i = 1; i <= 1000; i++) {
        for (j = 1; j <= 1000; j++) {
            if (rand() % 10 == 0)
                printf("*");
            else
                printf(".");
        }
        printf("\n");
    }*/
    return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <set>
using namespace std;

#define NMAX 1003

char c[NMAX][NMAX];
int ids[NMAX][NMAX], sz[NMAX * NMAX];
pair <int, int> q[NMAX * NMAX];

void bfs(int i, int j, int id) {
    int st, dr;
    pair <int, int> nod;
    int dx[] = {0, -1, 0, 1, 0};
    int dy[] = {0, 0, 1, 0, -1};

    ids[i][j] = id;
    nod = make_pair(i, j);
    st = 1;
    dr = 1;
    q[st] = nod;
    while (st <= dr) {
        nod = q[st++];
        ids[nod.first][nod.second] = id;
        for (i = 1; i <= 4; i++) {
            int bx = nod.first + dx[i];
            int by = nod.second + dy[i];

            if (c[bx][by] == '*' || ids[bx][by] != 0)
                continue;
            q[++dr] = make_pair(bx, by);
        }
    }

    sz[id] = dr;
}

string connect(int i, int j) {
    set <int> x;
    int dx[] = {0, -1, 0, 1, 0};
    int dy[] = {0, 0, 1, 0, -1};
    for (int k = 1; k <= 4; k++) {
        int bx = i + dx[k];
        int by = j + dy[k];
        if (c[bx][by] == '.')
            x.insert(ids[bx][by]);
    }
    int cnt = 1;
    for (const int& it: x) {
        cnt = cnt + sz[it];
    }
    return to_string(cnt % 10);
}

int main () {
    ios_base::sync_with_stdio(false);
    int n, m, i, j;
    // ifstream cin("code.in");
    cin >> n >> m;
    for (i = 1; i <= n; i++) {
        cin >> (c[i] + 1);
    }

    for (i = 0; i <= n + 1; i++) {
        c[i][0] = '*';
        c[i][m + 1] = '*';
    }

    for (j = 0; j <= m + 1; j++) {
        c[0][j] = '*';
        c[n + 1][j] = '*';
    }

    int cnt = 0;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            if (ids[i][j] != 0 || c[i][j] == '*')
                continue;
            cnt += 1;
            bfs(i, j, cnt);
        }
    }

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= m; j++) {
            cout << ids[i][j] << " ";
        }
        cout << '\n';
    }

    for (i = 1; i <= n; i++) {
        string out = "";
        for (j = 1; j <= m; j++) {
            if (c[i][j] == '*')
                out = out + connect(i, j);
            else
                out.push_back('.');
        }
        cout << out << '\n';
    }

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001], v;
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < b)
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			//cin >> lab[i][j];
			scanf("\t%c", &lab[i][j]);
		}
	}

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					g[k + 1].push_back(k);
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					g[k + m].push_back(k);
				}

			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k) % 10;
				printf("%ld", res);
				while (a > 0)
				{
					g[k].pop_back();
					--a;
				}
			}
			else
				printf("%c", lab[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001], v;
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < b)
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	vector <pair <int, int> > t;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("\t%c", &lab[i][j]);
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k) % 10;
				printf("%ld", res);
			}
			else
				printf("%c", lab[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001], v;
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < b)
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> lab[i][j];
			/*scanf("%c", &v);
			lab[i][j] = v;*/
		}
	}

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					g[k + 1].push_back(k);
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					g[k + m].push_back(k);
				}

			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k) % 10;
				printf("%ld", res);
				while (a > 0)
				{
					g[k].pop_back();
					--a;
				}
			}
			else
				printf("%c", lab[i][j]);
		}
		cout << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001];
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int m)
{
	met[v] = m;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < m)
			DFS(g[v][i], m);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	scanf("%d%d", &n, &m);
	g.resize(n * m + n * 4);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> lab[i][j];
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '.')
			{
				k = i * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					g[k + 1].push_back(k);
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					g[k + m].push_back(k);
				}

			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = i * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k);
				printf("%ld", res);
				while (a > 0)
				{
					g[k].pop_back();
					--a;
				}
			}
			else
				printf("%c", lab[i][j]);
		}
		cout << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001], v;
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < b)
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			//cin >> lab[i][j];
			scanf("\t%c", &lab[i][j]);
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
			}

		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k) % 10;
				printf("%ld", res);
				while (a > 0)
				{
					g[k].pop_back();
					--a;
				}
			}
			else
				printf("%c", lab[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001];
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int m)
{
	met[v] = m;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < m)
			DFS(g[v][i], m);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	scanf("%d%d", &n, &m);
	g.resize(n * m + n * 4);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> lab[i][j];
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '.')
			{
				k = i * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					g[k + 1].push_back(k);
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					g[k + m].push_back(k);
				}

			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = i * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k);
				printf("%ld", res);
				while (a > 0)
				{
					g[k].pop_back();
					--a;
				}
			}
			else
				printf("%c", lab[i][j]);
		}
		cout << endl;
	}
	return 0;


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100], met1[1001][1001], comp[1000010];
char lab[1001][1001], v;
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (!met[g[v][i]])
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("\t%c", &lab[i][j]);
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
			}
		}
	}
	set <int> in;
	set <int> ::iterator it;
	int r = 0;
	for (int i = 1; i <= n*m; ++i)
	{
		if (!met[i])
		{
			++r;
			sum = 0;
			comp[r] = DFS(i, r);
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				k = (i - 1) * m + j;
				res = 0;
				if (lab[i][j - 1] == '.')
				{
					in.insert(met[k - 1]);
				}
				if (lab[i - 1][j] == '.')
				{
					in.insert(met[k - m]);
				}
				if (lab[i][j + 1] == '.')
				{
					in.insert(met[k + 1]);
				}
				if (lab[i + 1][j] == '.')
				{
					in.insert(met[k + m]);
				}
				for (it = in.begin(); it != in.end(); ++it)
				{
					res += comp[*it];
				}
				++res;
				res %= 10;
				in.clear();
				printf("%ld", res);
			}
			else
				printf("%c", lab[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100], met1[1001][1001], comp[1000010];
char lab[1001][1001], v;
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (!met[g[v][i]])
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("\t%c", &lab[i][j]);
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
			}
		}
	}
	set <int> in;
	set <int> ::iterator it;
	int r = 0;
	for (int i = 1; i <= n*m; ++i)
	{
		if (!met[i])
		{
			++r;
			sum = 0;
			comp[r] = DFS(i, r);
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				k = (i - 1) * m + j;
				res = 0;
				if (lab[i][j - 1] == '.')
				{
					in.insert(met[k - 1]);
				}
				if (lab[i - 1][j] == '.')
				{
					in.insert(met[k - m]);
				}
				if (lab[i][j + 1] == '.')
				{
					in.insert(met[k + 1]);
				}
				if (lab[i + 1][j] == '.')
				{
					in.insert(met[k + m]);
				}
				for (it = in.begin(); it != in.end(); ++it)
				{
					res += comp[*it];
				}
				res %= 10;
				in.clear();
				printf("%ld", res + 1);
			}
			else
				printf("%c", lab[i][j]);
		}
		printf("\n");
	}
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
# include <iostream>
# include <cstdio>
# include <cstring>
# include <string>
# include <cmath>
# include <algorithm>
# include <vector>
# include <map>
# include <queue>
# include <iterator>
# include <set>

using namespace std;
int n, m, k, a, met[1000100];
char lab[1001][1001];
long long res, res1;
vector <vector<int> > g;
int sum;
int DFS(int v, int b)
{
	met[v] = b;
	++sum;
	for (int i = 0; i < (int)g[v].size(); ++i)
	{
		if (met[g[v][i]] < b)
			DFS(g[v][i], b);
	}
	return sum;
}
int main()
{
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	cin >> n >> m;
	g.resize(n * m + 2);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			cin >> lab[i][j];
		}
	}

	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '.')
			{
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					g[k - 1].push_back(k);
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					g[k - m].push_back(k);
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					g[k + 1].push_back(k);
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					g[k + m].push_back(k);
				}

			}
		}
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= m; ++j)
		{
			if (lab[i][j] == '*')
			{
				a = 0;
				k = (i - 1) * m + j;
				if (lab[i][j - 1] == '.')
				{
					g[k].push_back(k - 1);
					++a;
				}
				if (lab[i - 1][j] == '.')
				{
					g[k].push_back(k - m);
					++a;
				}
				if (lab[i][j + 1] == '.')
				{
					g[k].push_back(k + 1);
					++a;
				}
				if (lab[i + 1][j] == '.')
				{
					g[k].push_back(k + m);
					++a;
				}
				sum = 0;
				res = DFS(k, k);
				printf("%ld", res);
				while (a > 0)
				{
					g[k].pop_back();
					--a;
				}

			}
			else
				printf("%c", lab[i][j]);
		}
		cout << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
char g[MAX][MAX];
int d[MAX][MAX];
int used[MAX][MAX];

vector < pair < int, int > > v;

void input ( )
{
    cin >> n >> m;

    memset ( g, '*', sizeof ( g ) );

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            d[i][j] = -1;
            cin >> g[i][j];

            if ( g[i][j] == '*' )
            {
                v.pb ( { i, j } );
            }
        }
    }
}

int cnt = 0;

void bfs ( int u, int v )
{
    queue < pair < int, int > > qu;
    qu.push ( { u, v } );

    used[u][v] = true;

    while ( !qu.empty ( ) )
    {
        int x = qu.front ( ).first;
        int y = qu.front ( ).second;

        qu.pop ( );

        int p = x + 1, q = y;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }

        p = x - 1; q = y;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }

        p = x; q = y + 1;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }

        p = x; q = y - 1;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }
    }
}

void solve ( )
{

    for ( int i = 0; i < v.size ( ); ++ i )
    {
        cnt = 1;

        memset ( used, 0, sizeof ( used ) );

        bfs ( v[i].first, v[i].second );

        d[v[i].first][v[i].second] = cnt;
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( d[i][j] >= 0 ) cout << d[i][j];
            else  if ( d[i][j] == -1 ) cout << ".";
        }
        cout << endl;
    }

}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );

}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

void input ( )
{
    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
         }
    }

}

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

void solve ( )
{
    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;

                set < int > s;

                if ( g[i+1][j] != '*' ) ans[i][j] += colsz[g[i+1][j]], s.insert ( g[i+1][j] );
                if ( g[i-1][j] != '*' && !s.count ( g[i-1][j] ) ) ans[i][j] += colsz[g[i-1][j]], s.insert ( g[i-1][j] );
                if ( g[i][j+1] != '*' && !s.count ( g[i][j+1] ) ) ans[i][j] += colsz[g[i][j+1]], s.insert ( g[i][j+1] );;
                if ( g[i][j-1] != '*' && !s.count ( g[i][j-1] ) ) ans[i][j] += colsz[g[i][j-1]], s.insert ( g[i][j-1] );;

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );

}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
char g[MAX][MAX];
int d[MAX][MAX];
int used[MAX][MAX];

vector < pair < int, int > > v;

void input ( )
{
    cin >> n >> m;

    memset ( g, '*', sizeof ( g ) );

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            d[i][j] = -1;
            cin >> g[i][j];

            if ( g[i][j] == '*' )
            {
                v.pb ( { i, j } );
            }
        }
    }
}

int cnt = 0;

void bfs ( int u, int v )
{
    queue < pair < int, int > > qu;
    qu.push ( { u, v } );

    used[u][v] = true;

    while ( !qu.empty ( ) )
    {
        int x = qu.front ( ).first;
        int y = qu.front ( ).second;

        qu.pop ( );

        int p = x + 1, q = y;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }

        p = x - 1; q = y;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }

        p = x; q = y + 1;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }

        p = x; q = y - 1;
        if ( !used[p][q] && g[p][q] == '.' )
        {
            ++ cnt;
            used[p][q] = true;
            qu.push ( { p, q } );
        }
    }
}

void solve ( )
{

    for ( int i = 0; i < v.size ( ); ++ i )
    {
        cnt = 1;

        memset ( used, 0, sizeof ( used ) );

        bfs ( v[i].first, v[i].second );

        d[v[i].first][v[i].second] = cnt % 10;
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( d[i][j] >= 0 ) cout << d[i][j] % 10;
            else  if ( d[i][j] == -1 ) cout << ".";
        }
        cout << endl;
    }

}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );

}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

void input ( )
{
    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
         }
    }

}

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

void solve ( )
{
    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;
//                cout << '*' << " ";
                if ( g[i+1][j] != '*' ) ans[i][j] += colsz[g[i+1][j]];
                if ( g[i-1][j] != '*' && g[i-1][j] != g[i+1][j] ) ans[i][j] += colsz[g[i-1][j]];
                if ( g[i][j+1] != '*' && g[i][j+1] != g[i+1][j] && g[i][j+1] != g[i-1][j] ) ans[i][j] += colsz[g[i][j+1]];
                if ( g[i][j-1] != '*' && g[i][j-1] != g[i+1][j] && g[i][j-1] != g[i-1][j] && g[i][j-1] != g[i][j+1] ) ans[i][j] += colsz[g[i][j-1]];

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );

}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

void input ( )
{
    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
         }
    }

}

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

void solve ( )
{
    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;

                int h[MAX] = { 0 };

                if ( g[i+1][j] != '*' ) {ans[i][j] += colsz[g[i+1][j]]; h[g[i+1][j]] = 1;}
                if ( g[i-1][j] != '*' && !h[g[i-1][j]] ) { ans[i][j] += colsz[g[i-1][j]]; h[g[i-1][j]] = 1; }
                if ( g[i][j+1] != '*' && !h[g[i][j+1]] ) {ans[i][j] += colsz[g[i][j+1]]; h[g[i][j+1]] = 1; }
                if ( g[i][j-1] != '*' && !h[g[i][j-1]] ) {ans[i][j] += colsz[g[i][j-1]]; }

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define MAXX 1000010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAXX];

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1;
    y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1;
    y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i;
    y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i;
    y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
        }
    }

    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;

                int h[MAXX] = { 0 };

                if ( g[i+1][j] != '*' )
                {
                    ans[i][j] += colsz[g[i+1][j]];
                    h[g[i+1][j]] = 1;
                }
                if ( g[i-1][j] != '*' && !h[g[i-1][j]] )
                {
                    ans[i][j] += colsz[g[i-1][j]];
                    h[g[i-1][j]] = 1;
                }
                if ( g[i][j+1] != '*' && !h[g[i][j+1]] )
                {
                    ans[i][j] += colsz[g[i][j+1]];
                    h[g[i][j+1]] = 1;
                }
                if ( g[i][j-1] != '*' && !h[g[i][j-1]] )
                {
                    ans[i][j] += colsz[g[i][j-1]];
                }

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

void input ( )
{
    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
         }
    }

}

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

void solve ( )
{
    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;
                if ( g[i+1][j] != '*' ) ans[i][j] += colsz[g[i+1][j]];
                if ( g[i-1][j] != '*' ) ans[i][j] += colsz[g[i-1][j]];
                if ( g[i][j+1] != '*' ) ans[i][j] += colsz[g[i][j+1]];
                if ( g[i][j-1] != '*' ) ans[i][j] += colsz[g[i][j-1]];
                ans[i][j] ++;
                ans[i][j] %= 10;
                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );

}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1;
    y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1;
    y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i;
    y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i;
    y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
        }
    }

    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;

                int h[MAX] = { 0 };

                if ( g[i+1][j] != '*' )
                {
                    ans[i][j] += colsz[g[i+1][j]];
                    h[g[i+1][j]] = 1;
                }
                if ( g[i-1][j] != '*' && !h[g[i-1][j]] )
                {
                    ans[i][j] += colsz[g[i-1][j]];
                    h[g[i-1][j]] = 1;
                }
                if ( g[i][j+1] != '*' && !h[g[i][j+1]] )
                {
                    ans[i][j] += colsz[g[i][j+1]];
                    h[g[i][j+1]] = 1;
                }
                if ( g[i][j-1] != '*' && !h[g[i][j-1]] )
                {
                    ans[i][j] += colsz[g[i][j-1]];
                }

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

void input ( )
{
    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
         }
    }

}

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

void solve ( )
{
    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;

                int h[4] = { 0 };

                if ( g[i+1][j] != '*' ) ans[i][j] += colsz[g[i+1][j]], h[g[i+1][j]] = 1;
                if ( g[i-1][j] != '*' && !h[g[i-1][j]] = 1 ) ans[i][j] += colsz[g[i-1][j]], h[g[i-1][j]] = 1;
                if ( g[i][j+1] != '*' && !h[g[i][j+1]] = 1 ) ans[i][j] += colsz[g[i][j+1]], h[g[i][j+1]] = 1;
                if ( g[i][j-1] != '*' && !h[g[i][j-1]] = 1 ) ans[i][j] += colsz[g[i][j-1]];

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010

int n, m, d[MAX*MAX], used[MAX][MAX];
char g[MAX][MAX];

void input ( )
{
    scanf ( "%d %d", &n, &m );
    getchar ( );

    char c;
    int i = 1, j = 1;

    memset ( g, '*', sizeof ( g ) );
    memset ( used, 0, sizeof ( used ) );

    while ( ( c = getchar ( ) ) != EOF )
    {
        if ( c == '\n' )
        {
            g[i][j] = '\0';
            ++ i;
            j = 1;
        }
        else
        {
            g[i][j] = c;
            ++ j;
        }
    }
}

int colour = 1;

void dfs ( int i, int j )
{
    used[i][j] = colour;

    d[colour] ++;

    if ( g[i+1][j] != '*' && !used[i+1][j] ) dfs ( i+1, j );
    if ( g[i-1][j] != '*' && !used[i-1][j] ) dfs ( i-1, j );
    if ( g[i][j+1] != '*' && !used[i][j+1] ) dfs ( i, j+1 );
    if ( g[i][j-1] != '*' && !used[i][j-1] ) dfs ( i, j-1 );
}

int h[5];

int magic ( )
{
    sort ( h+1, h+5 );

    int res = 0;

    for ( int i = 1; i <= 4; ++ i )
    {
        if ( h[i] == h[i-1] || h[i] == 0 ) continue;
        res += d[h[i]];
    }
    return res;
}

void solve ( )
{

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] != '*' )
            {
                dfs ( i, j );
                ++ colour;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                memset ( h, 0, sizeof ( h ) );

                h[1] = used[i+1][j];
                h[2] = used[i-1][j];
                h[3] = used[i][j+1];
                h[4] = used[i][j-1];

                cout << ( magic ( ) + 1 ) % 10;
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010

int n, m, d[MAX*MAX], used[MAX][MAX];
char g[MAX][MAX];

void input ( )
{
    scanf ( "%d %d", &n, &m );
    getchar ( );

    char c;
    int i = 1, j = 1;

    memset ( used, 0, sizeof ( used ) );
    memset ( g, '*', sizeof ( g ) );

    while ( ( c = getchar ( ) ) != EOF )
    {
        if ( c == '\n' )
        {
            g[i][j] = '*';
            ++ i;
            j = 1;
        }
        else
        {
            g[i][j] = c;
            ++ j;
        }
    }
}

int colour = 1;

void dfs ( int i, int j )
{
    used[i][j] = colour;

    d[colour] ++;

    if ( g[i+1][j] != '*' && !used[i+1][j] ) dfs ( i+1, j );
    if ( g[i-1][j] != '*' && !used[i-1][j] ) dfs ( i-1, j );
    if ( g[i][j+1] != '*' && !used[i][j+1] ) dfs ( i, j+1 );
    if ( g[i][j-1] != '*' && !used[i][j-1] ) dfs ( i, j-1 );
}

int h[5];

int magic ( )
{
    sort ( h+1, h+5 );

    int res = 0;

    for ( int i = 1; i <= 4; ++ i )
    {
        if ( h[i] == h[i-1] || h[i] == 0 ) continue;
        res += d[h[i]];
    }
    return res;
}

void solve ( )
{

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] != '*' )
            {
                dfs ( i, j ); ++ colour;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                memset ( h, 0, sizeof ( h ) );

                h[1] = used[i+1][j];
                h[2] = used[i-1][j];
                h[3] = used[i][j+1];
                h[4] = used[i][j-1];

                cout << ( magic ( ) + 1 ) % 10;
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 1010
#define pb push_back
#define ll long long

#define INF 1000000010

int n, m;
int g[MAX][MAX];
int ans[MAX][MAX];
int used[MAX][MAX];
int colsz[MAX];

void input ( )
{
    cin >> n >> m;

    memset ( used, 0, sizeof ( used ) );

    for ( int i = 0; i <= n + 1; ++ i )
    {
        for ( int j = 0; j <= m + 1; ++ j )
        {
            if ( i >= 1 && i <= n && j >= 1 && j <= m )
            {
                char c;
                cin >> c;
                g[i][j] = c;
            }
            else g[i][j] = '*';

            ans[i][j] = g[i][j];
         }
    }

}

int cnt = 0;

void dfs ( int i, int j, int col )
{
//    cout << i << " " << j << endl;
    ++ cnt;
    if ( g[i][j] != '*' ) g[i][j] = col;
    used[i][j] = true;

    int x, y;

    x = i + 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i - 1; y = j;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j + 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
    x = i; y = j - 1;
    if ( !used[x][y] && g[x][y] != '*' ) dfs ( x, y, col );
}

void solve ( )
{
    int colour = 1;

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( !used[i][j] && g[i][j] == '.' )
            {
//                cout << colour << endl;
                cnt = 0;
                dfs ( i, j, colour );
                colsz[colour ++] = cnt;
//                cout << "  " << cnt << endl;
            }
        }
    }

    for ( int i = 1; i <= n; ++ i )
    {
        for ( int j = 1; j <= m; ++ j )
        {
            if ( g[i][j] == '*' )
            {
                ans[i][j] = 0;
//                cout << '*' << " ";
                if ( g[i+1][j] != '*' ) ans[i][j] += colsz[g[i+1][j]];
                if ( g[i-1][j] != '*' && g[i-1][j] != g[i+1][j] ) ans[i][j] += colsz[g[i-1][j]];
                if ( g[i][j+1] != '*' && g[i][j+1] != g[i+1][j] && g[i][j+1] != g[i-1][j] ) ans[i][j] += colsz[g[i][j+1]];
                if ( g[i][j-1] != '*' && g[i][j-1] != g[i+1][j] && g[i][j-1] != g[i-1][j] && g[i][j-1] != g[i][j+1] ) ans[i][j] += colsz[g[i][j-1]];

                ans[i][j] ++;
                ans[i][j] %= 10;

                cout << ans[i][j];
            }
            else cout << '.';
        }
        cout << endl;
    }
}

int main ( )
{
    ios_base::sync_with_stdio ( false );
//    freopen ( "input.txt", "r", stdin );

    input ( );

    solve ( );

}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
int mark[N][N];
char gra[N][N];
bool visited[N][N];
int n,m,tmp,ct;
map<int,int> mp;
set<int> ad;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}
void init()
{
    memset(gra,0,sizeof(gra));
    memset(mark,0xff,sizeof(mark));
    memset(visited,0,sizeof(visited));
    ct=0;
}
void dfs(int x,int y)
{
    tmp++;mark[x][y]=ct;
    visited[x][y]=1;
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
int main()
{
    init();mp.clear();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mark[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                mp[ct++]=tmp;
                memset(visited,0,sizeof(visited));tmp=0;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*')
            {
                int num=1;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]=='.')
                    {
                        if(ad.insert(mark[tx][ty]).second!=0)
                        {
                            num+=mp[mark[tx][ty]];
                        }
                    }
                }
                gra[i][j]=(num%10)+'0';
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%s\n",gra[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
queue <p> point;p tp;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
bool visited[N][N];
int mark[N][N];
char gra[N][N];
int num[N][N];
int n,m,tmp,ct;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}

void dfs(int x,int y)
{
    tmp++;visited[x][y]=1;mark[x][y]=ct;
    tp.first=x;tp.second=y;
    point.push(tp);
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
void debug()
{
    //puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*'){printf("%d",num[i][j]%10);}
            else{printf("%c",gra[i][j]);}
        }
        puts("");
    }
    /*puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d",mark[i][j]);
        }
        puts("");
    }*/
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(gra,0,sizeof(gra));
    memset(num,0xff,sizeof(num));
    memset(visited,0,sizeof(visited));
    memset(mark,0xff,sizeof(mark));
    ct=0;
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                while(!point.empty())
                {
                    num[point.front().first][point.front().second]=tmp;
                    point.pop();
                }
                memset(visited,0,sizeof(visited));tmp=0;++ct;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1)
            {
                num[i][j]=1;vector<p>ad;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]!='*')
                    {
                        if(ad.size())
                        {
                            bool flag=0;
                            for(int q=0;q<ad.size();q++)
                            {
                                if(mark[tx][ty]==mark[ad[q].first][ad[q].second])
                                {
                                    flag=1;break;
                                }
                            }
                            if(!flag){
                                num[i][j]+=num[tx][ty];
                                tp.first=tx;tp.second=ty;ad.push_back(tp);
                            }
                        }
                        else{
                            num[i][j]+=num[tx][ty];tp.first=tx;tp.second=ty;
                            ad.push_back(tp);
                        }
                    }
                }
            }
        }
    }
    debug();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
queue <p> point;p tp;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
bool visited[N][N];
int mark[N][N];
char gra[N][N];
int num[N][N];
int n,m,tmp,ct;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}

void dfs(int x,int y)
{
    tmp++;visited[x][y]=1;mark[x][y]=ct;
    tp.first=x;tp.second=y;
    point.push(tp);
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
void debug()
{
    //puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*'){printf("%d",num[i][j]);}
            else{printf("%c",gra[i][j]);}
        }
        puts("");
    }
    /*puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d",mark[i][j]);
        }
        puts("");
    }*/
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(gra,0,sizeof(gra));
    memset(num,0xff,sizeof(num));
    memset(visited,0,sizeof(visited));
    memset(mark,0xff,sizeof(mark));
    ct=0;
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                while(!point.empty())
                {
                    num[point.front().first][point.front().second]=tmp;
                    point.pop();
                }
                memset(visited,0,sizeof(visited));tmp=0;++ct;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1)
            {
                num[i][j]=1;vector<p>ad;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]!='*')
                    {
                        if(ad.size())
                        {
                            for(int q=0;q<ad.size();q++)
                            {
                                if(mark[tx][ty]!=mark[ad[q].first][ad[q].second])
                                {
                                    num[i][j]+=num[tx][ty];
                                }
                            }
                        }
                        else{
                            num[i][j]+=num[tx][ty];tp.first=tx;tp.second=ty;
                            ad.push_back(tp);
                        }
                    }
                }
            }
        }
    }
    debug();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
queue <p> point;p tp;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
bool visited[N][N];
int mark[N][N];
char gra[N][N];
int num[N][N];
int n,m,tmp,ct;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}

void dfs(int x,int y)
{
    tmp++;visited[x][y]=1;mark[x][y]=ct;
    tp.first=x;tp.second=y;
    point.push(tp);
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
void debug()
{
    //puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*'){printf("%d",num[i][j]%10);}
            else{printf("%c",gra[i][j]);}
        }
        puts("");
    }
    /*puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d",mark[i][j]);
        }
        puts("");
    }*/
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(gra,0,sizeof(gra));
    memset(num,0xff,sizeof(num));
    memset(visited,0,sizeof(visited));
    memset(mark,0xff,sizeof(mark));
    ct=0;
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                while(!point.empty())
                {
                    num[point.front().first][point.front().second]=tmp;
                    point.pop();
                }
                memset(visited,0,sizeof(visited));tmp=0;++ct;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1)
            {
                num[i][j]=1;vector<p>ad;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]!='*')
                    {
                        if(ad.size())
                        {
                            bool flag=0;
                            for(int q=0;q<ad.size();q++)
                            {
                                if(mark[tx][ty]==mark[ad[q].first][ad[q].second])
                                {
                                    flag=1;break;
                                }
                                if(!flag){num[i][j]+=num[tx][ty];}
                            }
                        }
                        else{
                            num[i][j]+=num[tx][ty];tp.first=tx;tp.second=ty;
                            ad.push_back(tp);
                        }
                    }
                }
            }
        }
    }
    debug();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
queue <p> point;p tp;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
bool visited[N][N];
int mark[N][N];
char gra[N][N];
int num[N][N];
int n,m,tmp,ct;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}

void dfs(int x,int y)
{
    tmp++;visited[x][y]=1;mark[x][y]=ct;
    tp.first=x;tp.second=y;
    point.push(tp);
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
void debug()
{
    //puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*'){printf("%d",num[i][j]%10);}
            else{printf("%c",gra[i][j]);}
        }
        puts("");
    }
    /*puts("***************debug********************");
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            printf("%d",mark[i][j]);
        }
        puts("");
    }*/
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(gra,0,sizeof(gra));
    memset(num,0xff,sizeof(num));
    memset(visited,0,sizeof(visited));
    memset(mark,0xff,sizeof(mark));
    ct=0;
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                while(!point.empty())
                {
                    num[point.front().first][point.front().second]=tmp;
                    point.pop();
                }
                memset(visited,0,sizeof(visited));tmp=0;++ct;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1)
            {
                num[i][j]=1;vector<p>ad;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]!='*')
                    {
                        if(ad.size())
                        {
                            for(int q=0;q<ad.size();q++)
                            {
                                if(mark[tx][ty]!=mark[ad[q].first][ad[q].second])
                                {
                                    num[i][j]+=num[tx][ty];
                                }
                            }
                        }
                        else{
                            num[i][j]+=num[tx][ty];tp.first=tx;tp.second=ty;
                            ad.push_back(tp);
                        }
                    }
                }
            }
        }
    }
    debug();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
int mark[N][N];
char gra[N][N];
int n,m,tmp,ct;
int mp[555555];
set<int> ad;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m) return false;
    return true;
}
void init()
{
    memset(gra,0,sizeof(gra));
    memset(mp,0,sizeof(mp));
    memset(mark,0xff,sizeof(mark));
    ct=0;
}
void dfs(int x,int y)
{
    tmp++;mark[x][y]=ct;
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            if(mark[tx][ty]!=-1)
            {
                tmp+=mp[mark[tx][ty]];
            }else
            {
                dfs(tx,ty);
            }
        }
    }
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mark[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                mp[ct++]=tmp;
                tmp=0;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*')
            {
                int num=1;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]=='.')
                    {
                        if(ad.insert(mark[tx][ty]).second!=0)
                        {
                            num+=mp[mark[tx][ty]];
                        }
                    }
                }
                gra[i][j]=(num%10)+'0';
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%s\n",gra[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
int mark[N][N];
char gra[N][N];
int n,m,tmp,ct;
map<int,int> mp;
set<int> ad;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m) return false;
    return true;
}
void init()
{
    memset(gra,0,sizeof(gra));
    memset(mark,0xff,sizeof(mark));
    ct=0;
}
void dfs(int x,int y)
{
    tmp++;mark[x][y]=ct;
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            if(mark[tx][ty]!=-1)
            {
                tmp+=mp[mark[tx][ty]];
            }else
            {
                dfs(tx,ty);
            }
        }
    }
}
int main()
{
    init();mp.clear();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(mark[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                mp[ct++]=tmp;
                tmp=0;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*')
            {
                int num=1;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]=='.')
                    {
                        if(ad.insert(mark[tx][ty]).second!=0)
                        {
                            num+=mp[mark[tx][ty]];
                        }
                    }
                }
                gra[i][j]=(num%10)+'0';
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        printf("%s\n",gra[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
bool visited[N][N];
int mark[N][N];
char gra[N][N];
int num[N][N];
int n,m,tmp,ct;
map<int,int> mp;
set<int> ad;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}
void init()
{
    memset(gra,0,sizeof(gra));
    memset(num,0xff,sizeof(num));
    memset(visited,0,sizeof(visited));
    memset(mark,0xff,sizeof(mark));
    ct=0;
}
void dfs(int x,int y)
{
    tmp++;visited[x][y]=1;mark[x][y]=ct;
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
void debug()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*'){printf("%d",num[i][j]%10);}
            else{printf("%c",gra[i][j]);}
        }
        puts("");
    }
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                mp[ct++]=tmp;
                memset(visited,0,sizeof(visited));tmp=0;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1)
            {
                num[i][j]=1;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]!='*')
                    {
                        if(ad.insert(mark[tx][ty]).second!=0)
                        {
                            num[i][j]+=mp[mark[tx][ty]];
                        }
                    }
                }
            }
        }
    }
    debug();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define N 1111
typedef pair<int,int> p;
queue <p> point;p tp;
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
bool visited[N][N];
int mark[N][N];
char gra[N][N];
int num[N][N];
int n,m,tmp,ct;
set<int> ad;
bool is_in(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m||visited[x][y]) return false;
    return true;
}
void init()
{
    memset(gra,0,sizeof(gra));
    memset(num,0xff,sizeof(num));
    memset(visited,0,sizeof(visited));
    memset(mark,0xff,sizeof(mark));
    ct=0;
}
void dfs(int x,int y)
{
    tmp++;visited[x][y]=1;mark[x][y]=ct;
    tp.first=x;tp.second=y;
    point.push(tp);
    for(int i=0;i<4;i++){
        int tx=x+dx[i],ty=y+dy[i];
        if(gra[tx][ty]=='.'&&is_in(tx,ty))
        {
            dfs(tx,ty);
        }
    }
}
void debug()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(gra[i][j]=='*'){printf("%d",num[i][j]%10);}
            else{printf("%c",gra[i][j]);}
        }
        puts("");
    }
}
int main()
{
    init();
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",gra[i]);
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1&&gra[i][j]=='.')
            {
                dfs(i,j);
                while(!point.empty())
                {
                    num[point.front().first][point.front().second]=tmp;
                    point.pop();
                }
                memset(visited,0,sizeof(visited));tmp=0;++ct;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(num[i][j]==-1)
            {
                num[i][j]=1;ad.clear();
                for(int k=0;k<4;k++)
                {
                    int tx=i+dx[k],ty=j+dy[k];
                    if(is_in(tx,ty)&&gra[tx][ty]!='*')
                    {
                        if(ad.insert(mark[tx][ty]).second!=0)
                        {
                            num[i][j]+=num[tx][ty];
                        }
                    }
                }
            }
        }
    }
    debug();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
int odw[1007][1007];
int q;
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[i][0]='c';
        t[i][m+1]='c';

    }

    for(int i=0; i<=m+1; i++)
    {
        t[0][i]='c';
        t[n+1][i]='c';

    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            q++;
        licznik =0;
            if(t[i][j]!='.')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=q;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=q;

                    if(t[a-1][b]=='.'&&odw[a-1][b]!=q)
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=q;
                    }
                    if(t[a][b-1]=='.'&&odw[a][b-1]!=q)
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=q;
                    }
                    if(t[a+1][b]=='.'&&odw[a+1][b]!=q)
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=q;
                    }
                    if(t[a][b+1]=='.'&&odw[a][b+1]!=q)
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=q;
                    }
                }

            }

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]%10);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
bool odw[1007][1007];
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[0][i]='c';
        t[i][m+1]='c';
        odw[0][i]=true;
        odw[m+1][i]=true;
    }

    for(int i=0; i<=m+1; i++)
    {
        t[i][0]='c';
        t[n+1][i]='c';
        odw[i][0]=true;
        odw[i][n+1]=true;
    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
        licznik =0;
            if(t[i][j]!='.')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=true;

                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }
                }

            }

            for(int x=1; x<=n; x++)
                for(int y=1; y<=m; y++)
                    odw[x][y]=false;

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];//poczatkowa
int z[1007][1007];//ponumerowana
bool odw[1007][1007];//odwiedzone składowych
int roz[1000007];//rozmiary
int wyn[1007][1007];

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }
vector<int> po;

    int u=m;
    if(n>u)u=n;
    for(int i=0; i<=u+1; i++)
    {
        t[i][0]='c';
        t[i][u+1]='c';
        t[0][i]='c';
        t[u+1][i]='c';
    }

    int rozmiar=0;
    int numer=1;
    queue< pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.'&&!odw[i][j])
            {
                kol.push(make_pair(i,j));
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int> v=kol.front();
                    kol.pop();
                    int a=v.first;
                    int b=v.second;
                    z[a][b]=numer;
                    roz[numer]++;
                    odw[a][b]=true;
                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }

                }
            numer++;
            }

            rozmiar=0;

        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            int h=0;
            if(t[i][j]=='*')
            {
                if(t[i-1][j]=='.')
                {
                    po.push_back(z[i-1][j]);
                }
                if(t[i][j-1]=='.')
                {
                    po.push_back(z[i][j-1]);
                }
                if(t[i+1][j]=='.')
                {
                    po.push_back(z[i+1][j]);
                }
                if(t[i][j+1]=='.')
                {
                    po.push_back(z[i][j+1]);
                }

                for(int x=0; x<po.size(); x++)
                {
                    for(int y=x+1; y<po.size(); y++)
                    {
                        if(po[x]==po[y])h=1;
                    }
                    if(h==0)wyn[i][j]+=roz[po[x]];
                    h=0;
                }
                po.clear();
            }
            else wyn[i][j]=0;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
           if(t[i][j]=='.')printf(".");
            else printf("%d",(wyn[i][j]+1)%10);
        }
        printf("\n");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
bool odw[1007][1007];
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[i][0]='c';
        t[i][m+1]='c';
        odw[i][0]=true;
        odw[i][m+1]=true;
    }

    for(int i=0; i<=m+1; i++)
    {
        t[0][i]='c';
        t[n+1][i]='c';
        odw[0][1]=true;
        odw[n+1][i]=true;
    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
        licznik =0;
            if(t[i][j]!='.')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=true;

                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }
                }

            }

            for(int x=1; x<=n; x++)
                for(int y=1; y<=m; y++)
                    odw[x][y]=false;

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]%10);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
bool odw[1007][1007];
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[i][0]='c';
        t[i][m+1]='c';
        odw[i][0]=true;
        odw[i][m+1]=true;
    }

    for(int i=0; i<=m+1; i++)
    {
        t[0][i]='c';
        t[n+1][i]='c';
        odw[0][1]=true;
        odw[n+1][i]=true;
    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
        licznik =0;
            if(t[i][j]!='.')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=true;

                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }
                }

            }

            for(int x=1; x<=n; x++)
                for(int y=1; y<=m; y++)
                    odw[x][y]=false;

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];//poczatkowa
int z[1007][1007];//ponumerowana
bool odw[1007][1007];//odwiedzone składowych
int roz[1000007];//rozmiary
int wyn[1007][1007];
vector<int> pow;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    int u=m;
    if(n>u)u=n;
    for(int i=0; i<=u+1; i++)
    {
        t[i][0]='c';
        t[i][u+1]='c';
        t[0][i]='c';
        t[u+1][i]='c';
    }

    int rozmiar=0;
    int numer=1;
    queue< pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.'&&!odw[i][j])
            {
                kol.push(make_pair(i,j));
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int> v=kol.front();
                    kol.pop();
                    int a=v.first;
                    int b=v.second;
                    z[a][b]=numer;
                    roz[numer]++;
                    odw[a][b]=true;
                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }

                }
            numer++;
            }

            rozmiar=0;

        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            int h=0;
            if(t[i][j]=='*')
            {
                if(t[i-1][j]=='.')
                {
                    pow.push_back(z[i-1][j]);
                }
                if(t[i][j-1]=='.')
                {
                    pow.push_back(z[i][j-1]);
                }
                if(t[i+1][j]=='.')
                {
                    pow.push_back(z[i+1][j]);
                }
                if(t[i][j+1]=='.')
                {
                    pow.push_back(z[i][j+1]);
                }

                for(int x=0; x<pow.size(); x++)
                {
                    for(int y=x+1; y<pow.size(); y++)
                    {
                        if(pow[x]==pow[y])h=1;
                    }
                    if(h==0)wyn[i][j]+=roz[pow[x]];
                    h=0;
                }
                pow.clear();
            }
            else wyn[i][j]=0;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
           if(t[i][j]=='.')printf(".");
            else printf("%d",(wyn[i][j]+1)%10);
        }
        printf("\n");
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
bool odw[1007][1007];
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[i][0]='c';
        t[i][m+1]='c';
        odw[0][i]=true;
        odw[m+1][i]=true;
    }

    for(int i=0; i<=m+1; i++)
    {
        t[i][0]='c';
        t[n+1][i]='c';
        odw[i][0]=true;
        odw[i][n+1]=true;
    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
        licznik =0;
            if(t[i][j]!='.')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=true;

                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }
                }

            }

            for(int x=1; x<=n; x++)
                for(int y=1; y<=m; y++)
                    odw[x][y]=false;

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
bool odw[1007][1007];
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[0][i]='c';
        t[i][m+1]='c';
        odw[0][i]=true;
        odw[m+1][i]=true;
    }

    for(int i=0; i<=m+1; i++)
    {
        t[0][i]='c';
        t[n+1][i]='c';
        odw[i][0]=true;
        odw[i][n+1]=true;
    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
        licznik =0;
            if(t[i][j]!='.')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=true;

                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }
                }

            }

            for(int x=1; x<=n; x++)
                for(int y=1; y<=m; y++)
                    odw[x][y]=false;

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
bool odw[1007][1007];
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=n+1; i++)
    {
        t[0][i]='*';
        t[m+1][i]='*';
        odw[0][i]=true;
        odw[m+1][i]=true;
    }

    for(int i=0; i<=m+1; i++)
    {
        t[i][0]='*';
        t[i][n+1]='*';
        odw[i][0]=true;
        odw[i][n+1]=true;
    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='*')
            {
                licznik =0;
                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=true;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=true;

                    if(t[a-1][b]=='.'&&!odw[a-1][b])
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=true;
                    }
                    if(t[a][b-1]=='.'&&!odw[a][b-1])
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=true;
                    }
                    if(t[a+1][b]=='.'&&!odw[a+1][b])
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=true;
                    }
                    if(t[a][b+1]=='.'&&!odw[a][b+1])
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=true;
                    }
                }

            }

            for(int x=1; x<=n; x++)
                for(int y=1; y<=m; y++)
                    odw[x][y]=false;

            w[i][j]=licznik;
        }
    }

    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; j++)
        {
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]);

        }
        printf("\n");
        }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <utility>

using namespace std;

int n,m;
char t[1007][1007];
int w[1007][1007];
int odw[1007][1007];
int q;
int licznik;

int main()
{
    scanf("%d%d",&n,&m);

    int u=m;
    if(n>u)u=n;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
            cin>>t[i][j];

    }

    for(int i=0; i<=u+1; i++)
    {
        t[i][0]='c';
        t[i][u+1]='c';
         t[0][i]='c';
        t[u+1][i]='c';

    }

    queue<pair<int,int> > kol;

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            q++;
            licznik =0;
            if(t[i][j]=='*')
            {

                pair<int,int> s=make_pair(i,j);
                kol.push(s);
                odw[i][j]=q;

                while(!kol.empty())
                {
                    pair<int,int>v=kol.front();
                    kol.pop();
                    licznik++;
                    int a=v.first;
                    int b=v.second;
                    odw[a][b]=q;

                    if(t[a-1][b]=='.'&&odw[a-1][b]!=q)
                    {
                        kol.push(make_pair(a-1,b));
                        odw[a-1][b]=q;
                    }
                    if(t[a][b-1]=='.'&&odw[a][b-1]!=q)
                    {
                        kol.push(make_pair(a,b-1));
                        odw[a][b-1]=q;
                    }
                    if(t[a+1][b]=='.'&&odw[a+1][b]!=q)
                    {
                        kol.push(make_pair(a+1,b));
                        odw[a+1][b]=q;
                    }
                    if(t[a][b+1]=='.'&&odw[a][b+1]!=q)
                    {
                        kol.push(make_pair(a,b+1));
                        odw[a][b+1]=q;
                    }
                }

            }

            w[i][j]=licznik;
            if(t[i][j]=='.')printf(".");
            else printf("%d",w[i][j]%10);

        }
         printf("\n");
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;
int n,m;

int sea(int n,int m,char mat[][1001],bool vis[][1001],int x,int y)
{
    int ans=1;
    if(x>0 && mat[x-1][y]=='.' && !vis[x-1][y])
    {
        vis[x-1][y]=true;
        ans+=sea(n,m,mat,vis,x-1,y);
    }
    if(x<n-1 && mat[x+1][y]=='.' && !vis[x+1][y])
    {
        vis[x+1][y]=true;
        ans+=sea(n,m,mat,vis,x+1,y);
    }
    if(y>0 && mat[x][y-1]=='.' && !vis[x][y-1])
    {
        vis[x][y-1]=true;
        ans+=sea(n,m,mat,vis,x,y-1);
    }
    if(y<m-1 && mat[x][y+1]=='.' && !vis[x][y+1])
    {
        vis[x][y+1]=true;
        ans+=sea(n,m,mat,vis,x,y+1);
    }
    return (ans%10);
}

int main()
{

    cin>>n>>m;
    char mat[n][1001];

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            cin>>mat[i][j];
    }

    for(int i=0;i<n;i++)
    {
        string s;
        for(int j=0;j<m;j++)
        {
            if(mat[i][j]=='.') s+='.';
            else
            {
                bool vis[n][1001];
                for(int i=0;i<n;i++)
                {
                    memset(vis[i],false,m);
                }
                s+='0'+sea(n,m,mat,vis,i,j);
            }
        }
        cout<<s<<endl;
    }

    return 0;
}