#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long
#define FILE ""
#define pii pair <int,int>

using namespace std;

const int mod = 1e9 + 7;
const ll inf = 1e18;

char a[1005][1005];
int b[1005][1005],dp[1005][1005],us[1005][1005],mark[1005][1005];

int main() {
    ios_base::sync_with_stdio(0);
   // freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            cin >> a[i][j];
            b[i][j] = -1;
        }
    }
    int cur = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (!dp[i][j] && a[i][j] == '.') {
                cur++;
                queue <pii> q;
                vector <pii> good;
                int tmp = 1;
                us[i][j] = 1;
                good.pb({i,j});
                q.push({i,j});
                while (!q.empty()) {
                    int x = q.front().F, y = q.front().S;
                    q.pop();
                    if (a[x-1][y] == '.' && !us[x-1][y]) {
                        q.push({x-1,y});
                        good.pb({x-1,y});
                        us[x-1][y] = 1;
                        tmp++;

                    }
                    if (a[x+1][y] == '.' && !us[x+1][y]) {
                        q.push({x+1,y});
                        good.pb({x+1,y});
                        us[x+1][y] = 1;
                        tmp++;
                    }
                    if (a[x][y-1] == '.' && !us[x][y-1]) {
                        q.push({x,y-1});
                        good.pb({x,y-1});
                        us[x][y-1] = 1;
                        tmp++;
                    }
                    if (a[x][y+1] == '.' && !us[x][y+1]) {
                        q.push({x,y+1});
                        good.pb({x,y+1});
                        us[x][y+1] = 1;
                        tmp++;
                    }
                }
                for (auto x : good) {
                    dp[x.F][x.S] = tmp;
                    mark[x.F][x.S] = cur;
                }
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (a[i][j] == '*') {
                set <int> kek;
                int tmp = 0;
                if (i > 0 && a[i-1][j] == '.' && !kek.count(mark[i-1][j])) {
                    tmp += dp[i-1][j];
                    kek.insert(mark[i-1][j]);
                }
                if (i < n - 1 && a[i+1][j] == '.' && !kek.count(mark[i+1][j])) {
                    tmp += dp[i+1][j];
                    kek.insert(mark[i+1][j]);
                }
                if (j > 0 && a[i][j-1] == '.' && !kek.count(mark[i][j-1])) {
                    tmp += dp[i][j-1];
                    kek.insert(mark[i][j-1]);
                }
                if (j < m - 1 && a[i][j+1] == '.' && !kek.count(mark[i][j+1])) {
                    tmp += dp[i][j+1];
                    kek.insert(mark[i][j+1]);
                }
                cout << (tmp + 1) % 10;
                continue;
            }
            cout << a[i][j];
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long
#define FILE ""
#define pii pair <int,int>

using namespace std;

const int mod = 1e9 + 7;
const ll inf = 1e18;

char a[1005][1005];
int b[1005][1005],dp[1005][1005],us[1005][1005],mark[1005][1005];

int main() {
    ios_base::sync_with_stdio(0);
   // freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            cin >> a[i][j];
            b[i][j] = -1;
        }
    }
    int cur = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (!dp[i][j] && a[i][j] == '.') {
                cur++;
                queue <pii> q,good;
                int tmp = 1;
                us[i][j] = 1;
                good.push({i,j});
                q.push({i,j});
                while (!q.empty()) {
                    int x = q.front().F, y = q.front().S;
                    q.pop();
                    if (a[x-1][y] == '.' && !us[x-1][y]) {
                        q.push({x-1,y});
                        good.push({x-1,y});
                        us[x-1][y] = 1;
                        tmp++;

                    }
                    if (a[x+1][y] == '.' && !us[x+1][y]) {
                        q.push({x+1,y});
                        good.push({x+1,y});
                        us[x+1][y] = 1;
                        tmp++;
                    }
                    if (a[x][y-1] == '.' && !us[x][y-1]) {
                        q.push({x,y-1});
                        good.push({x,y-1});
                        us[x][y-1] = 1;
                        tmp++;
                    }
                    if (a[x][y+1] == '.' && !us[x][y+1]) {
                        q.push({x,y+1});
                        good.push({x,y+1});
                        us[x][y+1] = 1;
                        tmp++;
                    }
                }
                for (auto x : good) {
                    dp[x.F][x.S] = tmp;
                    mark[x.F][x.S] = cur;
                }
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (a[i][j] == '*') {
                set <int> kek;
                int tmp = 0;
                if (i > 0 && a[i-1][j] == '.' && !kek.count(mark[i-1][j])) {
                    tmp += dp[i-1][j];
                    kek.insert(mark[i-1][j]);
                }
                if (i < n - 1 && a[i+1][j] == '.' && !kek.count(mark[i+1][j])) {
                    tmp += dp[i+1][j];
                    kek.insert(mark[i+1][j]);
                }
                if (j > 0 && a[i][j-1] == '.' && !kek.count(mark[i][j-1])) {
                    tmp += dp[i][j-1];
                    kek.insert(mark[i][j-1]);
                }
                if (j < m - 1 && a[i][j+1] == '.' && !kek.count(mark[i][j+1])) {
                    tmp += dp[i][j+1];
                    kek.insert(mark[i][j+1]);
                }
                cout << (tmp + 1) % 10;
                continue;
            }
            cout << a[i][j];
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long
#define FILE ""
#define pii pair <int,int>

using namespace std;

const int mod = 1e9 + 7;
const ll inf = 1e18;

char a[1005][1005];
int b[1005][1005],dp[1005][1005],us[1005][1005],mark[1005][1005];

int main() {
    ios_base::sync_with_stdio(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            cin >> a[i][j];
            b[i][j] = -1;
        }
    }
    int cur = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (!dp[i][j] && a[i][j] == '.') {
                cur++;
                queue <pii> q,good;
                int tmp = 1;
                us[i][j] = 1;
                good.push({i,j});
                q.push({i,j});
                while (!q.empty()) {
                    int x = q.front().F, y = q.front().S;
                    q.pop();
                    if (a[x-1][y] == '.' && !us[x-1][y]) {
                        q.push({x-1,y});
                        good.push({x-1,y});
                        us[x-1][y] = 1;
                        tmp++;
                    }
                    if (a[x+1][y] == '.' && !us[x+1][y]) {
                        q.push({x+1,y});
                        good.push({x+1,y});
                        us[x+1][y] = 1;
                        tmp++;
                    }
                    if (a[x][y-1] == '.' && !us[x][y-1]) {
                        q.push({x,y-1});
                        good.push({x,y-1});
                        us[x][y-1] = 1;
                        tmp++;
                    }
                    if (a[x][y+1] == '.' && !us[x][y+1]) {
                        q.push({x,y+1});
                        good.push({x,y+1});
                        us[x][y+1] = 1;
                        tmp++;
                    }
                }
                while (!good.empty()) {
                    int x = good.front().F, y = good.front().S;
                    good.pop();
                    dp[x][y] = tmp;
                    mark[x][y] = cur;
                }
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (a[i][j] == '*') {
                set <int> kek;
                int tmp = 0;
                if (i > 0 && !kek.count(mark[i-1][j])) tmp += dp[i-1][j];
                kek.insert(mark[i-1][j]);
                if (i < n - 1 && !kek.count(mark[i+1][j])) tmp += dp[i+1][j];
                kek.insert(mark[i-1][j]);
                if (j > 0 && !kek.count(mark[i][j-1])) tmp += dp[i][j-1];
                kek.insert(mark[i][j-1]);
                if (j < m - 1 && !kek.count(mark[i][j+1])) tmp += dp[i][j+1];
                kek.insert(mark[i][j+1]);
                b[i][j] = tmp;
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (b[i][j] == -1) cout << a[i][j];
            else cout << b[i][j] + 1;
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define ll long long
#define FILE ""
#define pii pair <int,int>

using namespace std;

const int mod = 1e9 + 7;
const ll inf = 1e18;

char a[1005][1005];
int b[1005][1005],dp[1005][1005],us[1005][1005],mark[1005][1005];

int main() {
    ios_base::sync_with_stdio(0);
    //freopen("input.txt","r",stdin);
   // freopen("output.txt","w",stdout);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            cin >> a[i][j];
            b[i][j] = -1;
        }
    }
    int cur = 0;
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (!dp[i][j] && a[i][j] == '.') {
                cur++;
                queue <pii> q,good;
                int tmp = 1;
                us[i][j] = 1;
                good.push({i,j});
                q.push({i,j});
                while (!q.empty()) {
                    int x = q.front().F, y = q.front().S;
                    q.pop();
                    if (a[x-1][y] == '.' && !us[x-1][y]) {
                        q.push({x-1,y});
                        good.push({x-1,y});
                        us[x-1][y] = 1;
                        tmp++;

                    }
                    if (a[x+1][y] == '.' && !us[x+1][y]) {
                        q.push({x+1,y});
                        good.push({x+1,y});
                        us[x+1][y] = 1;
                        tmp++;
                    }
                    if (a[x][y-1] == '.' && !us[x][y-1]) {
                        q.push({x,y-1});
                        good.push({x,y-1});
                        us[x][y-1] = 1;
                        tmp++;
                    }
                    if (a[x][y+1] == '.' && !us[x][y+1]) {
                        q.push({x,y+1});
                        good.push({x,y+1});
                        us[x][y+1] = 1;
                        tmp++;
                    }
                }
                while (!good.empty()) {
                    int x = good.front().F, y = good.front().S;
                    good.pop();
                    dp[x][y] = tmp;
                    mark[x][y] = cur;
                }
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (a[i][j] == '*') {
                set <int> kek;
                int tmp = 0;
                if (i > 0 && !kek.count(mark[i-1][j])) tmp += dp[i-1][j];
                kek.insert(mark[i-1][j]);
                if (i < n - 1 && !kek.count(mark[i+1][j])) tmp += dp[i+1][j];
                kek.insert(mark[i+1][j]);
                if (j > 0 && !kek.count(mark[i][j-1])) tmp += dp[i][j-1];
                kek.insert(mark[i][j-1]);
                if (j < m - 1 && !kek.count(mark[i][j+1])) tmp += dp[i][j+1];
                kek.insert(mark[i][j+1]);
                b[i][j] = tmp;
            }
        }
    }
    for (int i=0; i<n; i++) {
        for (int j=0; j<m; j++) {
            if (b[i][j] == -1) cout << a[i][j];
            else cout << (b[i][j] + 1) % 10;
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000*1001];
int c;
int dfs(int i, int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return 0;
	vis[i][j] = c;
	int cnt = 1;
	for (int k = 0; k < 4; k++)
	{
		cnt += dfs((i + dx[k]), (j + dy[k]));
	}
	return cnt;
}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

			cin >> ch[i][j];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			c++;
			tp[c] = dfs(i, j);
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

			if (ch[i][j] == '*')
			{
				int sum = 1;
				set<int>s;
				for (int k = 0; k < 4; k++)
				{
					s. insert(vis[i + dx[k]][j + dy[k]]);
				}
				for (auto t : s)sum += tp[t];
				sum %= 10;
				ch[i][j] = '0' + sum;
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cout << ch[i][j];
		cout << endl;
	}

}


#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dy[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dx[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1001][1001];
int  c, vis[1001][1001], tp[1000001];

int dfs(int i, int j) {
	if (!valid(i,j)|| vis[i][j] || ch[i][j] == '*') return 0;
	vis[i][j] = c;
	int cnt = 1;
	for (int k = 0; k < 4;k++) cnt += dfs(i + dx[k], j + dy[k]);
	return cnt;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) scanf(" %s", ch[i]);
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)tp[++c] = dfs(i, j);

	}
		for (int i = 0; i < n; i++)

		{
			for (int j = 0; j < m; j++)
				if (ch[i][j] == '*') {
			set<int> a;
		for(int k=0;k<4;k++)
			a.insert(vis[i + dx[k]][j+dy[k ]]);
			int z = 1;
			for (auto x : a) z += tp[x];
			ch[i][j] = '0' + (z % 10);
		}
		printf("%s\n", ch[i]);
	}
}


