#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

class Team
{
public:
    Team(string n){
        curScore = 0;
        curGoal = 0;
        curGLost = 0;
        name = n;
    }
    ~Team() {

    }
    bool operator<(const Team& other) const{
        if(curScore < other.curScore) {
            return true;
        } else if (curScore > other.curScore) {
            return false;
        } else {
            if (curGoal - curGLost < other.curGoal - other.curGLost) {
                return true;
            } else if (curGoal - curGLost > other.curGoal - other.curGLost) {
                return false;
            } else {
                return curGoal < other.curGoal;
            }
        }
    }
    string name;
    int curScore;
    int curGoal;
    int curGLost;
};

void scoreChange(string s, int goal, int lost, vector<Team>& v) {
    // cout << "Team name " << s << endl;
    for (int i = 0; i < v.size(); ++i)
    {
        if(!s.compare(v[i].name)) { // equal
            v[i].curGoal += goal;
            v[i].curGLost += lost;
            if(goal > lost) {
                v[i].curScore += 3;
            } else if (goal == lost) {
                v[i].curScore += 1;
            }
            // cout << v[i].name << " " << v[i].curScore << endl;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    vector<Team> v;
    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;
        Team t(s);
        v.push_back(t);
    }
    for (int i = 0; i < n*(n-1)/2; ++i)
    {
        string a;
        cin >> a;
        string team1;
        string team2;
        int score1;
        int score2;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == '-') {
                team1 = a.substr(0,j);
                team2 = a.substr(j+1, a.length());
                // cout << "team1 " << team1 << endl;
                // cout << "team2 " << team2 << endl;
                break;
            }
        }
        cin >> a;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == ':') {
                score1 = stoi(a.substr(0,j));
                score2 = stoi(a.substr(j+1, a.length()));
                break;
            }
        }
        scoreChange(team1, score1, score2, v);
        scoreChange(team2, score2, score1, v);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < n/2; i++) {
        cout << v[n-i-1].name << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

class Team
{
public:
    Team(string n){
        curScore = 0;
        curGoal = 0;
        curGLost = 0;
        name = n;
    }
    ~Team() {

    }
    bool operator<(const Team& other) const{
        if(curScore < other.curScore) {
            return true;
        } else if (curScore > other.curScore) {
            return false;
        } else {
            if (curGoal - curGLost < other.curGoal - other.curGLost) {
                return true;
            } else if (curGoal - curGLost > other.curGoal - other.curGLost) {
                return false;
            } else {
                return curGoal < other.curGoal;
            }
        }
    }
    string name;
    int curScore;
    int curGoal;
    int curGLost;
};

void scoreChange(string s, int goal, int lost, vector<Team>& v) {
    // cout << "Team name " << s << endl;
    for (int i = 0; i < v.size(); ++i)
    {
        if(!s.compare(v[i].name)) { // equal
            v[i].curGoal += goal;
            v[i].curGLost += lost;
            if(goal > lost) {
                v[i].curScore += 3;
            } else if (goal == lost) {
                v[i].curScore += 1;
            }
            // cout << v[i].name << " " << v[i].curScore << endl;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    vector<Team> v;
    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;
        Team t(s);
        v.push_back(t);
    }
    for (int i = 0; i < n*(n-1)/2; ++i)
    {
        string a;
        cin >> a;
        string team1;
        string team2;
        int score1;
        int score2;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == '-') {
                team1 = a.substr(0,j);
                team2 = a.substr(j+1, a.length());
                // cout << "team1 " << team1 << endl;
                // cout << "team2 " << team2 << endl;
                break;
            }
        }
        cin >> a;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == ':') {
                score1 = std::atoi(a.substr(0,j).c_str());
                score2 = std::atoi(a.substr(j+1, a.length()).c_str());
                break;
            }
        }
        scoreChange(team1, score1, score2, v);
        scoreChange(team2, score2, score1, v);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < n/2; i++) {
        cout << v[n-i-1].name << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

class Team
{
public:
    Team(string n){
        curScore = 0;
        curGoal = 0;
        curGLost = 0;
        name = n;
    }
    ~Team() {

    }
    bool operator<(const Team& other) const{
        if(curScore < other.curScore) {
            return true;
        } else if (curScore > other.curScore) {
            return false;
        } else {
            if (curGoal - curGLost < other.curGoal - other.curGLost) {
                return true;
            } else if (curGoal - curGLost > other.curGoal - other.curGLost) {
                return false;
            } else {
                return curGoal < other.curGoal;
            }
        }
    }
    string name;
    int curScore;
    int curGoal;
    int curGLost;
};

void scoreChange(string s, int goal, int lost, vector<Team>& v) {
    // cout << "Team name " << s << endl;
    for (int i = 0; i < v.size(); ++i)
    {
        if(!s.compare(v[i].name)) { // equal
            v[i].curGoal += goal;
            v[i].curGLost += lost;
            if(goal > lost) {
                v[i].curScore += 3;
            } else if (goal == lost) {
                v[i].curScore += 1;
            }
            // cout << v[i].name << " " << v[i].curScore << endl;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    vector<Team> v;
    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;
        Team t(s);
        v.push_back(t);
    }
    for (int i = 0; i < n*(n-1)/2; ++i)
    {
        string a;
        cin >> a;
        string team1;
        string team2;
        int score1;
        int score2;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == '-') {
                team1 = a.substr(0,j);
                team2 = a.substr(j+1, a.length());
                // cout << "team1 " << team1 << endl;
                // cout << "team2 " << team2 << endl;
                break;
            }
        }
        cin >> a;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == ':') {
                score1 = std::atoi(a.substr(0,j).c_str());
                score2 = std::atoi(a.substr(j+1, a.length()).c_str());
                break;
            }
        }
        scoreChange(team1, score1, score2, v);
        scoreChange(team2, score2, score1, v);
    }
    sort(v.begin(), v.end());
    string*  arr = new string[n/2];
    for(int i = 0; i < n/2; i++) {
        arr[i] = v[n-i-1].name;
    }
    sort(arr, arr+n/2);
    for (int i = 0; i < n/2; ++i)
    {
        cout << arr[i] << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

class Team
{
public:
    Team(string n){
        curScore = 0;
        curGoal = 0;
        curGLost = 0;
        name = n;
    }
    ~Team() {

    }
    bool operator<(const Team& other) const{
        if(curScore < other.curScore) {
            return true;
        } else if (curScore > other.curScore) {
            return false;
        } else {
            if (curGoal - curGLost < other.curGoal - other.curGLost) {
                return true;
            } else if (curGoal - curGLost > other.curGoal - other.curGLost) {
                return false;
            } else {
                return curGoal < other.curGoal;
            }
        }
    }
    string name;
    int curScore;
    int curGoal;
    int curGLost;
};

void scoreChange(string s, int goal, int lost, vector<Team>& v) {
    // cout << "Team name " << s << endl;
    for (int i = 0; i < v.size(); ++i)
    {
        if(!s.compare(v[i].name)) { // equal
            v[i].curGoal += goal;
            v[i].curGLost += lost;
            if(goal > lost) {
                v[i].curScore += 3;
            } else if (goal == lost) {
                v[i].curScore += 1;
            }
            // cout << v[i].name << " " << v[i].curScore << endl;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    vector<Team> v;
    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;
        Team t(s);
        v.push_back(t);
    }
    for (int i = 0; i < n*(n-1)/2; ++i)
    {
        string a;
        cin >> a;
        string team1;
        string team2;
        int score1;
        int score2;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == '-') {
                team1 = a.substr(0,j);
                team2 = a.substr(j+1, a.length());
                // cout << "team1 " << team1 << endl;
                // cout << "team2 " << team2 << endl;
                break;
            }
        }
        cin >> a;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == ':') {
                score1 = std::stoi(a.substr(0,j));
                score2 = std::stoi(a.substr(j+1, a.length()));
                break;
            }
        }
        scoreChange(team1, score1, score2, v);
        scoreChange(team2, score2, score1, v);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < n/2; i++) {
        cout << v[n-i-1].name << endl;
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

class Team
{
public:
    Team(string n){
        curScore = 0;
        curGoal = 0;
        curGLost = 0;
        name = n;
    }
    ~Team() {

    }
    bool operator<(const Team& other) const{
        if(curScore < other.curScore) {
            return true;
        } else if (curScore > other.curScore) {
            return false;
        } else {
            if (curGoal - curGLost < other.curGoal - other.curGLost) {
                return true;
            } else if (curGoal - curGLost > other.curGoal - other.curGLost) {
                return false;
            } else {
                return curGoal < other.curGoal;
            }
        }
    }
    string name;
    int curScore;
    int curGoal;
    int curGLost;
};

void scoreChange(string s, int goal, int lost, vector<Team>& v) {
    // cout << "Team name " << s << endl;
    for (int i = 0; i < v.size(); ++i)
    {
        if(!s.compare(v[i].name)) { // equal
            v[i].curGoal += goal;
            v[i].curGLost += lost;
            if(goal > lost) {
                v[i].curScore += 3;
            } else if (goal == lost) {
                v[i].curScore += 1;
            }
            // cout << v[i].name << " " << v[i].curScore << endl;
            return;
        }
    }
}

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    vector<Team> v;
    for (int i = 0; i < n; ++i)
    {
        string s;
        cin >> s;
        Team t(s);
        v.push_back(t);
    }
    for (int i = 0; i < n*(n-1)/2; ++i)
    {
        string a;
        cin >> a;
        string team1;
        string team2;
        int score1;
        int score2;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == '-') {
                team1 = a.substr(0,j);
                team2 = a.substr(j+1, a.length());
                // cout << "team1 " << team1 << endl;
                // cout << "team2 " << team2 << endl;
                break;
            }
        }
        cin >> a;
        for(int j = 0; j < a.length(); j++) {
            if(a[j] == ':') {
                score1 = stoi(a.substr(0,j));
                score2 = stoi(a.substr(j+1, a.length()));
                break;
            }
        }
        scoreChange(team1, score1, score2, v);
        scoreChange(team2, score2, score1, v);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < n/2; i++) {
        cout << v[n-i-1].name << endl;
    }
    return 0;
}


//Seidazimov Nurbol
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <list>
#include <functional>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <ctime>
#include <ctype.h>

using namespace std;
typedef long long ll;
const double eps = 1e-9;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz size()
#define sqr(x) ((x)*(x))
#define INF numeric_limits<int>::max()

int n, num1, num2, ix; string s[55], t, t1, t2;
map<string, int> fig; map<string, int> goal_1; map<string, int> goal;
struct item {
    int x, goal, raz; string s;
} res[55];
bool cmp (item a, item b) {
    return (a.x > b.x) || (a.x == b.x && a.raz > b.raz) ||
    (a.x == b.x && a.raz == b.raz && a.goal > b.goal) ||
    (a.x == b.x && a.raz == b.raz && a.goal == b.goal && a.s < b.s);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
            scanf ("%d\n", &n);
            for (int i=0; i<n; i++) {
                getline (cin, t);
                s[i] = t;
            }
            for (int i=0; i<n*(n-1)/2; i++) {
                cin >> t; t1.clear(); t2.clear();
                scanf ("%d:%d\n", &num1, &num2);
                for (ix=0; ix<t.sz; ix++) {
                    if (t[ix] == '-') break;
                    t1 += t[ix];
                } ix++;
                for (; ix<t.sz; ix++) t2 += t[ix];
                if (num1 > num2) fig[t1] = fig[t1] + 3;
                else if (num2 > num1) fig[t2] = fig[t2] + 3;
                else if (num1 == num2) fig[t1]++, fig[t2]++;
                goal[t1] = goal[t1] + num1 - num2, goal_1[t1]++;
                goal[t2] = goal[t2] + num2 - num1, goal_1[t2]++;
            }
            for (int i=0; i<n; i++) {
                res[i].x = fig[s[i]], res[i].s = s[i],
                res[i].raz = goal[s[i]], res[i].goal = goal_1[s[i]];
            }
            sort (res, res + n, cmp); n /= 2;
            for (int i=0; i<n; i++)
                s[i] = res[i].s;
            sort (s, s + n);
            for (int i=0; i<n-1; i++)
               puts (s[i].c_str());
            printf ("%s", s[n-1].c_str());
    return 0;
}


//Seidazimov Nurbol
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <list>
#include <functional>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <ctime>
#include <ctype.h>

using namespace std;
typedef long long ll;
const double eps = 1e-9;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz size()
#define sqr(x) ((x)*(x))
#define INF numeric_limits<int>::max()

int n, num1, num2, ix; string s[55], t, t1, t2;
map<string, int> fig; map<string, int> goal_1; map<string, int> goal;
struct item {
    int x, goal, raz; string s;
} res[55];
bool cmp (item a, item b) {
    return (a.x > b.x) || (a.x == b.x && a.raz > b.raz) ||
    (a.x == b.x && a.raz == b.raz && a.goal > b.goal) ||
    (a.x == b.x && a.raz == b.raz && a.goal == b.goal && a.s < b.s);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
            scanf ("%d\n", &n);
            for (int i=0; i<n; i++) {
                getline (cin, t);
                s[i] = t;
            }
            for (int i=0; i<n*(n-1)/2; i++) {
                cin >> t; t1.clear(); t2.clear();
                scanf ("%d:%d\n", &num1, &num2);
                for (ix=0; ix<t.sz; ix++) {
                    if (t[ix] == '-') break;
                    t1 += t[ix];
                } ix++;
                for (; ix<t.sz; ix++) t2 += t[ix];
                if (num1 > num2) fig[t1] = fig[t1] + 3;
                else if (num2 > num1) fig[t2] = fig[t2] + 3;
                else if (num1 == num2) fig[t1]++, fig[t2]++;
                goal[t1] += num1 - num2, goal_1[t1] += num1;
                goal[t2] += num2 - num1, goal_1[t2] += num2;
            }
            for (int i=0; i<n; i++) {
                res[i].x = fig[s[i]], res[i].s = s[i],
                res[i].raz = goal[s[i]], res[i].goal = goal_1[s[i]];
            }
            sort (res, res + n, cmp); n /= 2;
            for (int i=0; i<n; i++)
                s[i] = res[i].s;
            sort (s, s + n);
            for (int i=0; i<n-1; i++)
               puts (s[i].c_str());
            printf ("%s", s[n-1].c_str());
    return 0;
}


//Seidazimov Nurbol
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <list>
#include <functional>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <ctime>
#include <ctype.h>

using namespace std;
typedef long long ll;
const double eps = 1e-9;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz size()
#define sqr(x) ((x)*(x))
#define INF numeric_limits<int>::max()

int n, num1, num2, ix; string s[55], t, t1, t2;
map<string, int> fig;
struct item {
    int x; string s;
} res[55];
bool cmp (item a, item b) {
    return (a.x < b.x) || (a.x == b.x && a.s.sz < b.s.sz);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
            scanf ("%d\n", &n);
            for (int i=0; i<n; i++) {
                getline (cin, t);
                s[i] = t;
            }
            for (int i=0; i<n*(n-1)/2; i++) {
                cin >> t; t1.clear(); t2.clear();
                scanf ("%d:%d\n", &num1, &num2);
                for (ix=0; ix<t.sz; ix++) {
                    if (t[ix] == '-') break;
                    t1 += t[ix];
                } ix++;
                for (; ix<t.sz; ix++) t2 += t[ix];
                if (num1 > num2) fig[t] = fig[t] + 3;
                else if (num2 > num1) fig[t1] = fig[t1] + 3;
                else if (num1 == num2) fig[t1]++, fig[t]++;
            }
            for (int i=0; i<n; i++)
                res[i].x = fig[s[i]], res[i].s = s[i];
            sort (res, res + n, cmp); n /= 2;
            for (int i=0; i<n; i++)
                s[i] = res[i].s;
            sort (s, s + n);
            for (int i=0; i<n-1; i++)
               puts (s[i].c_str());
            printf ("%s", s[n-1].c_str());
    return 0;
}


//Seidazimov Nurbol
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <list>
#include <functional>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <ctime>
#include <ctype.h>

using namespace std;
typedef long long ll;
const double eps = 1e-9;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz size()
#define sqr(x) ((x)*(x))
#define INF numeric_limits<int>::max()

int n, num1, num2, ix; string s[55], t, t1, t2;
map<string, int> fig; map<string, int> goal_1; map<string, int> goal;
struct item {
    int x, goal, raz; string s;
} res[55];
bool cmp (item a, item b) {
    return (a.x > b.x) || (a.x == b.x && a.raz > b.raz) ||
    (a.x == b.x && a.raz == b.raz && a.goal > b.goal);
    // || (a.x == b.x && a.raz == b.raz && a.goal == b.goal && a.s < b.s);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
            scanf ("%d\n", &n);
            for (int i=0; i<n; i++) {
                getline (cin, t);
                s[i] = t;
            }
            for (int i=0; i<n*(n-1)/2; i++) {
                cin >> t; t1.clear(); t2.clear();
                scanf ("%d:%d\n", &num1, &num2);
                for (ix=0; ix<t.sz; ix++) {
                    if (t[ix] == '-') break;
                    t1 += t[ix];
                } ix++;
                for (; ix<t.sz; ix++) t2 += t[ix];
                if (num1 > num2) fig[t1] = fig[t1] + 3;
                else if (num2 > num1) fig[t2] = fig[t2] + 3;
                else if (num1 == num2) fig[t1]++, fig[t2]++;
                goal[t1] = goal[t1] + num1 - num2, goal_1[t1]++;
                goal[t2] = goal[t2] + num2 - num1, goal_1[t2]++;
            }
            for (int i=0; i<n; i++) {
                res[i].x = fig[s[i]], res[i].s = s[i],
                res[i].raz = goal[s[i]], res[i].goal = goal_1[s[i]];
            }
            sort (res, res + n, cmp); n /= 2;
            for (int i=0; i<n; i++)
                s[i] = res[i].s;
            sort (s, s + n);
            for (int i=0; i<n-1; i++)
               puts (s[i].c_str());
            printf ("%s", s[n-1].c_str());
    return 0;
}


//Seidazimov Nurbol
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <list>
#include <functional>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <ctime>
#include <ctype.h>

using namespace std;
typedef long long ll;
const double eps = 1e-9;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz size()
#define sqr(x) ((x)*(x))
#define INF numeric_limits<int>::max()

int n, num1, num2, ix; string s[55], t, t1, t2;
map<string, int> fig;
struct item {
    int x; string s;
} res[55];
bool cmp (item a, item b) {
    return (a.x < b.x);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
            scanf ("%d\n", &n);
            for (int i=0; i<n; i++) {
                getline (cin, t);
                s[i] = t;
            }
            for (int i=0; i<n*(n-1)/2; i++) {
                cin >> t; t1.clear(); t2.clear();
                scanf ("%d:%d\n", &num1, &num2);
                for (ix=0; ix<t.sz; ix++) {
                    if (t[ix] == '-') break;
                    t1 += t[ix];
                } ix++;
                for (; ix<t.sz; ix++) t2 += t[ix];
                if (num1 > num2) fig[t] = fig[t] + 3;
                else if (num2 > num1) fig[t1] = fig[t1] + 3;
                else if (num1 == num2) fig[t1]++, fig[t]++;
            }
            for (int i=0; i<n; i++)
                res[i].x = fig[s[i]], res[i].s = s[i];
            sort (res, res + n, cmp); n /= 2;
            for (int i=0; i<n; i++)
                s[i] = res[i].s;
            sort (s, s + n);
            for (int i=0; i<n-1; i++)
               puts (s[i].c_str());
            printf ("%s", s[n-1].c_str());
    return 0;
}


//Seidazimov Nurbol
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <list>
#include <functional>
#include <bitset>
#include <numeric>
#include <iomanip>
#include <ctime>
#include <ctype.h>

using namespace std;
typedef long long ll;
const double eps = 1e-9;

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define sz size()
#define sqr(x) ((x)*(x))
#define INF numeric_limits<int>::max()