#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000001];
int c;
int dfs(int i,int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return 0;
	vis[i][j] = c;
	int cnt = 1;
	for (int k = 0; k < 4; k++)
	{
		cnt += dfs(i + dx[k], j + dy[k]);
	}
	return cnt;
}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

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

			tp[c++] = dfs(i, j);
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			int sum = 1;
			if (ch[i][j] ==  '*')
			{
				for (int k = 0; k < 4; k++)
				{
					sum += tp[vis[i + dx[k]][j + dy[k]]];
				}
				ch[i][j] = '0'+(sum % 10);
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)cout << ch[i][j];
		cout << endl;
	}

}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000 * 1009];
int c,cnt;
void dfs(int i, int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return ;
	vis[i][j] = c;
	cnt++;
	for (int k = 0; k < 4; k++)
	{
		 dfs((i + dx[k]), (j + dy[k]));
	}

}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

			cin >> ch[i][j];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			c++;
			 dfs(i, j);
			 tp[c] = cnt;
			 cnt = 0;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

			if (ch[i][j] == '*')
			{
				int sum = 1;
				set<int>s;
				for (int k = 0; k < 4; k++)
				{
					if (valid(i + dx[k], j + dy[k]))
					s.insert(vis[i + dx[k]][j + dy[k]]);
				}
				for (auto t : s)sum += tp[t];
				sum %= 10;
				ch[i][j] = (char)('0' + sum);
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cout << ch[i][j];
		cout << endl;
	}

}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000 * 1001];
int c,cnt;
void dfs(int i, int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return ;
	vis[i][j] = c;
	cnt++;
	for (int k = 0; k < 4; k++)
	{
		 dfs((i + dx[k]), (j + dy[k]));
	}

}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

			cin >> ch[i][j];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			c++;
			 dfs(i, j);
			 tp[c] = cnt;
			 cnt = 0;
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

			if (ch[i][j] == '*')
			{
				int sum = 1;
				set<int>s;
				for (int k = 0; k < 4; k++)
				{
					s.insert(vis[i + dx[k]][j + dy[k]]);
				}
				for (auto t : s)sum += tp[t];
				sum %= 10;
				ch[i][j] = (char)('0' + sum);
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cout << ch[i][j];
		cout << endl;
	}

}


#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000*1001];
int c;
int dfs(int i, int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return 0;
	vis[i][j] = c;
	int cnt = 1;
	for (int k = 0; k < 4; k++)
	{
		cnt += dfs((i + dx[k]), (j + dy[k]));
	}
	return cnt;
}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

			cin >> ch[i][j];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			c++;
			tp[c] = dfs(i, j);
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

			if (ch[i][j] == '*')
			{
				int sum = 1;
				set<int>s;
				for (int k = 0; k < 4; k++)
				{
					s. insert(vis[i + dx[k]][j + dy[k]]);
				}
				for (auto t : s)sum += tp[t];
				sum %= 10;
				ch[i][j] = char( sum);
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cout << ch[i][j];
		cout << endl;
	}

}


#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000001];
int c;
int dfs(int i,int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return 0;
	vis[i][j] = c;
	int cnt = 1;
	for (int k = 0; k < 4; k++)
	{
		cnt += dfs(i + dx[k], j + dy[k]);
	}
	return cnt;
}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

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

			tp[c++] = dfs(i, j);
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

			if (ch[i][j] ==  '*')

			{
			int sum = 1;
				for (int k = 0; k < 4; k++)
				{
					sum += tp[vis[i + dx[k]][j + dy[k]]];
				}
				ch[i][j] = '0'+(sum % 10);
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)cout << ch[i][j];
		cout << endl;
	}

}


#include<bits/stdc++.h>
#include <stdlib.h>
#include<assert.h>
using namespace std;
#define PI 3.14159265359
#define OO 1e9
#define S second
#define F first
#define MP                  make_pair
#define sz(v) ( (int) (v.size()) )
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
int dx[] = { 0, 0, -1, 1, 1, 1, -1, -1 };
int dy[] = { 1, -1, 0, 0, 1, -1, 1, -1 };
void fast()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int n, m;
bool valid(int i, int j)
{
	return i >= 0 && i < n&&j >= 0 && j < m;
}
/*
كُن مُشعًّا ، كُن شخصًا يُستضاءُ بهِ أملًا ، و اصنع مِن نفسك روحًا يُحبّها الله 💜
*/
char ch[1009][1009];
int vis[1009][1009];
int tp[1000001];
int c;
int dfs(int i, int j)
{
	if (!valid(i, j) || vis[i][j] || ch[i][j] == '*')
		return 0;
	vis[i][j] = c;
	int cnt = 1;
	for (int k = 0; k < 4; k++)
	{
		cnt += dfs((i + dx[k]), (j + dy[k]));
	}
	return cnt;
}
int main()
{
	fast();

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{

		for (int j = 0; j < m; j++)

			cin >> ch[i][j];
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			c++;
			tp[c] = dfs(i, j);
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{

			if (ch[i][j] == '*')
			{
				int sum = 1;
				set<int>s;
				for (int k = 0; k < 4; k++)
				{
					s. insert(vis[i + dx[k]][j + dy[k]]);
				}
				for (auto t : s)sum += tp[t];
				sum %= 10;
				ch[i][j] = char('0' + sum);
			}
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
			cout << ch[i][j];
		cout << endl;
	}

}


#include <bits/stdc++.h>
#define a first
#define b second
using namespace std;
char arr[1010][1010];
int cost[1010][1010];
pair<pair<int,int>,bool> collect[1010][1010];
int n,m;
pair<int,int> node;
void dfs(int i,int j){
  if(i < 0 || i >= n || j < 0 || j >= n || arr[i][j] != '.' ||collect[i][j].b)return;
  collect[i][j].b = 1;cost[node.a][node.b]++;
  collect[i][j].a = node;
  dfs(i+1,j);dfs(i-1,j);dfs(i,j+1);dfs(i,j-1);
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '.'){
        node = make_pair(i,j);
        dfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  //for(int i=0;i<n;i++){for(int j=0;j<m;j++)printf("%d ",cost[i][j]);printf("\n");}
  int sum = 0;
  set<pair<int,int> > q;
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '.')printf(".");
      else{
        if(i<n-1 && arr[i+1][j]=='.')q.insert(collect[i+1][j].a);
        if(i > 0 && arr[i-1][j]=='.')q.insert(collect[i-1][j].a);
        if(j<m-1 && arr[i][j+1]=='.')q.insert(collect[i][j+1].a);
        if(j > 0 && arr[i][j-1]=='.')q.insert(collect[i][j-1].a);
        for(auto p:q)sum+=cost[p.a][p.b];
        printf("%d",(sum+1)%10);
        sum = 0;q.clear();
      }
    }
    printf("\n");
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
int mark = 1;
char arr[1010][1010];
int test[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  int fillit = mark;mark++;
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && arr[x+1][y] == '.' && test[x+1][y] != fillit){test[x+1][y] = fillit;solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && arr[x-1][y] == '.' && test[x-1][y] != fillit){test[x-1][y] = fillit;solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && arr[x][y+1] == '.' && test[x][y+1] != fillit){test[x][y+1] = fillit;solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && arr[x][y-1] == '.' && test[x][y-1] != fillit){test[x][y-1] = fillit;solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char arr[1010][1010],test[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  for(int k=0;k<n;k++)strcpy(test[k],arr[k]);
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && test[x+1][y] == '.'){test[x+1][y] = '*';solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && test[x-1][y] == '.'){test[x-1][y] = '*';solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && test[x][y+1] == '.'){test[x][y+1] = '*';solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && test[x][y-1] == '.'){test[x][y-1] = '*';solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char arr[1010][1010],test[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  for(int k=0;k<n;k++)strcpy(test[k],arr[k]);
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && test[x+1][y] == '.'){test[x+1][y] = '*';solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && test[x-1][y] == '.'){test[x-1][y] = '*';solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && test[x][y+1] == '.'){test[x][y+1] = '*';solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && test[x][y-1] == '.'){test[x][y-1] = '*';solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char mark = '0';
char arr[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  char fillit = mark;mark++;
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && arr[x+1][y] != '*' && arr[x+1][y] != fillit){arr[x+1][y] = fillit;solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && arr[x-1][y] != '*' && arr[x-1][y] != fillit){arr[x-1][y] = fillit;solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && arr[x][y+1] != '*' && arr[x][y+1] != fillit){arr[x][y+1] = fillit;solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && arr[x][y-1] != '*' && arr[x][y-1] != fillit){arr[x][y-1] = fillit;solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char mark = '0';
char arr[1010][1010],test[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  char fillit = mark;if(mark == 'z'){mark = '0';strcpy(test[i],arr[i]);}mark++;
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && test[x+1][y] != '*' && test[x+1][y] != fillit){test[x+1][y] = fillit;solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && test[x-1][y] != '*' && test[x-1][y] != fillit){test[x-1][y] = fillit;solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && test[x][y+1] != '*' && test[x][y+1] != fillit){test[x][y+1] = fillit;solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && test[x][y-1] != '*' && test[x][y-1] != fillit){test[x][y-1] = fillit;solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++){scanf("%s",&arr[i]);strcpy(test[i],arr[i]);}
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char mark = '0';
char arr[1010][1010],test[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  char fillit = mark;mark++;
  for(int k=0;k<n;k++)strcpy(test[k],arr[k]);
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && test[x+1][y] != '*' && test[x+1][y] != fillit){test[x+1][y] = fillit;solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && test[x-1][y] != '*' && test[x-1][y] != fillit){test[x-1][y] = fillit;solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && test[x][y+1] != '*' && test[x][y+1] != fillit){test[x][y+1] = fillit;solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && test[x][y-1] != '*' && test[x][y-1] != fillit){test[x][y-1] = fillit;solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


#include <bits/stdc++.h>
#define a first
#define b second
using namespace std;
char arr[1010][1010];
int cost[1010][1010];
pair<pair<int,int>,bool> collect[1010][1010];
int n,m;
pair<int,int> node;
void dfs(int i,int j){
  if(i < 0 || i >= n || j < 0 || j >= m || arr[i][j] != '.' ||collect[i][j].b)return;
  collect[i][j].b = 1;cost[node.a][node.b]++;
  collect[i][j].a = node;
  dfs(i+1,j);dfs(i-1,j);dfs(i,j+1);dfs(i,j-1);
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '.'){
        node = make_pair(i,j);
        dfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  //for(int i=0;i<n;i++){for(int j=0;j<m;j++)printf("%d ",cost[i][j]);printf("\n");}
  int sum = 0;
  set<pair<int,int> > q;
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '.')printf(".");
      else{
        if(i<n-1 && arr[i+1][j]=='.')q.insert(collect[i+1][j].a);
        if(i > 0 && arr[i-1][j]=='.')q.insert(collect[i-1][j].a);
        if(j<m-1 && arr[i][j+1]=='.')q.insert(collect[i][j+1].a);
        if(j > 0 && arr[i][j-1]=='.')q.insert(collect[i][j-1].a);
        for(auto p:q)sum+=cost[p.a][p.b];
        printf("%d",(sum+1)%10);
        sum = 0;q.clear();
      }
    }
    printf("\n");
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char arr[1010][1010],test[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  for(int k=0;k<n;k++)strcpy(test[k],arr[k]);
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && test[x+1][y] != '*' && test[x+1][y] != i*j){test[x+1][y] = i*j;solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && test[x-1][y] != '*' && test[x-1][y] != i*j){test[x-1][y] = i*j;solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && test[x][y+1] != '*' && test[x][y+1] != i*j){test[x][y+1] = i*j;solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && test[x][y-1] != '*' && test[x][y-1] != i*j){test[x][y-1] = i*j;solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m;
char mark = '0';
char arr[1010][1010];
int solve[1010][1010];
void bfs(int i,int j){
  char fillit = mark;mark++;if(mark == 'z')mark = '0';
  queue<pair<int,int> > q;int x,y;
  q.push(make_pair(i,j));
  while(!q.empty()){
    pair<int,int> h = q.front();q.pop();x = h.first;y = h.second;
    if(x+1 < n && arr[x+1][y] != '*' && arr[x+1][y] != fillit){arr[x+1][y] = fillit;solve[i][j]++;q.push(make_pair(x+1,y));}
    if(x-1 >=0 && arr[x-1][y] != '*' && arr[x-1][y] != fillit){arr[x-1][y] = fillit;solve[i][j]++;q.push(make_pair(x-1,y));}
    if(y+1 < m && arr[x][y+1] != '*' && arr[x][y+1] != fillit){arr[x][y+1] = fillit;solve[i][j]++;q.push(make_pair(x,y+1));}
    if(y-1 >=0 && arr[x][y-1] != '*' && arr[x][y-1] != fillit){arr[x][y-1] = fillit;solve[i][j]++;q.push(make_pair(x,y-1));}
  }
}
void run(){
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(arr[i][j] == '*'){
        solve[i][j] = 1;
        bfs(i,j);
      }
    }
  }
}
int main(){
  scanf("%d%d",&n,&m);
  for(int i=0;i<n;i++)scanf("%s",&arr[i]);
  run();
  for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(arr[i][j]=='*')printf("%d",solve[i][j]%10);else printf(".");}printf("\n");}
  //for(int i=0;i<n;i++)printf("%s\n",test[i]);
  return 0;
}


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

using namespace std;

const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
bool check(int x, int y){
	if(x>=0 && y>=0 && x<r && y<c){
		if(arr[x][y]=='*')
			return false;

		return true;
	}
	return false;
}

void dfs(int x,int y){
	mark[x][y] = true;

	if(check(x-1,y) && !mark[x-1][y]){
		cnt++;
		dfs(x-1,y);
	}
	if(check(x+1,y) && !mark[x+1][y]){
		cnt++;
		dfs(x+1,y);
	}
	if(check(x,y-1) && !mark[x][y-1]){
		cnt++;
		dfs(x,y-1);
	}
	if(check(x,y+1) && !mark[x][y+1]){
		cnt++;
		dfs(x,y+1);
	}
}

int main(){

	cin>>r>>c;

	for(int i=0;i<r;i++)
		for (int j=0;j<c;j++)
			cin>>arr[i][j];

	for(int i=0;i<r;i++)
		for(int j=0;j<c ;j++){
			if(arr[i][j]=='*'){
				cnt =0;
				memset(mark,false,sizeof(mark));
				dfs(i,j);
				result[i][j]=(cnt+1)%10;
			}
			else{
				result[i][j] = -1;
			}
		}

		for(int i=0;i<r;i++){
			for (int j=0;j<c;j++){
				if(result[i][j]==-1)
					cout<<".";
				else cout<<result[i][j];
			}
			cout<<endl;
		}
}


#include <iostream>
#include <vector>
#include <utility>
#include <string.h>
using namespace std;
const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
vector <pair<int , int> > cn[MAX_N];
bool check(int x, int y){
    if(x>=0 && y>=0 && x<r && y<c){
        if(arr[x][y]=='*')
            return false;

        return true;
    }
    return false;
}

void dfs(int x,int y , int p){
    mark[x][y] = true;
    cn[p].push_back(make_pair(x , y));
    if(check(x-1,y) && !mark[x-1][y]){
        cnt++;
        dfs(x-1,y , p);
    }
    if(check(x+1,y) && !mark[x+1][y]){
        cnt++;
        dfs(x+1,y,p);
    }
    if(check(x,y-1) && !mark[x][y-1]){
        cnt++;
        dfs(x,y-1,p);
    }
    if(check(x,y+1) && !mark[x][y+1]){
        cnt++;
        dfs(x,y+1 , p);
    }

}
int main(){
    cin>>r>>c;

    for(int i=0;i<r;i++)
        for (int j=0;j<c;j++){
        	cin>>arr[i][j];
			if(arr[i][j]=='.')result[i][j]=-1;
        }

    int k =0;
    for(int i =0 ; i<r ; i++)
        for(int j=0 ; j<c ; j++){
            if(arr[i][j]!='*' && !mark[i][j]){
                dfs(i, j ,k);
                k++;
            }
        }

    int x , y;
    for(int i =0 ; i<k ; i++){
		memset(mark , false , sizeof(mark));
        for(int j =0 ; j<cn[i].size() ; j++){
            x = cn[i][j].first;
            y = cn[i][j].second;
            if(x-1>=0 && y>=0 && x-1<r && y<c &&arr[x-1][y]=='*'&&!mark[x-1][y]){
                result[x-1][y]+=cn[i].size();
				mark[x-1][y]=true;
            }
            if(x+1>=0 && y>=0 && x+1<r && y<c &&arr[x+1][y]=='*'&&!mark[x+1][y]){
                result[x+1][y]+=cn[i].size();
				mark[x+1][y]=true;
            }
            if(x>=0 && y+1>=0 && x<r && y+1<c &&arr[x][y+1]=='*'&&!mark[x][y+1]){
                result[x][y+1]+=cn[i].size();
				mark[x][y+1]=true;
            }
            if(x>=0 && y-1>=0 && x<r && y-1<c &&arr[x][y-1]=='*'&& !mark[x][y-1]){
                result[x][y-1]+=cn[i].size();
				mark[x][y-1]=true;
            }
        }
    }

    for(int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            if(result[i][j]==-1)
                cout<<".";
            else cout<<(result[i][j]+1)%10;
        }
        cout<<endl;
    }

    return 0;}


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