int n, num1, num2, ix; string s[55], t, t1, t2;
map<string, int> fig;
struct item {
    int x; string s;
} res[55];
char small_symbol (char x) {
    if (x>='A' && x<='Z') return x + ' ';
    return x;
}
string small (string tt) {
    string cur;
    for (int i=0; i<tt.sz; i++)
        cur = cur + small_symbol(tt[i]);
    return cur;
}
bool cmp (item a, item b) {
    return (a.x < b.x) || (a.x == b.x && small(a.s) < small(b.s));
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
            scanf ("%d\n", &n);
            for (int i=0; i<n; i++) {
                getline (cin, t);
                s[i] = t;
            }
            for (int i=0; i<n*(n-1)/2; i++) {
                cin >> t; t1.clear(); t2.clear();
                scanf ("%d:%d\n", &num1, &num2);
                for (ix=0; ix<t.sz; ix++) {
                    if (t[ix] == '-') break;
                    t1 += t[ix];
                } ix++;
                for (; ix<t.sz; ix++) t2 += t[ix];
                if (num1 > num2) fig[t] = fig[t] + 3;
                else if (num2 > num1) fig[t1] = fig[t1] + 3;
                else if (num1 == num2) fig[t1]++, fig[t]++;
            }
            for (int i=0; i<n; i++)
                res[i].x = fig[s[i]], res[i].s = s[i];
            sort (res, res + n, cmp); n /= 2;
            for (int i=0; i<n; i++)
                s[i] = res[i].s;
            sort (s, s + n);
            for (int i=0; i<n-1; i++)
               puts (s[i].c_str());
            printf ("%s", s[n-1].c_str());
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define int long long int
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define rep(i,a,b) for(i=a;i<b;i++)
#define hell 1000000007
#define PI 3.14159265
#define mod 998244353
#define fs first
#define sc second
int n,m;
int vis[1005][1005], id[1005];
char a[1005][1005];
int sz ,ind;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dfs(int u, int v)
{
    if(u<0 || v<0 || u>=n || v>=m)return;
    if(vis[u][v])return;
    if(a[u][v]=='*')return;
    vis[u][v]=ind;
    sz++;
    for(int i=0;i<4;i++)
    {
        int x= u+dx[i];
        int y= v+dy[i];
        dfs(x,y);
    }
}
void solve()
{
    cin>>n>>m;
    int i,j;
    for(i=0;i<n;i++)
    {
        string s;
        cin>>s;
        for(j=0;j<m;j++)
            a[i][j]= s[j];
    }
    ind= 1;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.' && !vis[i][j])
            {
                sz=0;
                dfs(i,j);
                id[ind]=sz;
                ind++;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.')
                cout<<'.';
            else
            {
                int cmp=1;
                set<int>v;
                for(int k=0;k<4;k++)
                {
                    int x= i+dx[k];
                    int y= j+dy[k];
                    if(x>=0 && y>=0 && x<n && y<m)
                        v.insert(vis[x][y]);
                }
                for(auto it=v.begin();it!=v.end();it++)
                    {
                        cmp+= id[*it];
                    }
                cout<<cmp%10;
            }
        }
        cout<<"\n";
    }
}
signed main()
{
  fastio
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  int t;
  t=1;
  //cin>>t;
  while(t--)
  solve();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define int long long int
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define rep(i,a,b) for(i=a;i<b;i++)
#define hell 1000000007
#define PI 3.14159265
#define mod 998244353
#define fs first
#define sc second
int n,m;
int vis[1005][1005], id[1000005];
char a[1005][1005];
int sz ,ind;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dfs(int u, int v)
{
    vis[u][v]=ind;
    sz++;
    for(int i=0;i<4;i++)
    {
        int x= u+dx[i];
        int y= v+dy[i];
        if(x>=0 && y>=0 && x<n && y<m && vis[x][y]==0 && a[x][y]=='.')
            dfs(x,y);
    }
}
void solve()
{
    cin>>n>>m;
    int i,j;
    for(i=0;i<n;i++)
    {
        string s;
        cin>>s;
        for(j=0;j<m;j++)
            a[i][j]= s[j];
    }
    ind= 1;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.' && !vis[i][j])
            {
                sz=0;
                dfs(i,j);
                id[ind]=sz;
                ind++;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.')
                cout<<'.';
            else
            {
                int cmp=1;
                set<int>v;
                for(int k=0;k<4;k++)
                {
                    int x= i+dx[k];
                    int y= j+dy[k];
                    if(x>=0 && y>=0 && x<n && y<m && vis[x][y]!=0)
                        v.insert(vis[x][y]);
                }
                for(auto it=v.begin();it!=v.end();it++)
                    cmp+= id[*it];
                cout<<cmp;
            }
        }
        cout<<"\n";
    }
}
signed main()
{
  fastio
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  int t;
  t=1;
  //cin>>t;
  while(t--)
  solve();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define int long long int
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define rep(i,a,b) for(i=a;i<b;i++)
#define hell 1000000007
#define PI 3.14159265
#define mod 998244353
#define fs first
#define sc second
int n,m;
int vis[1005][1005], id[1000005];
char a[1005][1005];
int sz ,ind;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dfs(int u, int v)
{
    vis[u][v]=ind;
    sz++;
    for(int i=0;i<4;i++)
    {
        int x= u+dx[i];
        int y= v+dy[i];
        if(x>=0 && y>=0 && x<n && y<m && vis[x][y]==0 && a[x][y]=='.')
            dfs(x,y);
    }
}
void solve()
{
    cin>>n>>m;
    int i,j;
    for(i=0;i<n;i++)
    {
        string s;
        cin>>s;
        for(j=0;j<m;j++)
            a[i][j]= s[j];
    }
    ind= 1;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.' && !vis[i][j])
            {
                sz=0;
                dfs(i,j);
                id[ind]=sz;
                ind++;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.')
                cout<<'.';
            else
            {
                int cmp=1;
                set<int>v;
                for(int k=0;k<4;k++)
                {
                    int x= i+dx[k];
                    int y= j+dy[k];
                    if(x>=0 && y>=0 && x<n && y<m && vis[x][y]!=0)
                        v.insert(vis[x][y]);
                }
                for(auto it=v.begin();it!=v.end();it++)
                    cmp+= id[*it];
                cout<<cmp%10;
            }
        }
        cout<<"\n";
    }
}
signed main()
{
  fastio
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  int t;
  t=1;
  //cin>>t;
  while(t--)
  solve();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define int long long int
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define rep(i,a,b) for(i=a;i<b;i++)
#define hell 1000000007
#define PI 3.14159265
#define mod 998244353
#define fs first
#define sc second
int n,m;
int vis[1005][1005], id[1000005];
char a[1005][1005];
int sz ,ind;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void dfs(int u, int v)
{
    vis[u][v]=ind;
    sz++;
    for(int i=0;i<4;i++)
    {
        int x= u+dx[i];
        int y= v+dy[i];
        if(x>=0 && y>=0 && x<n && y<m && vis[x][y]==0 && a[x][y]=='.')
            dfs(x,y);
    }
}
void solve()
{
    cin>>n>>m;
    int i,j;
    for(i=0;i<n;i++)
    {
        string s;
        cin>>s;
        for(j=0;j<m;j++)
            a[i][j]= s[j];
    }
    ind= 1;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.' && !vis[i][j])
            {
                sz=0;
                dfs(i,j);
                id[ind]=sz;
                ind++;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(a[i][j]=='.')
                cout<<'.';
            else
            {
                int cmp=1;
                set<int>v;
                for(int k=0;k<4;k++)
                {
                    int x= i+dx[k];
                    int y= j+dy[k];
                    if(x>=0 && y>=0 && x<n && y<m && a[x][y]=='.')
                        v.insert(vis[x][y]);
                }
                for(auto it=v.begin();it!=v.end();it++)
                    {
                        cmp+= id[*it];
                    }
                cout<<cmp%10;
            }
        }
        cout<<"\n";
    }
}
signed main()
{
  fastio
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  int t;
  t=1;
  //cin>>t;
  while(t--)
  solve();
  return 0;
}


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

const int maxN = 1e3 + 10;
int n, m;

class DJS{
  public:
    int getRt(int i){
      if(roots[i] != i){
        roots[i] = getRt(roots[i]);
        return roots[i];
      }
      return i;
    }
    int getSetSz(int i){
      return setSz[getRt(i)];
    }
    void merge(int i , int j){
      int iRt = getRt(i);
      int jRt = getRt(j);
      if( iRt != jRt ){
        if( getSetSz(iRt) > getSetSz(jRt) ){
          roots[jRt] = iRt;
          setSz[iRt] = setSz[iRt] + setSz[jRt];
        }
        else{
          roots[iRt] = jRt;
          setSz[jRt] = setSz[jRt] + setSz[iRt];
        }
      }
    }
    DJS(int _sz): sz(_sz){
      if( sz > maxSz )
        printf("Error size too large");
      for(int i = 0; i < sz; i++){
        roots[i] = i;
        setSz[i] = 1;
      }
    }
  private:
    int maxSz = maxN * maxN;
    int roots[maxN * maxN];
    int setSz[maxN * maxN];
    int sz;
};

char M[maxN * maxN];
bool used[maxN * maxN];
const int mod = 1000;
DJS djs(maxN * maxN);

int rIdx(int i, int j){
  return i * mod + j;
}

void parce(){
  scanf("%d %d", &n, &m);
  char line[maxN];
  for(int i = 0; i < n; i++){
    scanf("%s", line);
    for(int j = 0; j < m; j++){
      M[rIdx(i, j)] = line[j];
    }
  }
}

void init(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      used[rIdx(i,j)] = false;
    }
  }
}

typedef pair<int, int> ii;
#define MKPII(A, B) make_pair<int, int>((A),(B))
vector<ii> dir{MKPII(1,0), MKPII(-1,0), MKPII(0,1), MKPII(0,-1)};

bool validRng(int nxtI, int nxtJ){
  if( nxtI >= 0 && nxtI < n && nxtJ >= 0 && nxtJ < m)
    return true;
  else
    return false;
}

//DisJoint Set

void dfs(int i, int j){
  used[rIdx(i, j)] = true;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      if(used[rIdx(nxtI, nxtJ)] == true)
        continue;
      if( M[rIdx(nxtI, nxtJ)] == '.' ){
        djs.merge(rIdx(i, j), rIdx(nxtI,nxtJ));
        dfs(nxtI, nxtJ);
      }
    }
  }
}

int cntCmpSz(int i , int j){
  set<int> roots;
  int cnt = 0;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      int root = djs.getRt( rIdx(nxtI, nxtJ) );
      int rootSz = djs.getSetSz(root);
      if( roots.find(root) != roots.end() ){
        continue;
      }
      else{
        cnt += rootSz;
        roots.insert(root);
      }
    }
  }
  return cnt;
}

void solve(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if( used[rIdx(i,j)] == false )
        dfs(i, j);
    }
  }

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

int main(){
  parce();
  init();
  solve();
}


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

const int maxN = 1e3 + 10;
int n, m;

class DJS{
  public:
    int getRt(int i){
      if(roots[i] != i){
        roots[i] = getRt(roots[i]);
        return roots[i];
      }
      return i;
    }
    int getSetSz(int i){
      return setSz[getRt(i)];
    }
    void merge(int i , int j){
      int iRt = getRt(i);
      int jRt = getRt(j);
      if( iRt != jRt ){
        if( getSetSz(iRt) > getSetSz(jRt) ){
          roots[jRt] = iRt;
          setSz[iRt] = setSz[iRt] + setSz[jRt];
        }
        else{
          roots[iRt] = jRt;
          setSz[jRt] = setSz[jRt] + setSz[iRt];
        }
      }
    }
    DJS(int _sz): sz(_sz){
      if( sz > maxSz )
        printf("Error size too large");
      for(int i = 0; i < sz; i++){
        roots[i] = i;
        setSz[i] = 1;
      }
    }
  private:
    int maxSz = maxN * maxN;
    int roots[maxN * maxN];
    int setSz[maxN * maxN];
    int sz;
};

char M[maxN * maxN];
const int mod = 1000;
DJS djs(maxN * maxN);

int rIdx(int i, int j){
  return i * mod + j;
}

void parce(){
  scanf("%d %d", &n, &m);
  char line[maxN];
  for(int i = 0; i < n; i++){
    scanf("%s", line);
    for(int j = 0; j < m; j++){
      M[rIdx(i, j)] = line[j];
    }
  }
}

typedef pair<int, int> ii;
#define MKPII(A, B) make_pair<int, int>((A),(B))
vector<ii> dir{MKPII(1,0), MKPII(-1,0), MKPII(0,1), MKPII(0,-1)};

bool validRng(int nxtI, int nxtJ){
  if( nxtI >= 0 && nxtI < n && nxtJ >= 0 && nxtJ < m)
    return true;
  else
    return false;
}

int cntCmpSz(int i , int j){
  set<int> roots;
  int cnt = 0;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      if( M[rIdx(nxtI, nxtJ)] == '*')
        continue;
      int root = djs.getRt( rIdx(nxtI, nxtJ) );
      int rootSz = djs.getSetSz(root);
      if( roots.find(root) != roots.end() ){
        continue;
      }
      else{
        cnt += rootSz;
        roots.insert(root);
      }
    }
  }
  return cnt + 1;
}

void solve(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if(M[rIdx(i,j)] == '.'){
        for( auto D : dir){
          int aI = i + D.first;
          int aJ = j + D.second;
          if( validRng(aI, aJ) ){
            if( M[rIdx(aI, aJ)] == '.')
              djs.merge( rIdx(i,j), rIdx(aI, aJ) );
          }
        }
      }
    }
  }

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

int main(){
  parce();
  solve();
}


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

const int maxN = 1e3 + 10;
int n, m;

class DJS{
  public:
    int getRt(int i){
      if(roots[i] != i){
        roots[i] = getRt(roots[i]);
        return roots[i];
      }
      return i;
    }
    int getSetSz(int i){
      return setSz[getRt(i)];
    }
    void merge(int i , int j){
      int iRt = getRt(i);
      int jRt = getRt(j);
      if( iRt != jRt ){
        if( getSetSz(iRt) > getSetSz(jRt) ){
          roots[jRt] = iRt;
          setSz[iRt] = setSz[iRt] + setSz[jRt];
        }
        else{
          roots[iRt] = jRt;
          setSz[jRt] = setSz[jRt] + setSz[iRt];
        }
      }
    }
    DJS(int _sz): sz(_sz){
      if( sz > maxSz )
        printf("Error size too large");
      for(int i = 0; i < sz; i++){
        roots[i] = i;
        setSz[i] = 1;
      }
    }
  private:
    int maxSz = maxN * maxN;
    int roots[maxN * maxN];
    int setSz[maxN * maxN];
    int sz;
};

char M[maxN * maxN];
const int mod = 1000;
DJS djs(maxN * maxN);

inline int rIdx(int i, int j){
  return i * mod + j;
}

void parce(){
  scanf("%d %d", &n, &m);
  char line[maxN];
  for(int i = 0; i < n; i++){
    scanf("%s", line);
    for(int j = 0; j < m; j++){
      M[rIdx(i, j)] = line[j];
    }
  }
}

typedef pair<int, int> ii;
#define MKPII(A, B) make_pair<int, int>((A),(B))
vector<ii> dir{MKPII(1,0), MKPII(-1,0), MKPII(0,1), MKPII(0,-1)};