using namespace std;

const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
bool check(int x, int y){
	if(x>=0 && y>=0 && x<r && y<c){
		if(arr[x][y]=='*')
			return false;

		return true;
	}
	return false;
}

void dfs(int x,int y){
	mark[x][y] = true;

	if(check(x-1,y) && !mark[x-1][y]){
		cnt++;
		dfs(x-1,y);
	}
	if(check(x+1,y) && !mark[x+1][y]){
		cnt++;
		dfs(x+1,y);
	}
	if(check(x,y-1) && !mark[x][y-1]){
		cnt++;
		dfs(x,y-1);
	}
	if(check(x,y+1) && !mark[x][y+1]){
		cnt++;
		dfs(x,y+1);
	}
}

int main(){

	cin>>r>>c;

	for(int i=0;i<r;i++)
		for (int j=0;j<c;j++)
			cin>>arr[i][j];

	for(int i=0;i<r;i++)
		for(int j=0;j<c ;j++){
			if(arr[i][j]=='*'){
				cnt =0;
				memset(mark,false,sizeof(mark));
				dfs(i,j);
				result[i][j]=cnt+1;
			}
			else{
				result[i][j] = -1;
			}
		}

		for(int i=0;i<r;i++){
			for (int j=0;j<c;j++){
				if(result[i][j]==-1)
					cout<<".";
				else cout<<result[i][j];
			}
			cout<<endl;
		}
}


#include <iostream>
#include <vector>
#include <utility>
#include <string.h>
using namespace std;
const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
vector <pair<int , int> > cn[MAX_N];
bool check(int x, int y){
    if(x>=0 && y>=0 && x<r && y<c){
        if(arr[x][y]=='*')
            return false;

        return true;
    }
    return false;
}

void dfs(int x,int y , int p){
    mark[x][y] = true;
    cn[p].push_back(make_pair(x , y));
    if(check(x-1,y) && !mark[x-1][y]){
        cnt++;
        dfs(x-1,y , p);
    }
    if(check(x+1,y) && !mark[x+1][y]){
        cnt++;
        dfs(x+1,y,p);
    }
    if(check(x,y-1) && !mark[x][y-1]){
        cnt++;
        dfs(x,y-1,p);
    }
    if(check(x,y+1) && !mark[x][y+1]){
        cnt++;
        dfs(x,y+1 , p);
    }

}
int main(){
    cin>>r>>c;

    for(int i=0;i<r;i++)
        for (int j=0;j<c;j++)
            cin>>arr[i][j];
    int k =0;
    for(int i =0 ; i<r ; i++)
        for(int j=0 ; j<c ; j++){
            if(arr[i][j]!='*' && !mark[i][j]){
                dfs(i, j ,k);
                k++;
            }
        }

    int x , y;
    for(int i =0 ; i<k ; i++){
		memset(mark , false , sizeof(mark));
        for(int j =0 ; j<cn[i].size() ; j++){
            x = cn[i][j].first;
            y = cn[i][j].second;
            if(x-1>=0 && y>=0 && x-1<r && y<c &&arr[x-1][y]=='*'&&!mark[x-1][y]){
                result[x-1][y]+=cn[i].size();
				mark[x-1][y]=true;
            }
            if(x+1>=0 && y>=0 && x+1<r && y<c &&arr[x+1][y]=='*'&&!mark[x+1][y]){
                result[x+1][y]+=cn[i].size();
				mark[x+1][y]=true;
            }
            if(x>=0 && y+1>=0 && x<r && y+1<c &&arr[x][y+1]=='*'&&!mark[x][y+1]){
                result[x][y+1]+=cn[i].size();
				mark[x][y+1]=true;
            }
            if(x>=0 && y-1>=0 && x<r && y-1<c &&arr[x][y-1]=='*'&& !mark[x][y-1]){
                result[x][y-1]+=cn[i].size();
				mark[x][y-1]=true;
            }
        }
    }

    for(int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            if(result[i][j]==0)
                cout<<".";
            else cout<<(result[i][j]+1)%10;
        }
        cout<<endl;
    }

    return 0;}


#include <iostream>
#include <vector>
#include <utility>
#include <string.h>
using namespace std;
const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
vector <pair<int , int> > cn[1000000];
bool check(int x, int y){
    if(x>=0 && y>=0 && x<r && y<c){
        if(arr[x][y]=='*')
            return false;

        return true;
    }
    return false;
}

void dfs(int x,int y , int p){
    mark[x][y] = true;
    cn[p].push_back(make_pair(x , y));
    if(check(x-1,y) && !mark[x-1][y]){
        cnt++;
        dfs(x-1,y , p);
    }
    if(check(x+1,y) && !mark[x+1][y]){
        cnt++;
        dfs(x+1,y,p);
    }
    if(check(x,y-1) && !mark[x][y-1]){
        cnt++;
        dfs(x,y-1,p);
    }
    if(check(x,y+1) && !mark[x][y+1]){
        cnt++;
        dfs(x,y+1 , p);
    }

}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    cin>>r>>c;
    for(int i=0;i<r;i++)
        for (int j=0;j<c;j++){
        	cin>>arr[i][j];
			if(arr[i][j]=='.')result[i][j]=-1;
        }

    int k =0;
    for(int i =0 ; i<r ; i++)
        for(int j=0 ; j<c ; j++){
            if(arr[i][j]!='*' && !mark[i][j]){
                dfs(i, j ,k);
                k++;
            }
        }

    int x , y;
	vector<pair<int, int> > vv;
	memset(mark , false , sizeof(mark));
    for(int i =0 ; i<k ; i++){
		for(int i =0 ;i<vv.size() ; i++){
            x = vv[i].first;
            y = vv[i].second;
			mark[x][y]=false;

		}
		vv.clear();
        for(int j =0 ; j<cn[i].size() ; j++){
            x = cn[i][j].first;
            y = cn[i][j].second;
            if(x-1>=0 && y>=0 && x-1<r && y<c &&arr[x-1][y]=='*'&&!mark[x-1][y]){
                result[x-1][y]+=cn[i].size();
				mark[x-1][y]=true;
				vv.push_back(make_pair(x-1 , y));
            }
            if(x+1>=0 && y>=0 && x+1<r && y<c &&arr[x+1][y]=='*'&&!mark[x+1][y]){
                result[x+1][y]+=cn[i].size();
				mark[x+1][y]=true;
				vv.push_back(make_pair(x+1 , y));
            }
            if(x>=0 && y+1>=0 && x<r && y+1<c &&arr[x][y+1]=='*'&&!mark[x][y+1]){
                result[x][y+1]+=cn[i].size();
				mark[x][y+1]=true;
				vv.push_back(make_pair(x , y+1));
            }
            if(x>=0 && y-1>=0 && x<r && y-1<c &&arr[x][y-1]=='*'&& !mark[x][y-1]){
                result[x][y-1]+=cn[i].size();
				mark[x][y-1]=true;
				vv.push_back(make_pair(x , y-1));
            }
        }
    }

    for(int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            if(result[i][j]==-1)
                cout<<".";
            else cout<<(result[i][j]+1)%10;
        }
        cout<<endl;
    }

    return 0;}


#include <iostream>
#include <vector>
#include <utility>
#include <string.h>
using namespace std;
const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
vector <pair<int , int> > cn[1000000];
bool check(int x, int y){
    if(x>=0 && y>=0 && x<r && y<c){
        if(arr[x][y]=='*')
            return false;

        return true;
    }
    return false;
}

void dfs(int x,int y , int p){
    mark[x][y] = true;
    cn[p].push_back(make_pair(x , y));
    if(check(x-1,y) && !mark[x-1][y]){
        cnt++;
        dfs(x-1,y , p);
    }
    if(check(x+1,y) && !mark[x+1][y]){
        cnt++;
        dfs(x+1,y,p);
    }
    if(check(x,y-1) && !mark[x][y-1]){
        cnt++;
        dfs(x,y-1,p);
    }
    if(check(x,y+1) && !mark[x][y+1]){
        cnt++;
        dfs(x,y+1 , p);
    }

}
int main(){
    cin>>r>>c;

    for(int i=0;i<r;i++)
        for (int j=0;j<c;j++){
        	cin>>arr[i][j];
			if(arr[i][j]=='.')result[i][j]=-1;
        }

    int k =0;
    for(int i =0 ; i<r ; i++)
        for(int j=0 ; j<c ; j++){
            if(arr[i][j]!='*' && !mark[i][j]){
                dfs(i, j ,k);
                k++;
            }
        }

    int x , y;
    for(int i =0 ; i<k ; i++){
		memset(mark , false , sizeof(mark));
        for(int j =0 ; j<cn[i].size() ; j++){
            x = cn[i][j].first;
            y = cn[i][j].second;
            if(x-1>=0 && y>=0 && x-1<r && y<c &&arr[x-1][y]=='*'&&!mark[x-1][y]){
                result[x-1][y]+=cn[i].size();
				mark[x-1][y]=true;
            }
            if(x+1>=0 && y>=0 && x+1<r && y<c &&arr[x+1][y]=='*'&&!mark[x+1][y]){
                result[x+1][y]+=cn[i].size();
				mark[x+1][y]=true;
            }
            if(x>=0 && y+1>=0 && x<r && y+1<c &&arr[x][y+1]=='*'&&!mark[x][y+1]){
                result[x][y+1]+=cn[i].size();
				mark[x][y+1]=true;
            }
            if(x>=0 && y-1>=0 && x<r && y-1<c &&arr[x][y-1]=='*'&& !mark[x][y-1]){
                result[x][y-1]+=cn[i].size();
				mark[x][y-1]=true;
            }
        }
    }

    for(int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            if(result[i][j]==-1)
                cout<<".";
            else cout<<(result[i][j]+1)%10;
        }
        cout<<endl;
    }

    return 0;}


#include <iostream>
#include <vector>
#include <utility>
using namespace std;
const int MAX_N = 1e3 + 20;
int r , c,cnt;
char arr[MAX_N][MAX_N];
int result[MAX_N][MAX_N];
bool mark[MAX_N][MAX_N];
vector <pair<int , int> > cn[MAX_N];
bool check(int x, int y){
    if(x>=0 && y>=0 && x<r && y<c){
        if(arr[x][y]=='*')
            return false;

        return true;
    }
    return false;
}

void dfs(int x,int y , int p){
    mark[x][y] = true;
    cn[p].push_back(make_pair(x , y));
    if(check(x-1,y) && !mark[x-1][y]){
        cnt++;
        dfs(x-1,y , p);
    }
    if(check(x+1,y) && !mark[x+1][y]){
        cnt++;
        dfs(x+1,y,p);
    }
    if(check(x,y-1) && !mark[x][y-1]){
        cnt++;
        dfs(x,y-1,p);
    }
    if(check(x,y+1) && !mark[x][y+1]){
        cnt++;
        dfs(x,y+1 , p);
    }

}
int main(){
    cin>>r>>c;

    for(int i=0;i<r;i++)
        for (int j=0;j<c;j++)
            cin>>arr[i][j];
    int k =0;
    for(int i =0 ; i<r ; i++)
        for(int j=0 ; j<c ; j++){
            if(arr[i][j]!='*' && !mark[i][j]){
                dfs(i, j ,k);
                k++;
            }
        }

    int x , y;
    for(int i =0 ; i<k ; i++){
		memset(mark , false , sizeof(mark));
        for(int j =0 ; j<cn[i].size() ; j++){
            x = cn[i][j].first;
            y = cn[i][j].second;
            if(x-1>=0 && y>=0 && x-1<r && y<c &&arr[x-1][y]=='*'&&!mark[x-1][y]){
                result[x-1][y]+=cn[i].size();
				mark[x-1][y]=true;
            }
            if(x+1>=0 && y>=0 && x+1<r && y<c &&arr[x+1][y]=='*'&&!mark[x+1][y]){
                result[x+1][y]+=cn[i].size();
				mark[x+1][y]=true;
            }
            if(x>=0 && y+1>=0 && x<r && y+1<c &&arr[x][y+1]=='*'&&!mark[x][y+1]){
                result[x][y+1]+=cn[i].size();
				mark[x][y+1]=true;
            }
            if(x>=0 && y-1>=0 && x<r && y-1<c &&arr[x][y-1]=='*'&& !mark[x][y-1]){
                result[x][y-1]+=cn[i].size();
				mark[x][y-1]=true;
            }
        }
    }

    for(int i=0;i<r;i++){
        for (int j=0;j<c;j++){
            if(result[i][j]==0)
                cout<<".";
            else cout<<(result[i][j]+1)%10;
        }
        cout<<endl;
    }

    return 0;}


#include <bits/stdc++.h>
#include <set>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
int t[100002];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	if (sum==10) sum=0;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

long long F(int x,int y){
	long long Ans=0;

	set <int> s;

	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'){
			s.insert(ans[xx][yy]);
		}
	}
	for (set<int>::iterator it=s.begin();it!=s.end();it++){
		Ans+=t[*it];
	}
	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (g[i][j]=='*'){
				printf("%lld",(F(i,j)+1)%10);
			}
			else printf(".");
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[10005];
int t[10005];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

long long F(int x,int y){
	long long Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (g[i][j]=='*'){
				printf("%lld",(F(i,j)+1)%10);
			}
			else printf(".");
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[100000];
int t[100000];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	if (sum==10) sum=0;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

long long F(int x,int y){
	long long Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (g[i][j]=='*'){
				printf("%lld",(F(i,j)+1)%10);
			}
			else printf(".");
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[1005*1005];
int t[1005*1005];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

int F(int x,int y){
	int Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			scanf("%c",&g[i][j]);
		}
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

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


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[100005];
int t[100005];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

int F(int x,int y){
	int Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

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


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[10005];
int t[10005];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	if (sum==10) sum=0;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

long long F(int x,int y){
	long long Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (g[i][j]=='*'){
				printf("%lld",(F(i,j)+1)%10);
			}
			else printf(".");
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[100002];
int t[100002];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	if (sum==10) sum=0;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

long long F(int x,int y){
	long long Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (g[i][j]=='*'){
				printf("%lld",(F(i,j)+1)%10);
			}
			else printf(".");
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
#include <set>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
int t[1000002];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	if (sum==10) sum=0;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

long long F(int x,int y){
	long long Ans=0;

	set <int> s;

	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'){
			s.insert(ans[xx][yy]);
		}
	}
	for (set<int>::iterator it=s.begin();it!=s.end();it++){
		Ans+=t[*it];
	}
	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (g[i][j]=='*'){
				printf("%lld",(F(i,j)+1)%10);
			}
			else printf(".");
		}
		printf("\n");
	}
}


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[10005];
int t[10005];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

int F(int x,int y){
	int Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

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


#include <bits/stdc++.h>
using namespace std;

int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int ans[1005][1005];
char g[1005][1005];
bool vis[1005][1005];
bool used[1005*1005];
int t[1005*1005];
int sum;
int part=0;

void dfs(int x,int y){
	sum++;
	vis[x][y]=true;
	ans[x][y]=part;
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!vis[xx][yy]){
			dfs(xx,yy);
		}
	}
}