bool validRng(int nxtI, int nxtJ){
  if( nxtI >= 0 && nxtI < n && nxtJ >= 0 && nxtJ < m)
    return true;
  else
    return false;
}

int cntCmpSz(int i , int j){
  set<int> roots;
  int cnt = 0;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      if( M[rIdx(nxtI, nxtJ)] == '*')
        continue;
      int root = djs.getRt( rIdx(nxtI, nxtJ) );
      int rootSz = djs.getSetSz(root);
      if( roots.find(root) != roots.end() ){
        continue;
      }
      else{
        cnt += rootSz;
        roots.insert(root);
      }
    }
  }
  return cnt + 1;
}

void solve(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if(M[rIdx(i,j)] == '.'){
        for( auto D : dir){
          int aI = i + D.first;
          int aJ = j + D.second;
          if( validRng(aI, aJ) ){
            if( M[rIdx(aI, aJ)] == '.')
              djs.merge( rIdx(i,j), rIdx(aI, aJ) );
          }
        }
      }
    }
  }

  char ans[maxN][maxN];
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if( M[rIdx(i, j)] == '.')
        ans[i][j] = '.';
      else
        ans[i][j] = '0' + cntCmpSz(i, j) % 10;
    }
  }
  for(int i = 0; i < n; i++){
    puts(ans[i]);
  }
}

int main(){
  parce();
  solve();
}


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

const int maxN = 1e3 + 10;
int n, m;

class DJS{
  public:
    int getRt(int i){
      if(roots[i] != i){
        roots[i] = getRt(roots[i]);
        return roots[i];
      }
      return i;
    }
    int getSetSz(int i){
      return setSz[getRt(i)];
    }
    void merge(int i , int j){
      int iRt = getRt(i);
      int jRt = getRt(j);
      if( iRt != jRt ){
        if( getSetSz(iRt) > getSetSz(jRt) ){
          roots[jRt] = iRt;
          setSz[iRt] = setSz[iRt] + setSz[jRt];
        }
        else{
          roots[iRt] = jRt;
          setSz[jRt] = setSz[jRt] + setSz[iRt];
        }
      }
    }
    DJS(int _sz): sz(_sz){
      if( sz > maxSz )
        printf("Error size too large");
      for(int i = 0; i < sz; i++){
        roots[i] = i;
        setSz[i] = 1;
      }
    }
  private:
    int maxSz = maxN * maxN;
    int roots[maxN * maxN];
    int setSz[maxN * maxN];
    int sz;
};

char M[maxN * maxN];
const int mod = 1000;
DJS djs(maxN * maxN);

int rIdx(int i, int j){
  return i * mod + j;
}

void parce(){
  scanf("%d %d", &n, &m);
  char line[maxN];
  for(int i = 0; i < n; i++){
    scanf("%s", line);
    for(int j = 0; j < m; j++){
      M[rIdx(i, j)] = line[j];
    }
  }
}

typedef pair<int, int> ii;
#define MKPII(A, B) make_pair<int, int>((A),(B))
vector<ii> dir{MKPII(1,0), MKPII(-1,0), MKPII(0,1), MKPII(0,-1)};

bool validRng(int nxtI, int nxtJ){
  if( nxtI >= 0 && nxtI < n && nxtJ >= 0 && nxtJ < m)
    return true;
  else
    return false;
}

int cntCmpSz(int i , int j){
  set<int> roots;
  int cnt = 0;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      if( M[rIdx(nxtI, nxtJ)] == '*')
        continue;
      int root = djs.getRt( rIdx(nxtI, nxtJ) );
      int rootSz = djs.getSetSz(root);
      if( roots.find(root) != roots.end() ){
        continue;
      }
      else{
        cnt += rootSz;
        roots.insert(root);
      }
    }
  }
  return cnt + 1;
}

void solve(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if(M[rIdx(i,j)] == '.'){
        for( auto D : dir){
          int aI = i + D.first;
          int aJ = j + D.second;
          if( validRng(aI, aJ) ){
            if( M[rIdx(aI, aJ)] == '.')
              djs.merge( rIdx(i,j), rIdx(aI, aJ) );
          }
        }
      }
    }
  }

  char ans[maxN][maxN];
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if( M[rIdx(i, j)] == '.')
        ans[i][j] = '.';
      else
        ans[i][j] = '0' + cntCmpSz(i, j) % 10;
    }
  }
  for(int i = 0; i < n; i++){
    puts(ans[i]);
  }
}

int main(){
  parce();
  solve();
}


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

const int maxN = 1e3 + 10;
int n, m;

class DJS{
  const int maxSz = maxN * maxN;
  int roots[maxSz];
  int setSz[maxSz];
  int sz;
  DJS(int _sz): sz(_sz){
    if( sz > maxSz )
      printf("Error size too large");
    sz = _sz;
    for(int i = 0; i < sz; i++){
      roots[i] = i;
      setSz[i] = 1;
    }
  }
  int getRt(int i){
    if(roots[i] != i){
      roots[i] = getRt(roots[i]);
      return roots[i];
    }
    return i;
  }
  int getSetSz(int i){
    return setSz[getRt(i)];
  }
  void merge(int i , int j){
    int iRt = getRt(i);
    int jRt = getRt(j);
    if( iRt != jRt ){
      if( getSetSz(iRt) > getSetSz(jRt) ){
        roots[jRt] = iRt;
        setSz[iRt] = setSz[iRt] + setSz[jRt];
      }
      else{
        roots[iRt] = jRt;
        setSz[jRt] = setSz[jRt] + setSz[iRt];
      }
    }
  }
}

char m[maxN * maxN];
bool used[maxN * maxN];
const int mod = 1000;
DJS djs(maxN * maxN);

int rIdx(int i, int j){
  return i * mod + j;
}

void parce(){
  scanf("%d %d", &n, &m);
  char line[maxN];
  for(int i = 0; i < n; i++){
    scanf("%s", line);
    for(int j = 0; j < m; j++){
      m[rIdx(i, j)] = line[j];
    }
  }
}

void init(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      used[rIdx(i,j)] = false;
    }
  }
}

typedef pair<int, int> ii;
vector<ii> dir{(1,0),(-1,0),(0,1),(0,-1)};

bool validRng(nxtI, nxtJ){
  if( nxtI >= 0 && nxtI < n && nxtJ >= 0 && nxtJ < m)
    return true;
  else
    return false;
}

//DisJoint Set

void dfs(int i, int j){
  used[rIdx(i, j)] = true;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      if(used[rIdx(nxtI, nxtJ)] == true)
        continue;
      if( m[rIdx(nxtI, nxtJ)] == '.' ){
        djs.merge(rIdx(i, j), rIdx(nxtI,nxtJ));
        dfs(nxtI, nxtJ);
      }
    }
  }
}

int cntCmpSz(int i , int j){
  set<int> roots;
  int cnt = 0;
  for(auto D : dir){
    int nxtI = i + D.first;
    int nxtJ = j + D.second;
    if( validRng(nxtI, nxtJ) ){
      int root = djs.getRt( rIdx(nxtI, nxtJ) );
      int rootSz = djs.getSetSz(root);
      if( roots.find(root) != roots.end() ){
        continue;
      }
      else{
        cnt += rootSz;
        roots.insert(root);
      }
    }
  }
  return cnt;
}

void solve(){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < m; j++){
      if( used[rIdx(i,j)] == False )
        dfs(i, j);
    }
  }

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