int F(int x,int y){
	int Ans=0;

	memset(used,0,sizeof(used));
	for (int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if (xx>=0&&xx<n&&yy>=0&&yy<m&&g[xx][yy]=='.'&&!used[ans[xx][yy]]){
			Ans+=t[ans[xx][yy]];
			used[ans[xx][yy]]=1;
		}
	}

	return Ans;
}
int main(){

	cin >> n >> m;
	for (int i=0;i<n;i++){
			scanf("%s",g[i]);
	}
	memset(vis,false,sizeof(vis));
	memset(t,0,sizeof(t));
	for (int i=0;i<n;i++){
		for (int j=0;j<m;j++){
			if (!vis[i][j]&&g[i][j]=='.'){
				sum=0;
				dfs(i,j);
				t[part++]=sum;
			}
		}
	}

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


#include<fstream>
using namespace std;
int main()
{
    ifstream in("input.txt");
    ofstream out("output.txt");
    int a[1000]={},Max=0;
    int tests;
    in>>tests;
    for(int i=0;i<tests;i++)
    {
        int m,d,p,t;
        in>>d>>p>>t;
        for(int j=0;j<t;j++)
            Max=max(Max,a[d+100-j]+=p);
    }
        out<<Max;
    return 0;
}


#include<fstream>
using namespace std;
int main()
{
    ifstream in("input.txt");
    ofstream out("output.txt");
    int mt[12]={0,31,59,90,120,151,181,212,243,273,304,334};
    int a[1000]={},Max=0;
    int tests;
    in>>n;
    for(int i=0;i<n;i++)
    {
        int m,d,p,t;
        in>>m>>d>>p>>t;
        m--;
        for(int j=0;j<t;j++)
            Max=max(Max,a[mt[m]+d+100-j]+=p);
    }
        out<<Max;
    return 0;
}


#include<fstream>
#include<iostream>
#include<map>
using namespace std;
int main()
{
    ifstream in("input.txt");
    ofstream out("output.txt");
    int month[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    int tests,MAX=0,hobo[600]={0};
    int m,d,p,t,d1;
    cin>>tests;

    while(tests--)
    {

        cin>>m>>d>>p>>t;
        m--;
        d1=100+month[m]+d;
for(int j = 1; j <= t; ++j) {
            hobo[d1- j] += p;
            if(hobo[d1- j] > MAX)
                MAX = hobo[d1-j];
        }

}

    cout<<MAX<<endl;
    return 0;
}


#include<fstream>
using namespace std;
int main()
{
    ifstream in("input.txt");
    ofstream out("output.txt");
    int a[1000]={},Max=0;
    int tests;
    in>>tests;
    for(int i=0;i<tests;i++)
    {
        int m,d,p,t;
        in>>m>>d>>p>>t;
        for(int j=0;j<t;j++)
            Max=max(Max,a[d+100-j]+=p);
    }
        out<<Max;
    return 0;
}


#include<fstream>
using namespace std;
int main()
{
    ifstream in("input.txt");
    ofstream out("output.txt");
    int month[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
    int tests,MAX=0,hobo[600]={0};
    int m,d,p,t,d1;
    in>>tests;

    while(tests--)
    {

       in>>m>>d>>p>>t;
        m--;
        d1=100+month[m]+d;
for(int j = 1; j <= t; ++j) {
            hobo[d1- j] += p;
            if(hobo[d1- j] > MAX)
                MAX = hobo[d1-j];
        }

}

    out<<MAX;
    return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define mp make_pair
#define F first
#define S second

using namespace std;
typedef long long ll;

int day(int m, int d){
	int r=0;
	fr(i,1,m){
		if(i==2) r += 28;
		else if(i<=7){
			if(i&1) r+=31;
			else r+=30;
		}else{
			if(i&1) r+=30;
			else r+=31;
		}
	}
	return r + d;
}

pair<int,int> ls[10000];

int main(){
	int n;
	scanf("%d",&n);
	int cnt=0;
	rep(i,n){
		int m, d, l, k;
		scanf("%d %d %d %d",&m,&d,&k,&l);
		int a = day(m,d)-l, b = day(m,d)-1;
		ls[cnt++] = mp(a,k);
		ls[cnt++] = mp(b+1,-k);
	}
	sort(ls,ls+cnt);
	int q=0, r=0;
	rep(i,cnt){
		int d=ls[i].F;
		while(i<cnt && d==ls[i].F) q+=ls[i].S, i++;
		r = max(r,q);
		i--;
	}
	printf("%d\n",r);

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define fr(a,b,c) for(int a = b; a < c; a++)
#define rep(a,b) fr(a,0,b)
#define ln puts("")

typedef long long ll;

int n;

int getday(int d, int m){
	int res = 0;

	if(m > 1) res += 31;
	if(m > 2) res += 28;
	if(m > 3) res += 31;
	if(m > 4) res += 30;
	if(m > 5) res += 31;
	if(m > 6) res += 30;
	if(m > 7) res += 31;
	if(m > 8) res += 31;
	if(m > 9) res += 30;
	if(m > 10) res += 31;
	if(m > 11) res += 30;

	res += d;

	return d-1;
}

int tonum(int d, int m, int y){
	return y*365 + getday(d, m);
}

struct ol{
	int s, e, p, t;
} r[111];

bool read(){
	if(scanf("%d", &n) == EOF) return false;

	rep(i,n){
		int m, d, p, t;
		scanf("%d%d%d%d", &m, &d, &p, &t);

		r[i].e = tonum(d, m, 2013)-1;
		r[i].p = p;
		r[i].t = t;
		r[i].s = r[i].e - r[i].t + 1;
	}

	return true;
}

void process(){
	int cur = tonum(1,1,2012);
	int last = tonum(1,1,2014);

	int work = 0;
	int res = 0;
	while(cur < last){
		rep(i,n) {
			if(r[i].s == cur) work += r[i].p;
			if(r[i].e+1 == cur) work -= r[i].p;
		}

		res = max(work, res);
		cur++;
	}

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

}

int main(){
	while(read()) process();

	return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define mp make_pair
#define F first
#define S second

using namespace std;
typedef long long ll;

int day(int m, int d){
	int r=0;
	fr(i,1,m){
		if(i==2) r += 28;
		else if(i<=7){
			if(i&1) r+=31;
			else r+=30;
		}else{
			if(i&1) r+=30;
			else r+=31;
		}
	}
	return r + d;
}

pair<int,int> ls[10000];

int main(){
	int n;
	cin >> n;
	int cnt=0;
	rep(i,n){
		int m, d, l, k;
		cin >> m >> d >> k >> l;
		int a = day(m,d)-l, b = day(m,d)-1;
		ls[cnt++] = mp(a,k);
		ls[cnt++] = mp(b+1,-k);
	}
	sort(ls,ls+cnt);
	int q=0, r=0;
	rep(i,cnt){
		int d=ls[i].F;
		while(i<cnt && d==ls[i].F) q+=ls[i].S, i++;
		r = max(r,q);

		i--;
	}
	cout << r << endl;

	return 0;
}


#include <bits/stdc++.h>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define mp make_pair
#define F first
#define S second

using namespace std;
typedef long long ll;

int day(int m, int d){
	int r=0;
	fr(i,1,m){
		if(i==2) r += 28;
		else if(i<=7){
			if(i&1) r+=31;
			else r+=30;
		}else{
			if(i&1) r+=30;
			else r+=31;
		}
	}
	return r + d;
}

pair<int,int> ls[10000];

int main(){
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	int n;
	scanf("%d",&n);
	int cnt=0;
	rep(i,n){
		int m, d, l, k;
		scanf("%d %d %d %d",&m,&d,&k,&l);
		int a = day(m,d)-l, b = day(m,d)-1;
		ls[cnt++] = mp(a,k);
		ls[cnt++] = mp(b+1,-k);
	}
	sort(ls,ls+cnt);
	int q=0, r=0;
	rep(i,cnt){
		int d=ls[i].F;
		while(i<cnt && d==ls[i].F) q+=ls[i].S, i++;
		r = max(r,q);
		i--;
	}
	printf("%d\n",r);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr(i,a,b) for(int i=a;i<b;++i)
typedef long long ll;

struct ol{
	int in, fi, p;
	ol(){}
	ol(int I, int F, int P){in = I, fi = F, p = P; }
}o[100100];

int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int c(int a, int b){
	int dn = 0;
	fr(i,0,a-1){
		dn += m[i];
	}
	return dn + (b-1);
}

int n;

int main(){
	freopen("input.txt", "r", stdin);
	freopen("output.txt","w", stdout);
	scanf("%d",&n);
	fr(i,0,n){
		int m,d,p,t;
		scanf("%d %d %d %d",&m,&d,&p,&t);
		o[i] = ol(c(m,d)-t, c(m,d)-1, p);
	}
	int r = -1;
	fr(i,-200,665){
		int now = 0;
		fr(j,0,n){
			if(o[j].in <= i && o[j].fi >= i){
				now += o[j].p;
			}
		}
		r =  max(r, now);
	}
	printf("%d\n",r);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fr(i,a,b) for(int i=a;i<b;++i)
typedef long long ll;

struct ol{
	int in, fi, p;
	ol(){}
	ol(int I, int F, int P){in = I, fi = F, p = P; }
}o[100100];

int m[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int c(int a, int b){
	int dn = 0;
	fr(i,0,a-1){
		dn += m[i];
	}
	return dn + (b-1);
}

int n;

int main(){
	freopen("input.txt", "r", stdin);
	freopen("output.txt","w", stdout);
	scanf("%d",&n);
	fr(i,0,n){
		int m,d,p,t;
		scanf("%d %d %d %d",&m,&d,&p,&t);
		o[i] = ol(c(m,d)-t, c(m,d)-1, p);
	}
	int r = -1;
	fr(i,0,665){
		int now = 0;
		fr(j,0,n){
			if(o[j].in <= i && o[j].fi >= i){
				now += o[j].p;
			}
		}
		r =  max(r, now);
	}
	printf("%d\n",r);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define fr(a,b,c) for(int a = b; a < c; a++)
#define rep(a,b) fr(a,0,b)
#define ln puts("")

typedef long long ll;

int n;

int getday(int d, int m){
	int res = 0;

	if(m > 1) res += 31;
	if(m > 2) res += 28;
	if(m > 3) res += 31;
	if(m > 4) res += 30;
	if(m > 5) res += 31;
	if(m > 6) res += 30;
	if(m > 7) res += 31;
	if(m > 8) res += 31;
	if(m > 9) res += 30;
	if(m > 10) res += 31;
	if(m > 11) res += 30;

	res += d;

	return d-1;
}

int tonum(int d, int m, int y){
	return y*365 + getday(d, m);
}

struct ol{
	int s, e, p, t;
} r[111];

bool read(){
	if(scanf("%d", &n) == EOF) return false;

	rep(i,n){
		int m, d, p, t;
		scanf("%d%d%d%d", &m, &d, &p, &t);

		r[i].e = tonum(d, m, 2013)-1;
		r[i].p = p;
		r[i].t = t;
		r[i].s = r[i].e - r[i].t + 1;
	}

	return true;
}

void process(){
	int cur = tonum(1,1,2012);
	int last = tonum(1,1,2014);

	int work = 0;
	int res = 0;
	while(cur < last){
		rep(i,n) {
			if(r[i].s == cur) work += r[i].p;
			if(r[i].e+1 == cur) work -= r[i].p;
		}

		res = max(work, res);
		cur++;
	}

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

}

int main(){
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	while(read()) process();

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define fr(a,b,c) for(int a = b; a < c; a++)
#define rep(a,b) fr(a,0,b)
#define ln puts("")

typedef long long ll;

int n;

int getday(int d, int m){
	int res = 0;

	if(m > 1) res += 31;
	if(m > 2) res += 28;
	if(m > 3) res += 31;
	if(m > 4) res += 30;
	if(m > 5) res += 31;
	if(m > 6) res += 30;
	if(m > 7) res += 31;
	if(m > 8) res += 31;
	if(m > 9) res += 30;
	if(m > 10) res += 31;
	if(m > 11) res += 30;

	res += d;

	return res-1;
}

int tonum(int d, int m, int y){
	return y*365 + getday(d, m);
}

struct ol{
	int s, e, p, t;
} r[111];

bool read(){
	if(scanf("%d", &n) == EOF) return false;

	rep(i,n){
		int m, d, p, t;
		scanf("%d%d%d%d", &m, &d, &p, &t);

		r[i].e = tonum(d, m, 2013)-1;
		r[i].p = p;
		r[i].t = t;
		r[i].s = r[i].e - r[i].t + 1;
	}

	return true;
}

void process(){
	int cur = tonum(1,1,2012);
	int last = tonum(1,1,2014);

	//printf("%d %d\n", cur, last);

	int work = 0;
	int res = 0;
	while(cur < last){
		rep(i,n) {
			if(r[i].s == cur) work += r[i].p;
			if(r[i].e+1 == cur) work -= r[i].p;
		}

		res = max(work, res);
		cur++;
	}

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

}

int main(){
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	while(read()) process();

	return 0;
}


#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define sz size
#define re return
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 2000000000
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define MOD 1000000007

int n, res;
struct item {
	int m, d, p, t, k;
} a[111];

int q[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
int d[15];

bool cmp(item t, item y) {
	re t.k < y.k;
}
map < int, int > m;
int maxx = 0;

int main() {

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		d[i] = d[i - 1] + q[i];

	for(int i = 1; i <= n; i++) {
		cin >> a[i].m >> a[i].d >> a[i].p >> a[i].t;
		a[i].k = d[a[i].m - 1] + a[i].d - a[i].t;
	}

	sort(a + 1, a + n + 1, cmp);

//	for(int i = 1; i <= n; i++)
//		cout << a[i].k << " " << a[i].k + a[i].t << endl;

	res = 0;
	res += a[1].p;
	int cur = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = a[i].k; j <= a[i].k + a[i].t - 1; j++)
			m[j] += a[i].p;
		}
	for(int i = -900; i <= 900; i++)
		maxx = max(maxx, m[i]);
	cout << maxx;
	re 0;
}


#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define sz size
#define re return
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 2000000000
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define MOD 1000000007

int n, res;
struct item {
	int m, d, p, t, k;
} a[111];

int q[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
int d[15];

bool cmp(item t, item y) {
	re t.k < y.k;
}

int main() {

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		d[i] = d[i - 1] + q[i];

	for(int i = 1; i <= n; i++) {
		cin >> a[i].m >> a[i].d >> a[i].p >> a[i].t;
		a[i].k = d[a[i].m - 1] + a[i].d - a[i].t;
	}

	sort(a + 1, a + n + 1, cmp);

//	for(int i = 1; i <= n; i++)
//		cout << a[i].k << " " << a[i].k + a[i].t << endl;

	res = 0;
	res += a[1].p;
	int cur = 0;
	for(int i = 2; i <= n; i++) {
		if(a[i - 1].k + a[i - 1].t <= a[i].k) cur += a[i - 1].p;

		if(cur < a[i].p) res += a[i].p - cur, cur += (a[i].p - cur);
		cur -= a[i].p;
		}
	cout << res;
	re 0;
}


#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define sz size
#define re return
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 2000000000
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define MOD 1000000007

int n, res;
struct item {
	int m, d, p, t, k;
} a[111];

int q[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
int d[15];

bool cmp(item t, item y) {
	re t.k < y.k;
}

int main() {

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		d[i] = d[i - 1] + q[i];

	for(int i = 1; i <= n; i++) {
		cin >> a[i].m >> a[i].d >> a[i].p >> a[i].t;
		a[i].k = d[a[i].m - 1] + a[i].d - a[i].t;
	}

	sort(a + 1, a + n + 1, cmp);

	//for(int i = 1; i <= n; i++)
	//	cout << a[i].k << " " << a[i].k + a[i].t << endl;

	res = 0;
	res += a[1].p;

	for(int i = 2; i <= n; i++) {
		int cur = 0, ccur = 0;
		for(int j = 1; j < i; j++)
			if(a[j].k + a[j].t <= a[i].k) cur += a[j].p;
			else
			ccur = a[j].p;
		//ccur = cur - ccur;
	//	cout << cur << " " << ccur << endl;
		if(cur - ccur >= 0) cur -= ccur;
		else
		cur = 0;
		if(cur < a[i].p)  res += (a[i].p - cur);

	}
	cout << res;
	re 0;
}


#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define sz size
#define re return
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 2000000000
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define MOD 1000000007

int n, res;
struct item {
	int m, d, p, t, k;
} a[111];

int q[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
int d[15];

bool cmp(item t, item y) {
	re t.k < y.k;
}

int main() {

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		d[i] = d[i - 1] + q[i];

	for(int i = 1; i <= n; i++) {
		cin >> a[i].m >> a[i].d >> a[i].p >> a[i].t;
		a[i].k = d[a[i].m - 1] + a[i].d - a[i].t;
	}

	sort(a + 1, a + n + 1, cmp);

	//for(int i = 1; i <= n; i++)
	//	cout << a[i].k << " " << a[i].k + a[i].t << endl;

	res = 0;
	res += a[1].p;

	for(int i = 2; i <= n; i++) {
		int cur = 0, ccur = 0;
		for(int j = 1; j < i; j++)
			if(a[j].k + a[j].t <= a[i].k) cur += a[j].p;
			else
			ccur = a[j].p;
	//	cout << cur << " " << ccur << endl;
		if(res - ccur < a[i].p)  res += (a[i].p - (res - ccur));

	}
	cout << res;
	re 0;
}


#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define sz size
#define re return
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 2000000000
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define MOD 1000000007

int n, res;
struct item {
	int m, d, p, t, k;
} a[111];

int q[] = {0, 31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
int d[15];

bool cmp(item t, item y) {
	re t.k < y.k;
}
map < int, int > m;
int maxx = 0;

int main() {

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		d[i] = d[i - 1] + q[i];

	for(int i = 1; i <= n; i++) {
		cin >> a[i].m >> a[i].d >> a[i].p >> a[i].t;
		a[i].k = d[a[i].m - 1] + a[i].d - a[i].t;
	}

	sort(a + 1, a + n + 1, cmp);

//	for(int i = 1; i <= n; i++)
//		cout << a[i].k << " " << a[i].k + a[i].t << endl;

	res = 0;
	res += a[1].p;
	int cur = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = a[i].k; j <= a[i].k + a[i].t - 1; j++)
			m[j] += a[i].p;
		}
	for(int i = -900; i <= 900; i++)
		maxx = max(maxx, m[i]);
	cout << maxx;
	re 0;
}


#include <vector>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define sz size
#define re return
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 2000000000
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()
#define MOD 1000000007

int n, res;
struct item {
	int m, d, p, t, k;
} a[111];

int q[] = {31, 28, 31, 30,31, 30, 31, 31, 30, 31, 30, 31};
int d[15];

bool cmp(item t, item y) {
	re t.k < y.k;
}

int main() {

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		d[i] = d[i - 1] + q[i];

	for(int i = 1; i <= n; i++) {
		cin >> a[i].m >> a[i].d >> a[i].p >> a[i].t;
		a[i].k = d[a[i].m - 1] + a[i].d - a[i].t;
	}

	sort(a + 1, a + n + 1, cmp);

	//for(int i = 1; i <= n; i++)
	//	cout << a[i].k << " " << a[i].k + a[i].t << endl;

	res = 0;
	res += a[1].p;

	for(int i = 2; i <= n; i++) {
		int cur = 0;
		for(int j = 1; j < i; j++)
			if(a[j].k + a[j].t <= a[i].k) cur += a[j].p;

		if(cur < a[i].p)  res += (a[i].p - cur);

	}
	cout << res;
	re 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;
    vector<pair<PI,int> > v;
    FOR(i,0,n){
        int m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        int s=d-t;
        v.pb(mp(mp(d-1,s),p));

    }
    sort(v.begin(),v.end());
    int mx=0;
    FOR(i,0,n)
    {
        int sum=0;
        FORDE(j,i,0){
            if(v[j].first.first<v[i].first.second) break;
            sum+=v[j].second;
        }
        mx=max(mx,sum);
    }

    cout<<mx<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;
    vector<pair<PI,int> > v;
    FOR(i,0,n){
        int m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m-1) d+=ar[j];

        int s=d-t;
        v.pb(mp(mp(d-1,s),p));

    }
    sort(v.begin(),v.end());
    int mx=0;
    FOR(i,0,n)
    {
        int sum=0;
        FORDE(j,i,0){
            if(v[j].first.first<v[i].first.second) break;
            sum+=v[j].second;
        }
        mx=max(mx,sum);
    }

    cout<<mx<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int tab[100000];

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;

    FOR(i,0,n){
        ll m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        ll s=d-t;
        tab[s+10000]+=p;
        tab[d-1+10000]-=p;
    }
    int mx=0;
    int sm=0;
    FOR(i,0,100000){
        sm+=tab[i];
        mx=max(mx,sm);
    }
    cout<<mx<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;
    vector<pair<PI,int> > v;
    FOR(i,0,n){
        int m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        int s=d-t;
        v.pb(mp(mp(d-1,s),p));

    }
    sort(v.begin(),v.end());

    int gak=0;

    int j=0;
    FOR(i,0,n)
    {
        while(v[j].first.first<v[i].first.second) gak+=v[j].second,j++;

        int mn=min(gak,v[i].second);
        //v[i].second-=mn;
        gak-=mn;
    }
    while(j<n) gak+=v[j].second,j++;

    cout<<gak;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int tab[100000];

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;

    FOR(i,0,n){
        ll m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        ll s=d-t;
        tab[s+10000]+=p;
        tab[d+10000]-=p;
    }
    int mx=0;
    int sm=0;
    FOR(i,0,100000){
        sm+=tab[i];
        mx=max(mx,sm);
    }
    cout<<mx<<endl;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;
    vector<pair<PI,int> > v;
    FOR(i,0,n){
        int m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        int s=d-t;
        v.pb(mp(mp(d-1,s),p));

    }
    sort(v.begin(),v.end());

    int gak=0;

    int j=0;
    FOR(i,0,n)
    {
        while(v[j].first.first<v[i].first.second) gak+=v[j].second,j++;

        v[i].second-=min(gak,v[i].second);
    }
    while(j<n) gak+=v[j].second,j++;

    cout<<gak;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;
    vector<pair<PI,int> > v;
    FOR(i,0,n){
        int m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        int s=d-t;
        v.pb(mp(mp(d-1,s),p));

    }
    sort(v.begin(),v.end());

    int gak=0;

    int j=0;
    FOR(i,0,n)
    {
        while(v[j].first.first<v[i].first.second) gak+=v[j].second,j++;

        v[i].second-=min(gak,v[i].second);
    }
    while(j<n) gak+=v[j].second,j++;

    cout<<gak;

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<cmath>
#include<assert.h>

#define FO(i,s,e,p) for(int i=(s);i<(e);i+=p)
#define FOD(i,s,e,p) for(int i=(s);i>(e);i-=p)

#define FOR(i,s,e) FO(i,s,e,1)
#define FORE(i,s,e) FOR(i,s,e+1)
#define FORD(i,s,e) FOD(i,s,e,1)
#define FORDE(i,s,e) FORD(i,s,e-1)

#define ALL(i,s) for(__typeof(s.begin()) i=s.begin();i!=s.end();i++)

#define MEM(tab,fill) memset(tab,fill,sizeof(tab))

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<bitset>
#include<map>

using namespace std;
#define ll long long
#define inf ((1<<20)-1)
#define deb(a) NULL
//cout<<#a<<" "<<a<<endl
#define pb push_back
#define mp make_pair

#define llu unsigned long long
#define PI pair<ll,ll>

int ar[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int main()
{
    freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int n;cin>>n;
    vector<pair<PI,ll> > v;
    FOR(i,0,n){
        ll m,d,p,t;cin>>m>>d>>p>>t;
        FOR(j,0,m) d+=ar[j];

        ll s=d-t;
        v.pb(mp(mp(d-1,s),p));

    }
    sort(v.begin(),v.end());

    ll gak=0;

    int j=0;
    FOR(i,0,n)
    {
        while(v[j].first.first<v[i].first.second) gak+=v[j].second,j++;

        int mn=min(gak,v[i].second);
        //v[i].second-=mn;
        gak-=mn;
    }
    while(j<n) gak+=v[j].second,j++;

    cout<<gak;

    return 0;
}


#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
using namespace std;
long long n,  k, i, j, res, ans, m, p, t, d, d1, cur[4000];
long long ma[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
main(){
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
       cin>>n;
       for(i=0;i<n;i++){
                         cin>>m>>d>>p>>t;
                         d1=d;
                         for(j=1;j<m;j++){
                                          d1+=ma[j];
                                          }
                         for(j=d1+1000-t;j<d1+1000;j++){
                                                        cur[j]+=p;
                                                        res=max(cur[j],res);
                                                        }
                         }
       cout<<res;
       }


#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
using namespace std;
long long n,  k, i, j, res, ans, m, p, t, d, d1, cur[4000];
long long ma[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
main(){
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
       cin>>n;
       for(i=0;i<n;i++){
                         cin>>m>>d>>p>>t;
                         d1=d;
                         for(j=1;j<m;j++){
                                          d1+=ma[j];
                                          }
                         for(j=d1+1000-t;j<d1+1000;j++){
                                                        cur[j]+=p;
                                                        res=max(cur[j],ans);
                                                        }
                         }
       cout<<res;
       }


#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
using namespace std;
int n,  k, i, j, ans, m, p, t, d, d1, cnt[3000];
int ma[14]={0,31,28,31,30,31,30,31,31,30,31,30,31};
main(){
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
       cin>>n;
       for(i=0;i<n;i++){
                         cin>>m>>d>>p>>t;
                         d1=d;
                         for(j=1;j<n;j++){
                                          d1+=ma[j];
                                          }
                         for(j=d1+1000-t;j<d1+1000;j++){
                                                        cnt[j]+=p;
                                                        ans=max(cnt[j],ans);
                                                        }
                         }
       cout<<ans;
       }


#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
using namespace std;
long long n,  k, i, j, res, ans, m, p, t, d, d1, cur[4000];
long long ma[13]={0,31,28,31,30,31,30,31,30,31,30,31,30};
main(){
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
       cin>>n;
       for(i=0;i<n;i++){
                         cin>>m>>d>>p>>t;
                         d1=d;
                         for(j=1;j<n;j++){
                                          d1+=ma[j];
                                          }
                         for(j=d1+1000-t;j<d1+1000;j++){
                                                        cur[j]+=p;
                                                        res=max(cur[j],ans);
                                                        }
                         }
       cout<<res;
       }


#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
using namespace std;
long long n,  k, i, j, res, ans, m, p, t, d, d1, cur[4000];
long long ma[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
main(){
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
       cin>>n;
       for(i=0;i<n;i++){
                         cin>>m>>d>>p>>t;
                         d1=d;
                         for(j=1;j<n;j++){
                                          d1+=ma[j];
                                          }
                         for(j=d1+1000-t;j<d1+1000;j++){
                                                        cur[j]+=p;
                                                        res=max(cur[j],ans);
                                                        }
                         }
       cout<<res;
       }