int main(){
  parce();
  init();
  solve();
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int col;
int cmp[1111][1111];
char c[1111][1111];
int num[11111111];
int n,m;
bool in(int x,int y)
{
	return x>=0&&y>=0&&x<n&&y<m;
}
bool used[1111][1111];
void bfs(int x,int y,int co)
{
	queue<pii>q;
	q.push(m_p(x,y));
	while(!q.empty())
	{
		pii p=q.front();
		q.pop();
		cmp[p.X][p.Y]=co;
		num[co]++;
		used[p.X][p.Y]=1;
		rep(i,4)
		{
			int nx=p.X+dx[i];
			int ny=p.Y+dy[i];
			if(in(nx,ny)&&!used[nx][ny]&&c[nx][ny]=='.')
			q.push(m_p(nx,ny));
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	//think twice,code once
	cin>>n>>m;
	rep(i,n)
		rep(j,m)
			cin>>c[i][j];
	rep(i,n)
		rep(j,m)
		{
			if(!used[i][j]&&c[i][j]=='.')
			{
				col++;
				bfs(i,j,col);
			}
		 }
//	rep(i,n)
//	{
//		rep(j,m)
//			cout<<cmp[i][j]<<" ";
//		cout<<endl;}
	rep(i,n)
	{
		rep(j,m)
		{
			if(c[i][j]=='*')
			{
				set<int>s;
				rep(k,4)
				{
					if(in(i+dx[k],j+dy[k])&&c[i+dx[k]][j+dy[k]]=='.')
					{
						s.insert(cmp[i+dx[k]][j+dy[k]]);
					}
				}
				int res=0;
				set<int>::iterator it=s.begin();
				while(it!=s.end())
				{
                    res+=num[*it];
                    it++;
                }
                res++;
				res%=10;
				cout<<res;
			}
			else cout<<".";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int col;
int cmp[1111][1111];
char c[1111][1111];
int num[11111111];
int n,m;
bool in(int x,int y)
{
	return x>=0&&y>=0&&x<n&&y<m;
}
bool used[1111][1111];
void bfs(int x,int y,int co)
{
	queue<pii>q;
	num[co]++;
	used[x][y]=1;
	q.push(m_p(x,y));
	while(!q.empty())
	{
		pii p=q.front();
		q.pop();
		cmp[p.X][p.Y]=co;
		rep(i,4)
		{
			int nx=p.X+dx[i];
			int ny=p.Y+dy[i];
			if(in(nx,ny)&&!used[nx][ny]&&c[nx][ny]=='.')
			{
			num[co]++;
			used[nx][ny]=1;
			q.push(m_p(nx,ny));
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	//think twice,code once
	cin>>n>>m;
	rep(i,n)
		rep(j,m)
			cin>>c[i][j];
	rep(i,n)
		rep(j,m)
		{
			if(!used[i][j]&&c[i][j]=='.')
			{
				col++;
				bfs(i,j,col);
			}
		 }
	//rep(i,col)cout<<num[i]<<endl;
	//rep(i,n)
	//{
	//	rep(j,m)
	//		cout<<cmp[i][j]<<" ";
	//	cout<<endl;}
	rep(i,n)
	{
		rep(j,m)
		{
			if(c[i][j]=='*')
			{
				set<int>s;
				rep(k,4)
				{
					if(in(i+dx[k],j+dy[k])&&c[i+dx[k]][j+dy[k]]=='.')
					{
						s.insert(cmp[i+dx[k]][j+dy[k]]);
					}
				}
				int res=0;
				set<int>::iterator it=s.begin();
				while(it!=s.end())
				{
                    res+=num[*it];
                    it++;
                }
                res++;
				res%=10;
				cout<<res;
			}
			else cout<<".";
		}
		cout<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <utility>
using namespace std;

const int maxn = 1e3+10;
char s[maxn][maxn];
pair<int, int> fa[maxn][maxn];
int vis[maxn][maxn];
int num[maxn][maxn];
int vis2[maxn][maxn];
int n, m;
int cnt;

int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

void dfs(int nx, int ny, int fx, int fy) {
    vis[nx][ny] = 1;
    fa[nx][ny] = {fx, fy};
    cnt++;

    for(int i = 0; i < 4; i++) {
        int nnx = nx + dx[i];
        int nny = ny + dy[i];
        if(nnx >= 0 && nnx < n && nny >= 0 && nny < m && !vis[nnx][nny] && s[nnx][nny] != '*')
            dfs(nnx, nny, fx, fy);
    }

}

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

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(s[i][j] != '*' && !vis[i][j]) {
                cnt = 0;
                dfs(i, j, i, j);
                num[i][j] = cnt;
            }
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(s[i][j] == '.') {
                printf(".");
                continue;
            }
            for(int k = 0; k < 4; k++) {
                int x = i + dx[k];
                int y = j + dy[k];
                if(x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '*') {
                    int fx = fa[x][y].first;
                    int fy = fa[x][y].second;
                    vis2[fx][fy]=0;
                }
            }
            cnt = 0;
            for(int k = 0; k < 4; k++) {
                int x = i + dx[k];
                int y = j + dy[k];
                if(x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '*') {
                    int fx = fa[x][y].first;
                    int fy = fa[x][y].second;
                    if(!vis2[fx][fy]) {
                        vis2[fx][fy] = 1;
                        cnt += num[fx][fy];
                    }
                }
            }
            printf("%d", (cnt+1)%10);
        }
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int col;
int cmp[1111][1111];
char c[1111][1111];
int num[1111111];
int n,m;
bool in(int x,int y)
{
	return x>=0&&y>=0&&x<n&&y<m;
}
bool used[1111][1111];
void bfs(int x,int y,int co)
{
	queue<pii>q;
	used[x][y]=1;
	q.push(m_p(x,y));
	while(!q.empty())
	{
		pii p=q.front();
		q.pop();
		cmp[p.X][p.Y]=co;
		num[co]++;
		rep(i,4)
		{
			int nx=p.X+dx[i];
			int ny=p.Y+dy[i];
			if(in(nx,ny)&&!used[nx][ny]&&c[nx][ny]=='.')
			{
			used[nx][ny]=1;
			q.push(m_p(nx,ny));
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	//think twice,code once
	cin>>n>>m;
	rep(i,n)
		rep(j,m)
			cin>>c[i][j];
	rep(i,n)
		rep(j,m)
		{
			if(!used[i][j]&&c[i][j]=='.')
			{
				col++;
				bfs(i,j,col);
			}
		 }
	//rep(i,col)cout<<num[i]<<endl;
	//rep(i,n)
	//{
	//	rep(j,m)
	//		cout<<cmp[i][j]<<" ";
	//	cout<<endl;}
	rep(i,n)
	{
		rep(j,m)
		{
			if(c[i][j]=='*')
			{
				set<int>s;
				rep(k,4)
				{
					if(in(i+dx[k],j+dy[k])&&c[i+dx[k]][j+dy[k]]=='.')
					{
						s.insert(cmp[i+dx[k]][j+dy[k]]);
					}
				}
				int res=0;
				set<int>::iterator it=s.begin();
				while(it!=s.end())
				{
                    res+=num[*it];
                    it++;
                }
                res++;
				res%=10;
				cout<<res;
			}
			else cout<<".";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int col;
int cmp[1111][1111];
char c[1111][1111];
int num[11111111];
int n,m;
bool in(int x,int y)
{
	return x>=0&&y>=0&&x<n&&y<m;
}
bool used[1111][1111];
void dfs(int x,int y,int co)
{
	cmp[x][y]=co;
	num[co]++;
	used[x][y]=1;
	rep(i,4)
	{
		if(in(x+dx[i],y+dy[i])&&!used[x+dx[i]][y+dy[i]]&&c[x+dx[i]][y+dy[i]]=='.')
			dfs(x+dx[i],y+dy[i],co);
	}
}
int main()
{
	ios::sync_with_stdio(0);
	//think twice,code once
	cin>>n>>m;
	rep(i,n)
		rep(j,m)
			cin>>c[i][j];
	rep(i,n)
		rep(j,m)
		{
			if(!used[i][j]&&c[i][j]=='.')
			{
				col++;
				dfs(i,j,col);
			}
		 }
//	rep(i,n)
//	{
//		rep(j,m)
//			cout<<cmp[i][j]<<" ";
//		cout<<endl;}
	rep(i,n)
	{
		rep(j,m)
		{
			if(c[i][j]=='*')
			{
				set<int>s;
				rep(k,4)
				{
					if(in(i+dx[k],j+dy[k])&&c[i+dx[k]][j+dy[k]]=='.')
					{
						s.insert(cmp[i+dx[k]][j+dy[k]]);
					}
				}
				int res=0;
				set<int>::iterator it=s.begin();
				while(it!=s.end())
				{
                    res+=num[*it];
                    it++;
                }
                res++;
				res%=10;
				cout<<res;
			}
			else cout<<".";
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
using namespace std;
int n,m,cnt=1;
char ax[1005][1005];
int vis[1005][1005];
int record[1005][1005];
int vt[1005][1005];
int d1[]={0,0,-1,1};
int d2[]={1,-1,0,0};
struct node
{
    int x,y,head;
};
void bfs(int x,int y)
{
   queue<node> ac;
   vector<node> ab;
   node now,Next;
   now.x=x;
   now.y=y;
   now.head=1;
   int t=1;
   ac.push(now);
   ab.push_back(now);
   vis[x][y]=cnt;
   while(!ac.empty())
   {
       now=ac.front();
       ac.pop();
       for(int i=0;i<4;i++)
       {
           int tx=now.x+d1[i];
           int ty=now.y+d2[i];
           if(vis[tx][ty]==cnt||tx<0||ty<0||tx>=n||ty>=m||ax[tx][ty]=='*')
               continue;
             vis[tx][ty]=cnt;
               t++;
               Next.head=t;
               Next.x=tx;
               Next.y=ty;
               ac.push(Next);
               ab.push_back(Next);
       }
   }
   for(int i=0;i<ab.size();i++)
   {
       node now=ab[i];
       record[ab[i].x][ab[i].y]=t;
       vt[ab[i].x][ab[i].y]=cnt;
   }
}
map<int,bool> aa;
int main()
{
    memset(vis,0,sizeof(vis));
    memset(record,0,sizeof(record));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
      scanf("%s",ax[i]);
    for(int i=0;i<n;i++)
       for(int j=0;j<m;j++)
         if(ax[i][j]=='.'&&!record[i][j])
           {
             bfs(i,j);
             cnt++;
           }
     for(int i=0;i<n;i++)
     {
         for(int j=0;j<m;j++)
         {
             if(ax[i][j]=='.')
                printf(".");
             else
             {
                 int ans=0;
                 for(int t=0;t<4;t++)
                 {
                    int tx=i+d1[t];
                    int ty=j+d2[t];
                   if(tx<0||ty<0||tx>=n||ty>=m||ax[tx][ty]=='*')
                       continue;
                     if(!aa[vt[tx][ty]])
                     {
                         ans+=record[tx][ty];
                         aa[vt[tx][ty]]=1;
                     }
                 }
                 printf("%d",(ans+1)%10);
                 aa.clear();
             }
         }
         printf("\n");
     }

    return 0;
}


#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<map>
using namespace std;
int n,m,cnt=1;
char ax[1005][1005];
int vis[1005][1005];
int record[1005][1005];
int vt[1005][1005];
int d1[]={0,0,-1,1};
int d2[]={1,-1,0,0};
struct node
{
    int x,y,head;
};
void bfs(int x,int y)
{
   queue<node> ac;
   vector<node> ab;
   node now,Next;
   now.x=x;
   now.y=y;
   now.head=1;
   int t=1;
   ac.push(now);
   ab.push_back(now);
   vis[x][y]=cnt;
   while(!ac.empty())
   {
       now=ac.front();
       ac.pop();
       for(int i=0;i<4;i++)
       {
           int tx=now.x+d1[i];
           int ty=now.y+d2[i];
           if(vis[tx][ty]==cnt||tx<0||ty<0||tx>=n||ty>=m||ax[tx][ty]=='*')
               continue;
             vis[tx][ty]=cnt;
               t++;
               Next.head=t;
               Next.x=tx;
               Next.y=ty;
               ac.push(Next);
               ab.push_back(Next);
       }
   }
   for(int i=0;i<ab.size();i++)
   {
       node now=ab[i];
       record[ab[i].x][ab[i].y]=t;
       vt[ab[i].x][ab[i].y]=cnt;
   }
}
map<int,bool> aa;
int main()
{
    memset(vis,0,sizeof(vis));
    memset(record,0,sizeof(record));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
      scanf("%s",ax[i]);
    for(int i=0;i<n;i++)
       for(int j=0;j<m;j++)
         if(ax[i][j]=='.'&&!record[i][j])
           {
             bfs(i,j);
             cnt++;
           }
     for(int i=0;i<n;i++)
     {
         for(int j=0;j<m;j++)
         {
             if(ax[i][j]=='.')
                printf(".");
             else
             {
                 int ans=0;
                 for(int t=0;t<4;t++)
                 {
                    int tx=i+d1[t];
                    int ty=j+d2[t];
                   if(tx<0||ty<0||tx>=n||ty>=m||ax[tx][ty]=='*')
                       continue;
                     if(!aa[vt[tx][ty]])
                     {
                         ans+=record[tx][ty];
                         aa[vt[tx][ty]]=1;
                     }
                 }
                 printf("%d",ans+1);
                 aa.clear();
             }
         }
         printf("\n");
     }

    return 0;
}


#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int n,m,cnt=1;
char ax[1005][1005];
int vis[1005][1005];
int record[1005][1005];
int d1[]={0,0,-1,1};
int d2[]={1,-1,0,0};
struct node
{
    int x,y,head;
};
void bfs(int x,int y)
{
   queue<node> ac;
   node now,Next;
   now.x=x;
   now.y=y;
   now.head=1;
   int t=1;
   ac.push(now);
   vis[x][y]=cnt;
   while(!ac.empty())
   {
       now=ac.front();
       ac.pop();
       for(int i=0;i<4;i++)
       {
           int tx=now.x+d1[i];
           int ty=now.y+d2[i];
           if(vis[tx][ty]==cnt||tx<0||ty<0||tx>=n||ty>=m||ax[tx][ty]=='*')
               continue;
           // cout<<tx<<"  "<<ty<<" "<<cnt<<endl;
             vis[tx][ty]=cnt;
               t++;
               Next.head=t;
               Next.x=tx;
               Next.y=ty;
               ac.push(Next);
       }
   }
   record[x][y]=t;
}
int main()
{
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
      scanf("%s",ax[i]);
    for(int i=0;i<n;i++)
    {
          for(int j=0;j<m;j++)
    {
        record[i][j]=-1;
        if(ax[i][j]=='*')
        {
            bfs(i,j);
            cnt++;
        }
        if(record[i][j]==-1)
            printf(".");
        else
            printf("%d",record[i][j]%10);

    }
        printf("\n");
    }

    return 0;
}


#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
int n,m,cnt=1;
char ax[1005][1005];
int vis[1005][1005];
int record[1005][1005];
int d1[]={0,0,-1,1};
int d2[]={1,-1,0,0};
struct node
{
    int x,y,head;
};
void bfs(int x,int y)
{
   queue<node> ac;
   node now,Next;
   now.x=x;
   now.y=y;
   now.head=1;
   int t=1;
   ac.push(now);
   vis[x][y]=cnt;
   while(!ac.empty())
   {
       now=ac.front();
       ac.pop();
       for(int i=0;i<4;i++)
       {
           int tx=now.x+d1[i];
           int ty=now.y+d2[i];
           if(vis[tx][ty]==cnt||tx<0||ty<0||tx>=n||ty>=m||ax[tx][ty]=='*')
               continue;
           // cout<<tx<<"  "<<ty<<" "<<cnt<<endl;
             vis[tx][ty]=cnt;
               t++;
               Next.head=t;
               Next.x=tx;
               Next.y=ty;
               ac.push(Next);
       }
   }
   record[x][y]=t;
}
int main()
{
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
      scanf("%s",ax[i]);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
    {
        record[i][j]=-1;
        if(ax[i][j]=='*')
        {
            bfs(i,j);
            cnt++;
        }
        if(record[i][j]==-1)
            printf(".");
        else
            printf("%d",record[i][j]%10);
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef unsigned long long ll;
const int maxn = 1010;
int n, m;
char s[maxn][maxn];
int mp[maxn<<1], vis[maxn][maxn];
int dir[5][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x, y;
};
il bool in(int x, int y) {
	return x>=0&&x<n&&y>=0&&y<m;
}
il void dfs(int x, int y, int num) {//记搜
	if(!in(x, y)) return;
	if(s[x][y] == '*') return;
	if(vis[x][y]) return;
	mp[num]++;
	vis[x][y] = num;
	for(re int i = 0; i < 4; ++i) {
		int fx = x+dir[i][0], fy = y+dir[i][1];
		dfs(fx, fy, num);
	}
	/*queue<node> q;
	node ne;
	ne.x = a, ne.y = b;
	q.push(ne);
	memset(vis, 0, sizeof(vis));
	vis[a][b] = 1;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		for(re int i = 0; i < 4; ++i) {
			int fx = x+dir[i][0];
			int fy = y+dir[i][1];
			if(in(fx, fy) && s[fx][fy] == '.' && !vis[fx][fy]) {
				mp[a][b]++;
				node next;
				next.x = fx, next.y = fy;
				vis[fx][fy] = 1;
				q.push(next);
			}
		}
	}*/
}
il void work() {
	int cnt = 0;
	for(re int i = 0; i < n; ++i)
		for(re int j = 0; j < m; ++j)
			if(s[i][j] == '.' && vis[i][j] == 0) {
				cnt++;
				dfs(i, j, cnt);
			}
}
int main() {
	memset(vis, 0, sizeof(vis));
	scanf("%d%d", &n, &m);
	for(re int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	work();
	for(re int i = 0; i < n; ++i) {
		for(re int j = 0; j < m; ++j) {
			if(s[i][j]=='.')putchar('.');
			else {
				int ans = 1;
				set<int> tag;
				for(re int k = 0; k < 4; ++k) {
					int x = i+dir[k][0], y = j+dir[k][1];
					if(!in(x, y)) continue;
					if(s[x][y] == '*') continue;
					if(!tag.count(vis[x][y])) {
						tag.insert(vis[x][y]);
						ans += mp[vis[x][y]];
					}
				}
				printf("%d", ans%10);
			}
		}
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
#define N 1005
using namespace std;
char s[N][N];
int color[N][N];
int Max[N*N];
const int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
int X,Y;
bool In(int x,int y)
{
    return x>=0 and x<X and y>=0 and y<Y;
}
void check(int x,int y,int colour)
{
    if(!In(x,y))return;
    if(color[x][y])return;
    if(s[x][y]=='*')return;
    Max[colour]++;
    color[x][y]=colour;
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        check(xx,yy,colour);
    }
}
void Init()
{
    scanf("%d%d",&X,&Y);
    for(int i=0;i<X;i++)
    {
        scanf("%s",s[i]);
    }
    int cnt=0;
    for(int i=0;i<X;i++)
    {
        for(int j=0;j<Y;j++)
        {
            if(s[i][j]=='.' and not color[i][j])
            {
                cnt++;
                check(i,j,cnt);
            }
        }
    }
}
void Print()
{
    for(int i=0;i<X;i++)
    {
        for(int j=0;j<Y;j++)
        {
            if(s[i][j]=='.')putchar('.');
            else
            {
                int ans=0;
                set <int> dd;
                for(int k=0;k<4;k++)
                {
                    int xx=i+dx[k],yy=j+dy[k];
                    if(!In(xx,yy))continue;
                    if(s[xx][yy]=='*')continue;
                    if(!dd.count(color[xx][yy]))
                    {
                        dd.insert(color[xx][yy]);
                        ans+=Max[color[xx][yy]];
                    }
                }
                printf("%d",(ans+1)%10);
            }
        }
        putchar('\n');
    }
}
int main()
{
    Init();
    Print();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef unsigned long long ll;
const int maxn = 1010;
int n, m;
char s[maxn][maxn];
int mp[maxn<<1], vis[maxn][maxn];
int dir[5][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x, y;
};
il bool in(int x, int y) {
	return x>=0&&x<n&&y>=0&&y<m;
}
il void dfs(int x, int y, int num) {//记搜
	if(!in(x, y)) return;
	if(s[x][y] == '*') return;
	if(vis[x][y] != 0) return;
	mp[num]++;
	vis[x][y] = num;
	for(re int i = 0; i < 4; ++i) {
		int fx = x+dir[i][0], fy = y+dir[i][1];
		dfs(fx, fy, num);
	}
	/*queue<node> q;
	node ne;
	ne.x = a, ne.y = b;
	q.push(ne);
	memset(vis, 0, sizeof(vis));
	vis[a][b] = 1;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		for(re int i = 0; i < 4; ++i) {
			int fx = x+dir[i][0];
			int fy = y+dir[i][1];
			if(in(fx, fy) && s[fx][fy] == '.' && !vis[fx][fy]) {
				mp[a][b]++;
				node next;
				next.x = fx, next.y = fy;
				vis[fx][fy] = 1;
				q.push(next);
			}
		}
	}*/
}
il void work() {
	int cnt = 0;
	for(re int i = 0; i < n; ++i)
		for(re int j = 0; j < m; ++j)
			if(s[i][j] == '.' && vis[i][j] == 0)
				dfs(i, j, ++cnt);
}
int main() {
	memset(vis, 0, sizeof(vis));
	scanf("%d%d", &n, &m);
	for(re int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	work();
	for(re int i = 0; i < n; ++i) {
		for(re int j = 0; j < m; ++j) {
			if(s[i][j] == '.') printf(".");
			else {
				int ans = 1;
				set<int> tag;
				for(re int k = 0; k < 4; ++k) {
					int x = i+dir[k][0], y = j+dir[k][1];
					if(!in(x, y)) continue;
					if(s[x][y] == '*') continue;
					if(!tag.count(vis[x][y])) {
						tag.insert(vis[x][y]);
						ans += mp[vis[x][y]];
					}
				}
				printf("%d", ans%10);
			}
		}
		printf("\n");
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef unsigned long long ll;
const int maxn = 1010;
int n, m;
char s[maxn][maxn];
int mp[maxn<<1], vis[maxn][maxn];
int dir[5][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x, y;
};
il bool in(int x, int y) {
	return x>=0&&x<n&&y>=0&&y<m;
}
il void dfs(int x, int y, int num) {//记搜
	if(!in(x, y)) return;
	if(s[x][y] == '*') return;
	if(vis[x][y]) return;
	mp[num]++;
	vis[x][y] = num;
	for(re int i = 0; i < 4; ++i) {
		int fx = x+dir[i][0], fy = y+dir[i][1];
		dfs(fx, fy, num);
	}
	/*queue<node> q;
	node ne;
	ne.x = a, ne.y = b;
	q.push(ne);
	memset(vis, 0, sizeof(vis));
	vis[a][b] = 1;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		for(re int i = 0; i < 4; ++i) {
			int fx = x+dir[i][0];
			int fy = y+dir[i][1];
			if(in(fx, fy) && s[fx][fy] == '.' && !vis[fx][fy]) {
				mp[a][b]++;
				node next;
				next.x = fx, next.y = fy;
				vis[fx][fy] = 1;
				q.push(next);
			}
		}
	}*/
}
il void work() {
	int cnt = 0;
	for(re int i = 0; i < n; ++i)
		for(re int j = 0; j < m; ++j)
			if(s[i][j] == '.' && vis[i][j] == 0) {
				cnt++;
				dfs(i, j, cnt);
			}
}
int main() {
	scanf("%d%d", &n, &m);
	for(re int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	work();
	for(re int i = 0; i < n; ++i) {
		for(re int j = 0; j < m; ++j) {
			if(s[i][j]=='.')putchar('.');
			else {
				int ans = 0;
				set<int> tag;
				for(re int k = 0; k < 4; ++k) {
					int x = i+dir[k][0], y = j+dir[k][1];
					if(!in(x, y)) continue;
					if(s[x][y] == '*') continue;
					if(!tag.count(vis[x][y])) {
						tag.insert(vis[x][y]);
						ans += mp[vis[x][y]];
					}
				}
				printf("%d", (ans+1)%10);
			}
		}
		printf("\n");
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef unsigned long long ll;
const int maxn = 1010;
int n, m;
char s[maxn][maxn];
int mp[maxn<<1], vis[maxn][maxn];
int dir[5][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x, y;
};
il bool in(int x, int y) {
	return x>=0&&x<n&&y>=0&&y<m;
}
il void dfs(int x, int y, int num) {//记搜
	if(!in(x, y)) return;
	if(s[x][y] == '*') return;
	if(vis[x][y]) return;
	mp[num]++;
	vis[x][y] = num;
	for(re int i = 0; i < 4; ++i) {
		int fx = x+dir[i][0], fy = y+dir[i][1];
		dfs(fx, fy, num);
	}
	/*queue<node> q;
	node ne;
	ne.x = a, ne.y = b;
	q.push(ne);
	memset(vis, 0, sizeof(vis));
	vis[a][b] = 1;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		for(re int i = 0; i < 4; ++i) {
			int fx = x+dir[i][0];
			int fy = y+dir[i][1];
			if(in(fx, fy) && s[fx][fy] == '.' && !vis[fx][fy]) {
				mp[a][b]++;
				node next;
				next.x = fx, next.y = fy;
				vis[fx][fy] = 1;
				q.push(next);
			}
		}
	}*/
}
il void work() {
	int cnt = 0;
	for(re int i = 0; i < n; ++i)
		for(re int j = 0; j < m; ++j)
			if(s[i][j] == '.' && vis[i][j] == 0) {
				cnt++;
				dfs(i, j, cnt);
			}
}
int main() {
	memset(vis, 0, sizeof(vis));
	scanf("%d%d", &n, &m);
	for(re int i = 0; i < n; ++i)
		scanf("%s", &s[i]);
	work();
	for(re int i = 0; i < n; ++i) {
		for(re int j = 0; j < m; ++j) {
			if(s[i][j] == '.') printf(".");
			else {
				int ans = 1;
				set<int> tag;
				for(re int k = 0; k < 4; ++k) {
					int x = i+dir[k][0], y = j+dir[k][1];
					if(!in(x, y)) continue;
					if(s[x][y] == '*') continue;
					if(!tag.count(vis[x][y])) {
						tag.insert(vis[x][y]);
						ans += mp[vis[x][y]];
					}
				}
				printf("%d", ans%10);
			}
		}
		printf("\n");
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef unsigned long long ll;
const int maxn = 1010;
int n, m;
char s[maxn][maxn];
int mp[maxn<<1], vis[maxn][maxn];
int dir[5][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x, y;
};
il bool in(int x, int y) {
	return x>=0&&x<n&&y>=0&&y<m;
}
il void dfs(int x, int y, int num) {//记搜
	if(!in(x, y)) return;
	if(s[x][y] == '*') return;
	if(vis[x][y]) return;
	mp[num]++;
	vis[x][y] = num;
	for(re int i = 0; i < 4; ++i) {
		int fx = x+dir[i][0], fy = y+dir[i][1];
		dfs(fx, fy, num);
	}
	/*queue<node> q;
	node ne;
	ne.x = a, ne.y = b;
	q.push(ne);
	memset(vis, 0, sizeof(vis));
	vis[a][b] = 1;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		for(re int i = 0; i < 4; ++i) {
			int fx = x+dir[i][0];
			int fy = y+dir[i][1];
			if(in(fx, fy) && s[fx][fy] == '.' && !vis[fx][fy]) {
				mp[a][b]++;
				node next;
				next.x = fx, next.y = fy;
				vis[fx][fy] = 1;
				q.push(next);
			}
		}
	}*/
}
il void work() {
	int cnt = 0;
	for(re int i = 0; i < n; ++i)
		for(re int j = 0; j < m; ++j)
			if(s[i][j] == '.' && vis[i][j] == 0) {
				cnt++;
				dfs(i, j, cnt);
			}
}
int main() {
	memset(vis, 0, sizeof(vis));
	scanf("%d%d", &n, &m);
	for(re int i = 0; i < n; ++i)
		scanf("%s", &s[i]);
	work();
	for(re int i = 0; i < n; ++i) {
		for(re int j = 0; j < m; ++j) {
			if(s[i][j]=='.')putchar('.');
			else {
				int ans = 1;
				set<int> tag;
				for(re int k = 0; k < 4; ++k) {
					int x = i+dir[k][0], y = j+dir[k][1];
					if(!in(x, y)) continue;
					if(s[x][y] == '*') continue;
					if(!tag.count(vis[x][y])) {
						tag.insert(vis[x][y]);
						ans += mp[vis[x][y]];
					}
				}
				printf("%d", ans%10);
			}
		}
		printf("\n");
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define il inline
#define re register
using namespace std;
typedef unsigned long long ll;
const int maxn = 1010;
int n, m;
char s[maxn][maxn];
int mp[maxn<<1], vis[maxn][maxn];
int dir[5][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
	int x, y;
};
il bool in(int x, int y) {
	return x>=1&&x<=n&&y>=1&&y<=m;
}
il void dfs(int x, int y, int num) {//记搜
	if(!in(x, y)) return;
	if(s[x][y] == '*') return;
	if(vis[x][y] != 0) return;
	mp[num]++;
	vis[x][y] = num;
	for(re int i = 0; i < 4; ++i) {
		int fx = x+dir[i][0], fy = y+dir[i][1];
		dfs(fx, fy, num);
	}
	/*queue<node> q;
	node ne;
	ne.x = a, ne.y = b;
	q.push(ne);
	memset(vis, 0, sizeof(vis));
	vis[a][b] = 1;
	while(!q.empty()) {
		node now = q.front();
		q.pop();
		int x = now.x, y = now.y;
		for(re int i = 0; i < 4; ++i) {
			int fx = x+dir[i][0];
			int fy = y+dir[i][1];
			if(in(fx, fy) && s[fx][fy] == '.' && !vis[fx][fy]) {
				mp[a][b]++;
				node next;
				next.x = fx, next.y = fy;
				vis[fx][fy] = 1;
				q.push(next);
			}
		}
	}*/
}
il void work() {
	int cnt = 1;
	for(re int i = 1; i <= n; ++i)
		for(re int j = 1; j <= m; ++j)
			if(s[i][j] == '.' && vis[i][j] == 0)
				dfs(i, j, ++cnt);
}
int main() {
	memset(vis, 0, sizeof(vis));
	scanf("%d%d", &n, &m);
	for(re int i = 1; i <= n; ++i)
		scanf("%s", s[i]+1);
	work();
	for(re int i = 1; i <= n; ++i) {
		for(re int j = 1; j <= m; ++j) {
			if(s[i][j] == '.') printf(".");
			else {
				int ans = 1;
				set<int> tag;
				for(re int k = 0; k < 4; ++k) {
					int x = i+dir[k][0], y = j+dir[k][1];
					if(!in(x, y)) continue;
					if(s[x][y] == '*') continue;
					if(!tag.count(vis[x][y])) {
						tag.insert(vis[x][y]);
						ans += mp[vis[x][y]];
					}
				}
				printf("%d", ans%10);
			}
		}
		printf("\n");
	}
	return 0;
}


#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std;

int _;

const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int n,m,cnt=0,num[1000500],sum[1000500];
bool vis[1050][1050];
string s[1050],ans[1050];
vector<pair<int,int> > wall;
set<int> S;

void dfs(int x,int y)
{
	if(x<0 || y<0 || x >= n || y >= m || vis[x][y] || s[x][y] != '.')
		return;
	vis[x][y]=true;
	num[cnt]++;
	sum[x*n+y]=cnt;
	for(int i=0;i<4;i++)
		dfs(x+dx[i],y+dy[i]);
	return;
}

int main()
{
	//for(scanf("%d",&_);_;_--)
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		s[i].resize(m);
		scanf("%s",&s[i][0]);
		ans[i]=s[i];
		for(int j=0;j<m;j++)
		{
			if(s[i][j] == '*')
				wall.push_back(pair<int,int> (i,j));
			vis[i][j]=false;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j] == '.' && !vis[i][j])
			{
				sum[i*n+j]=++cnt;
				num[cnt]=0;
				dfs(i,j);
			}
	for(int i=0;i<wall.size();i++)
	{
		int x=wall[i].first,y=wall[i].second,su=0;
		S.clear();
		for(int i=0;i<4;i++)
		{
			int xx=x+dx[i],yy=y+dy[i],si=S.size();
			if(xx<0 || yy<0 || xx >= n || yy >= m || s[xx][yy] == '*')
				continue;
			S.insert(sum[xx*n+yy]);
			if(si != S.size())
				su += num[sum[xx*n+yy]];
		}
		ans[x][y]='0'+(su+1)%10;
	}
	for(int i=0;i<n;i++)
		printf("%s\n",ans[i].c_str());
	return 0;
}


#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std;

int _;

const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int n,m,cnt=0,num[1000500],sum[1000500];
bool vis[1050][1050];
string s[1050],ans[1050];
vector<pair<int,int> > wall;
set<int> S;

void dfs(int x,int y)
{
	if(x<0 || y<0 || x >= n || y >= m || vis[x][y] || s[x][y] != '.')
		return;
	vis[x][y]=true;
	num[cnt]++;
	sum[x*n+y]=cnt;
	for(int i=0;i<4;i++)
		dfs(x+dx[i],y+dy[i]);
	return;
}

int main()
{
	//for(scanf("%d",&_);_;_--)
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		s[i].resize(m);
		scanf("%s",&s[i][0]);
		ans[i]=s[i];
		for(int j=0;j<m;j++)
		{
			if(s[i][j] == '*')
				wall.push_back(pair<int,int> (i,j));
			vis[i][j]=false;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j] == '.' && !vis[i][j])
			{
				sum[i*n+j]=++cnt;
				num[cnt]=0;
				dfs(i,j);
			}
	for(int i=0;i<wall.size();i++)
	{
		int x=wall[i].first,y=wall[i].second,su=0;
		for(int i=0;i<4;i++)
		{
			S.clear();
			int xx=x+dx[i],yy=y+dy[i],si=S.size();
			if(xx<0 || yy<0 || xx >= n || yy >= m || s[xx][yy] == '*')
				continue;
			S.insert(sum[xx*n+yy]);
			if(si != S.size())
				su += num[sum[xx*n+yy]];
		}
		ans[x][y]='0'+(su+1)%10;
	}
	for(int i=0;i<n;i++)
		printf("%s\n",ans[i].c_str());
	return 0;
}


#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std;

int _;

const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int n,m,cnt=0,num[1005000],sum[1005000];
bool vis[1050][1050];
string s[1050],ans[1050];
vector<pair<int,int> > wall;
set<int> S;

void dfs(int x,int y)
{
	if(x<0 || y<0 || x >= n || y >= m || vis[x][y] || s[x][y] != '.')
		return;
	vis[x][y]=true;
	num[cnt]++;
	sum[x*max(n,m)+y]=cnt;
	for(int i=0;i<4;i++)
		dfs(x+dx[i],y+dy[i]);
	return;
}

int main()
{
	//for(scanf("%d",&_);_;_--)
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		s[i].resize(m);
		scanf("%s",&s[i][0]);
		ans[i]=s[i];
		for(int j=0;j<m;j++)
		{
			if(s[i][j] == '*')
				wall.push_back(pair<int,int> (i,j));
			vis[i][j]=false;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j] == '.' && !vis[i][j])
			{
				num[++cnt]=0;
				dfs(i,j);
			}
	for(int i=0;i<wall.size();i++)
	{
		int x=wall[i].first,y=wall[i].second,su=0;
		S.clear();
		for(int i=0;i<4;i++)
		{
			int xx=x+dx[i],yy=y+dy[i],si=S.size();
			if(xx<0 || yy<0 || xx >= n || yy >= m || s[xx][yy] == '*')
				continue;
			S.insert(sum[xx*max(n,m)+yy]);
			if(si != S.size())
				su += num[sum[xx*(n,m)+yy]];
		}
		ans[x][y]='0'+(su+1)%10;
	}
	for(int i=0;i<n;i++)
		printf("%s\n",ans[i].c_str());
	return 0;
}


#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std;

int _;

const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int n,m,cnt=0,num[1000500],sum[1000500];
bool vis[1050][1050];
string s[1050],ans[1050];
vector<pair<int,int> > wall;
set<int> S;

void dfs(int x,int y)
{
	if(x<0 || y<0 || x >= n || y >= m || vis[x][y] || s[x][y] != '.')
		return;
	vis[x][y]=true;
	num[cnt]++;
	sum[x*n+y]=cnt;
	for(int i=0;i<4;i++)
		dfs(x+dx[i],y+dy[i]);
	return;
}

int main()
{
	//for(scanf("%d",&_);_;_--)
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		cin >> s[i];
		ans[i]=s[i];
		for(int j=0;j<m;j++)
		{
			if(s[i][j] == '*')
				wall.push_back(pair<int,int> (i,j));
			vis[i][j]=false;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j] == '.' && !vis[i][j])
			{
				sum[i*n+j]=++cnt;
				num[cnt]=0;
				dfs(i,j);
			}
	for(int i=0;i<wall.size();i++)
	{
		int x=wall[i].first,y=wall[i].second,su=0;
		S.clear();
		for(int i=0;i<4;i++)
		{
			int xx=x+dx[i],yy=y+dy[i],si=S.size();
			if(xx<0 || yy<0 || xx >= n || yy >= m || s[xx][yy] == '*')
				continue;
			S.insert(sum[xx*n+yy]);
			if(si != S.size())
				su += num[sum[xx*n+yy]];
		}
		ans[x][y]='0'+(su+1)%10;
	}
	for(int i=0;i<n;i++)
		printf("%s\n",ans[i].c_str());
	return 0;
}


#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std;

int _;

const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

int n,m,cnt=0,num[1005000],sum[1005000];
bool vis[1050][1050];
string s[1050],ans[1050];
vector<pair<int,int> > wall;
set<int> S;

void dfs(int x,int y)
{
	if(x<0 || y<0 || x >= n || y >= m || vis[x][y] || s[x][y] != '.')
		return;
	vis[x][y]=true;
	num[cnt]++;
	sum[x*max(n,m)+y]=cnt;
	for(int i=0;i<4;i++)
		dfs(x+dx[i],y+dy[i]);
	return;
}

int main()
{
	//for(scanf("%d",&_);_;_--)
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		s[i].resize(m);
		scanf("%s",&s[i][0]);
		ans[i]=s[i];
		for(int j=0;j<m;j++)
		{
			if(s[i][j] == '*')
				wall.push_back(pair<int,int> (i,j));
			vis[i][j]=false;
		}
	}
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			if(s[i][j] == '.' && !vis[i][j])
			{
				num[++cnt]=0;
				dfs(i,j);
			}
	for(int i=0;i<wall.size();i++)
	{
		int x=wall[i].first,y=wall[i].second,su=0;
		S.clear();
		for(int i=0;i<4;i++)
		{
			int xx=x+dx[i],yy=y+dy[i],si=S.size();
			if(xx<0 || yy<0 || xx >= n || yy >= m || s[xx][yy] == '*')
				continue;
			S.insert(sum[xx*max(n,m)+yy]);
			if(si != S.size())
				su += num[sum[xx*max(n,m)+yy]];
		}
		ans[x][y]='0'+(su+1)%10;
	}
	for(int i=0;i<n;i++)
		printf("%s\n",ans[i].c_str());
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,c;
char a[1001][1001];
int vis[1001][1001]={0};
void dfs(int x,int y,int &c)
{
         if(x<0||x>n-1||y<0||y>m-1||a[x][y]=='*'||vis[x][y]==1)
        return;
        vis[x][y]=1;
         dfs(x+1,y,c);
         dfs(x-1,y,c);
          dfs(x,y-1,c);
            dfs(x,y+1,c);

        c++;

}
int main()
{
        ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
                for(int j=0;j<m;j++)
                {
                        cin>>a[i][j];
                       // cout<<vis[i][j]<<" ";
                }//cout<<endl;
        }
     //   int c=0;
       //  dfs(0,0,c);

        for(int i=0;i<n;i++)
        {
                for(int j=0;j<m;j++)
                {
                         c=0;
                     if(a[i][j]=='*')
                     {
                             a[i][j]='.';
                             dfs(i,j,c);
                             a[i][j]='*';
                            // cout<<endl<<endl<<endl;
                             cout<<c;
                             memset(vis,0,sizeof(vis));
                     }else cout<<".";
                }cout<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,c;
char a[1001][1001];
int vis[1001][1001]={0};
void dfs(int x,int y,int &c)
{
         if(x<0||x>n-1||y<0||y>m-1||a[x][y]=='*'||vis[x][y]==1)
        return;
        vis[x][y]=1;
         dfs(x+1,y,c);
         dfs(x-1,y,c);
          dfs(x,y-1,c);
            dfs(x,y+1,c);

        c++;

}
int main()
{
        ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
                for(int j=0;j<m;j++)
                {
                        cin>>a[i][j];
                       // cout<<vis[i][j]<<" ";
                }//cout<<endl;
        }
     //   int c=0;
       //  dfs(0,0,c);

        for(int i=0;i<n;i++)
        {
                for(int j=0;j<m;j++)
                {
                         c=0;
                     if(a[i][j]=='*')
                     {
                             a[i][j]='.';
                             dfs(i,j,c);
                             a[i][j]='*';
                            // cout<<endl<<endl<<endl;
                             cout<<c%10;
                             memset(vis,0,sizeof(vis));
                     }else cout<<".";
                }cout<<endl;
        }
        return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define ff(i,a,b) for(int i = (a); i <= (b); i++)
#define fr(i,a,b) for(int i = (a); i >= (b); i--)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define sc2( x , y ) scanf( "%d%d" , &x , &y )
#define scd( x ) scanf( "%.9f" , &x )
#define scl( x ) scanf( "%I64d" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%.9f\n" , x )
#define pfl( x ) printf( "%I64d\n" , x )
#define rrc( x ) return cout << x , 0 ;
//#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define endl '\n'
#define SZ(a) int(a.size())
#define pb push_back
#define cln( x , v ) memset( x , v , sizeof( x ) )
#define pi acos(-1.0)
#define e2( x ) ( 1LL*( x )*( x ) )
#define r2( x ) sqrt( 1.0*( x ) )
#define ones(x) __builtin_popcount(x)
#define MCM( a , b ) ( ( a*b )/( __gcd( a , b ) ) )
#define ddd cout << "despues" << endl ;
#define sss cout << "------------------" << endl ;
#define aaa cout << "antes" << endl ;
#define da( a , b ) ( (a)/(b) - ( (a) < 0 && (a)%(b) != 0 ) )
#define ceil_( a , b ) ( da( (a) , (b) ) + ((a)%(b) > 0) )
#define Mm greater<int>
#define nxtPerm next_permutation
#define L( n ) 2 * n
#define R( n ) 2 * n + 1
#define A 1 , 0 , n - 1

typedef double db ;
typedef long double ld ;
typedef long long ll ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<ll> vl ;
typedef vector<bool> vb ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;
const ld EPS = 1e-8 ;
const int INF = (int)( 1e8 ) ;
const ll mod = (int)( 1e+9 + 7 ) ;
const int N = (int)( 1e3 ) ;
int days_month[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
//inline ll modulo( ll num ){ ( ( num %= mod ) += mod ) %= mod ; return num ; }
//inline ll pot( int b , int e ){ ll p = 1 ; REP( i , e ) p = (p*b)%mod ; return p ; }

char t[ N + 1 ][ N + 1 ] ;
bool vis[ N + 1 ][ N + 1 ] ;
int id[ N + 1 ][ N + 1 ] ;
int cnt[ N*N + 1 ] ;
int dir[ 5 ] = { 1 , 0 , -1 , 0 , 1 } ;
int n , m ;
pii a , b ;

inline bool val( int x , int y ) {
	bool a = ( 1 <= x && x <= n ) ;
	bool b = ( 1 <= y && y <= m ) ;
	return a && b ;
}

queue<pii> q ;
queue<pii> path ;

void bfs( pii st , int mark ) {
	int cnt_ = 0 ;
	vis[ st.fi ][ st.se ] = true ;
	q.push( st ) ;
	while( !q.empty() ) {
		a = q.front() ;
		path.push( a ) ;
		q.pop() ;
		cnt_ ++ ;
		REP( d , 4 ) {
			b.fi = a.fi + dir[ d ] ;
			b.se = a.se + dir[ d + 1 ] ;
			if( !val( b.fi , b.se ) ) continue ;
			if( t[ b.fi ][ b.se ] == '*' ) continue ;
			if( vis[ b.fi ][ b.se ] ) continue ;
			vis[ b.fi ][ b.se ] = true ;
			q.push( b ) ;
		}
	}
	while( !path.empty() ) {
		a = path.front() ;
		path.pop() ;
		id[ a.fi ][ a.se ] = mark ;
	}
	cnt[ mark ] = cnt_ ;
}

void update( int x , int y ) {
	int tot = 1 ;
	set<int> ids ;
	REP( d , 4 ) {
		int nx = x + dir[ d ] ;
		int ny = y + dir[ d + 1 ] ;
		if( !val( nx , ny ) ) continue ;
		if( t[ nx ][ ny ] == '*' ) continue ;
		int id_ = id[ nx ][ ny ] ;
		if( ids.count( id_ ) ) continue ;
		ids.insert( id_ ) ;
		tot += cnt[ id_ ] ;
	}
	t[ x ][ y ] = '0' + tot%10 ;
}

int main()
{
//	ios_base::sync_with_stdio(0);
	cin >> n >> m ;
	ff( i , 1 , n ) {
		scanf( "%s" , t[ i ] + 1 ) ;
	}
	cln( id , -1 ) ;
	ff( i , 1 , n ) {
		ff( j , 1 , m ) {
			if( vis[ i ][ j ] ) continue ;
			if( t[ i ][ j ] == '*' ) continue ;
			int fat = ( i - 1 )*N + ( j - 1 ) ;
			bfs( { i , j } , fat ) ;
		}
	}
	ff( i , 1 , n ) {
		ff( j , 1 , m ) {
			if( t[ i ][ j ] == '*' ) {
				update( i , j ) ;
			}
		}
	}
	ff( i , 1 , n ) {
		printf( "%s\n" , t[ i ] + 1 ) ;
	}

	return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define ff(i,a,b) for(int i = (a); i <= (b); i++)
#define fr(i,a,b) for(int i = (a); i >= (b); i--)
#define REP( i , n ) for( int i = 0 ; i < n ; i++ )
#define REPI( n , i ) for( int i = n ; i >= 0 ; i-- )
#define sc( x ) scanf( "%d" , &x )
#define sc2( x , y ) scanf( "%d%d" , &x , &y )
#define scd( x ) scanf( "%.9f" , &x )
#define scl( x ) scanf( "%I64d" , &x )
#define pf( x ) printf( "%d\n" , x )
#define pfd( x ) printf( "%.9f\n" , x )
#define pfl( x ) printf( "%I64d\n" , x )
#define rrc( x ) return cout << x , 0 ;
//#define all( v ) v.begin(),v.end()
#define all_r( v ) v.rbegin() , v.rend()
#define fi first
#define se second
#define endl '\n'
#define SZ(a) int(a.size())
#define pb push_back
#define cln( x , v ) memset( x , v , sizeof( x ) )
#define pi acos(-1.0)
#define e2( x ) ( 1LL*( x )*( x ) )
#define r2( x ) sqrt( 1.0*( x ) )
#define ones(x) __builtin_popcount(x)
#define MCM( a , b ) ( ( a*b )/( __gcd( a , b ) ) )
#define ddd cout << "despues" << endl ;
#define sss cout << "------------------" << endl ;
#define aaa cout << "antes" << endl ;
#define da( a , b ) ( (a)/(b) - ( (a) < 0 && (a)%(b) != 0 ) )
#define ceil_( a , b ) ( da( (a) , (b) ) + ((a)%(b) > 0) )
#define Mm greater<int>
#define nxtPerm next_permutation
#define L( n ) 2 * n
#define R( n ) 2 * n + 1
#define A 1 , 0 , n - 1

typedef double db ;
typedef long double ld ;
typedef long long ll ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<ll> vl ;
typedef vector<bool> vb ;
typedef pair<int,int> pii ;
typedef vector<pii> vpii ;
const ld EPS = 1e-8 ;
const int INF = (int)( 1e8 ) ;
const ll mod = (int)( 1e+9 + 7 ) ;
const int N = (int)( 1e3 ) ;
int days_month[ 12 ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
//inline ll modulo( ll num ){ ( ( num %= mod ) += mod ) %= mod ; return num ; }
//inline ll pot( int b , int e ){ ll p = 1 ; REP( i , e ) p = (p*b)%mod ; return p ; }

char t[ N + 1 ][ N + 1 ] ;
int id[ N + 1 ][ N + 1 ] ;
int cnt[ N*N ] ;
int dir[ 5 ] = { 1 , 0 , -1 , 0 , 1 } ;
int n , m ;
pii a , b ;

inline bool val( int x , int y ) {
	bool a = ( 1 <= x && x <= n ) ;
	bool b = ( 1 <= y && y <= m ) ;
	return a && b ;
}

void bfs( int x , int y , int mark ) {
	int cnt_ = 1 ;
	id[ x ][ y ] = mark ;
	queue<pii> q ;
	q.push( { x , y } ) ;
	while( !q.empty() ) {
		pii a = q.front() ;
		q.pop() ;
		REP( d , 4 ) {
			b.fi = a.fi + dir[ d ] ;
			b.se = a.se + dir[ d + 1 ] ;
			if( !val( b.fi , b.se ) ) continue ;
			if( t[ b.fi ][ b.se ] == '*' ) continue ;
			if( id[ b.fi ][ b.se ] != -1 ) continue ;
			id[ b.fi ][ b.se ] = mark ;
			cnt_ ++ ;
			q.push( b ) ;
		}
	}
	cnt[ mark ] = cnt_ ;
}

void update( int x , int y ) {
	int tot = 1 ;
	set<int> ids ;
	REP( d , 4 ) {
		int nx = x + dir[ d ] ;
		int ny = y + dir[ d + 1 ] ;
		if( !val( nx , ny ) ) continue ;
		if( t[ nx ][ ny ] != '.' ) continue ;
		int id_ = id[ nx ][ ny ] ;
		if( ids.count( id_ ) ) continue ;
		ids.insert( id_ ) ;
		tot += cnt[ id_ ] ;
	}
	t[ x ][ y ] = '0' + tot%10 ;
}

int main()
{
//	ios_base::sync_with_stdio(0);
	cin >> n >> m ;
	ff( i , 1 , n ) {
		scanf( "%s" , t[ i ] + 1 ) ;
	}
	cln( id , -1 ) ;
	ff( i , 1 , n ) {
		ff( j , 1 , m ) {
			if( t[ i ][ j ] == '*' ) continue ;
			if( id[ i ][ j ] != -1 ) continue ;
			int fat = ( i - 1 )*N + ( j - 1 ) ;
			bfs( i , j , fat ) ;
		}
	}
	ff( i , 1 , n ) {
		ff( j , 1 , m ) {
			if( t[ i ][ j ] == '*' ) {
				update( i , j ) ;
			}
		}
	}
	ff( i , 1 , n ) {
		printf( "%s\n" , t[ i ] + 1 ) ;
	}

	return 0 ;
}