/*input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \
                                                    |     |   )   \
                                                    |_____|_______)

*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <iomanip>
#include <fstream>
#include <stack>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define out(container) for (auto it : container) cout << it << " "; cout << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
  int x = 1 % mod;
  while(b){
    if(b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int test;
  cin >> test;
  while (test--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> vec, arr(n, 0);
    int v;
    for (int i = 0; i < n; i++) {
      cin >> v;
      vec.pb(v);
    }
    int ans = 0;
    if (k >= m) {
      int mx = 0;
      for (int i = 0; i < m; i++) {
        mx = max(mx, vec[i]);
        mx = max(mx, vec[n - i - 1]);
      }
      ans = mx;
    } else {
      int d = n - k;
      int x = m - k - 1;
      d = d - x - 1;
      // trace(d);
      for (int i = 0; i < n && i < n - d; i++) {
        if (i + d < n) arr[i] = max(vec[i], vec[i + d]);
      }
      for (int i = n - d; i >= 0 && i < n; i++) {
        if (i - d >= 0) arr[i] = arr[i - d];
      }
      // out(arr);
      vector <int> temp;
      deque <int> qu;
      int r = m - d;
      r %= (n - k);
      if (r == 0) r = 1;
      // trace(r);
      for (int i = 0; i < r; i++) {
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      for (int i = r; i < n && i < n - d; i++) {
        temp.pb(arr[qu.front()]);
        while (!qu.empty() && qu.front() <= i - r) {
          qu.pop_front();
        }
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      if (!qu.empty()) temp.pb(arr[qu.front()]);
      // out(temp);
      // sort(temp.begin(), temp.begin() + n - k - 1);
      sort(all(temp));
      if (k < temp.size()) ans = temp[k];
    }
    cout << ans << endl;
  }

  return 0;
}


/*input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \
                                                    |     |   )   \
                                                    |_____|_______)

*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <iomanip>
#include <fstream>
#include <stack>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define out(container) for (auto it : container) cout << it << " "; cout << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
  int x = 1 % mod;
  while(b){
    if(b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int test;
  cin >> test;
  while (test--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> vec, arr(n, 0);
    int v;
    for (int i = 0; i < n; i++) {
      cin >> v;
      vec.pb(v);
    }
    int ans = 0;
    if (k >= m) {
      int mx = 0;
      for (int i = 0; i < m; i++) {
        mx = max(mx, vec[i]);
        mx = max(mx, vec[n - i - 1]);
      }
      ans = mx;
    } else {
      int d = n - k;
      int x = m - k - 1;
      d = d - x - 1;
      // trace(d);
      // for (int i = 0; i < n && i < n - d; i++) {
      //   if (i + d < n) arr[i] = max(vec[i], vec[i + d]);
      // }
      // for (int i = n - d; i < n; i++) {
      //   if (i - d >= 0) arr[i] = arr[i - d];
      // }
      // out(arr);
      vector <int> temp;
      deque <int> qu;
      int r = m - d;
      r %= (n - k);
      if (r == 0) r = 1;
      // trace(r);
      for (int i = 0; i < r; i++) {
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      for (int i = r; i < n && i < n - d; i++) {
        temp.pb(arr[qu.front()]);
        while (!qu.empty() && qu.front() <= i - r) {
          qu.pop_front();
        }
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      if (!qu.empty()) temp.pb(arr[qu.front()]);
      // out(temp);
      // sort(temp.begin(), temp.begin() + n - k - 1);
      sort(all(temp));
      if (k < temp.size()) ans = temp[k];
    }
    cout << ans << endl;
  }

  return 0;
}


/*input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \
                                                    |     |   )   \
                                                    |_____|_______)

*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <iomanip>
#include <fstream>
#include <stack>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define out(container) for (auto it : container) cout << it << " "; cout << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
  int x = 1 % mod;
  while(b){
    if(b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int test;
  cin >> test;
  while (test--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> vec, arr(n, 0);
    int v;
    for (int i = 0; i < n; i++) {
      cin >> v;
      vec.pb(v);
    }
    int ans = 0;
    if (k >= m) {
      int mx = 0;
      for (int i = 0; i < m; i++) {
        mx = max(mx, vec[i]);
        mx = max(mx, vec[n - i - 1]);
      }
      ans = mx;
    } else {
      int d = n - k;
      int x = m - k - 1;
      d = d - x - 1;
      // trace(d);
      for (int i = 0; i >= 0 && i < n && i < n - d; i++) {
        if (i + d < n) arr[i] = max(vec[i], vec[i + d]);
      }
      for (int i = n - d; i >= 0 && i < n; i++) {
        if (i - d >= 0 && i - d < n) arr[i] = arr[i - d];
      }
      // out(arr);
      vector <int> temp;
      deque <int> qu;
      int r = m - d;
      r %= (n - k);
      if (r == 0) r = 1;
      // trace(r);
      for (int i = 0; i < r; i++) {
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      for (int i = r; i >= 0 && i < n && i < n - d; i++) {
        temp.pb(arr[qu.front()]);
        while (!qu.empty() && qu.front() <= i - r) {
          qu.pop_front();
        }
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      if (!qu.empty()) temp.pb(arr[qu.front()]);
      // out(temp);
      // sort(temp.begin(), temp.begin() + n - k - 1);
      sort(all(temp));
      if (k < temp.size()) ans = temp[k];
    }
    cout << ans << endl;
  }

  return 0;
}


/*input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2

*/

/*
                                                    ______________
                                                          |       )
                                                          |   )   /
                                                          |______/
                                                    |     |      \
                                                    |     |   )   \
                                                    |_____|_______)

*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <iomanip>
#include <fstream>
#include <stack>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 1000000007;
const int inf = 1034567891;
const ll LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

#define out(container) for (auto it : container) cout << it << " "; cout << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
  int x = 1 % mod;
  while(b){
    if(b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int test;
  cin >> test;
  while (test--) {
    int n, m, k;
    cin >> n >> m >> k;
    vector <int> vec, arr(n, 0);
    int v;
    for (int i = 0; i < n; i++) {
      cin >> v;
      vec.pb(v);
    }
    int ans = 0;
    if (k >= m) {
      int mx = 0;
      for (int i = 0; i < m; i++) {
        mx = max(mx, vec[i]);
        mx = max(mx, vec[n - i - 1]);
      }
      ans = mx;
    } else {
      int d = n - k;
      int x = m - k - 1;
      d = d - x - 1;
      assert(d >= 0);
      // trace(d);
      for (int i = 0; i >= 0 && i < n && i < n - d; i++) {
        if (i + d < n) arr[i] = max(vec[i], vec[i + d]);
      }
      for (int i = n - d; i >= 0 && i < n; i++) {
        if (i - d >= 0 && i - d < n) arr[i] = arr[i - d];
      }
      // out(arr);
      vector <int> temp;
      deque <int> qu;
      int r = m - d;
      r %= (n - k);
      if (r == 0) r = 1;
      // trace(r);
      for (int i = 0; i < r; i++) {
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      for (int i = r; i >= 0 && i < n && i < n - d; i++) {
        temp.pb(arr[qu.front()]);
        while (!qu.empty() && qu.front() <= i - r) {
          qu.pop_front();
        }
        while (!qu.empty() && arr[qu.back()] >= arr[i])
          qu.pop_back();
        qu.push_back(i);
      }
      if (!qu.empty()) temp.pb(arr[qu.front()]);
      // out(temp);
      // sort(temp.begin(), temp.begin() + n - k - 1);
      sort(all(temp));
      if (k < temp.size()) ans = temp[k];
    }
    cout << ans << endl;
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl "\n"
#define db(x) cout<<#x<<" ->"<<x<<endl
#define db2(x,y) cout<<#x<<"->"<<x<<", "<<#y<<" -> "<<y<<endl
#define db3(x,y,z) cout<<#x<<" -> "<<x<<" , "<<#y<<" -> "<<y<<" , "<<#z<<" -> "<<z<<endl
#define db4(w,x,y,z) cout<<#w <<" -> " << w << " , " << #x<<" -> "<<x<<" , "<<#y<<" -> "<<y<<" , "<<#z<<" -> "<<z<<endl
#define prt(x) for(auto it = x.begin(); it!=x.end(); it++) { cout<<*it<<" "; } cout<< endl
#define IN freopen("input.txt","r",stdin)

const int inf = 1LL << 60;

int32_t main(){
//    ios_base::sync_with_stdio(false); cin.tie(0);
//    IN ;
    int tc;
    cin >> tc;
    while(tc--){
        int n, m,p;
        cin >> n >> m >> p;
        deque< int > dq;
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            dq.push_back(x);
        }
        if(m==1){
            cout << max(dq.front(),dq.back()) << endl;
            continue;
        }
        int ans = -inf;
        int bal = inf;
        for(int i = 0; i <= min(m,p); i++){
            int bam = i ;
            int dan = min(m,p) - i;
            deque< int > temp = dq;
           // db2(bam,dan);
            while(bam-- && bam){
                temp.pop_front();
            }
            while(dan-- && dan){
                temp.pop_back();
            }
           // prt(temp);
            int done = p;
            int lol = inf;
            deque< int > x = temp;
            for(int i = 0; i < temp.size() && done < m; i++,done++){
                if(done==m-1){ break ;}
                temp.pop_back();
            }
           // prt(temp);
            lol = max(temp.front(),temp.back());
            done = p;
            for(int i = x.size()-1; i >= 0 && done < m; i-- , done++){
                if(done==m-1){ break ; }
                x.pop_front();
            }
          //  prt(x);
            lol = min(lol,max(x.front(),x.back()) );
            ans = max(lol,ans);
         //   db(ans);
            if(p==0){ bal = min(bal,lol) ; }
        }
        if(p==0){ cout << bal << endl; continue;}
        cout << ans << endl;
       // break;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define endl "\n"
#define db(x) cout<<#x<<" ->"<<x<<endl
#define db2(x,y) cout<<#x<<"->"<<x<<", "<<#y<<" -> "<<y<<endl
#define db3(x,y,z) cout<<#x<<" -> "<<x<<" , "<<#y<<" -> "<<y<<" , "<<#z<<" -> "<<z<<endl
#define db4(w,x,y,z) cout<<#w <<" -> " << w << " , " << #x<<" -> "<<x<<" , "<<#y<<" -> "<<y<<" , "<<#z<<" -> "<<z<<endl
#define prt(x) for(auto it = x.begin(); it!=x.end(); it++) { cout<<*it<<" "; } cout<< endl
#define IN freopen("input.txt","r",stdin)

const int inf = 1LL << 60;

int32_t main(){
//    ios_base::sync_with_stdio(false); cin.tie(0);
//    IN ;
    int tc;
    cin >> tc;
    while(tc--){
        int n, m,p;
        cin >> n >> m >> p;
        deque< int > dq;
        for(int i = 0; i < n; i++){
            int x;
            cin >> x;
            dq.push_back(x);
        }
        if(m==1){
            cout << max(dq.front(),dq.back()) << endl;
            continue;
        }
        if(m==n){
            cout << *min_element(dq.begin(),dq.end()) << endl; continue;
        }
        int ans = -inf;
        int bal = inf;
        for(int i = 0; i <= min(m,p); i++){
            int bam = i ;
            int dan = min(m,p) - i;
            deque< int > temp = dq;
           // db2(bam,dan);
            while(bam--){
                temp.pop_front();
            }
            while(dan--){
                temp.pop_back();
            }
           // prt(temp);
            int done = p;
            int lol = inf;
            deque< int > x = temp;
            for(int i = 0; i < temp.size() && done < m; i++,done++){
                if(done==m-1){ break ;}
                temp.pop_back();
            }
           // prt(temp);
            lol = max(temp.front(),temp.back());
            done = p;
            for(int i = x.size()-1; i >= 0 && done < m; i-- , done++){
                if(done==m-1){ break ; }
                x.pop_front();
            }
          //  prt(x);
            lol = min(lol,max(x.front(),x.back()) );
            ans = max(lol,ans);
         //   db(ans);
            if(p==0){ bal = min(bal,lol) ; }
        }
        if(p==0){ cout << bal << endl; continue;}
        cout << ans << endl;
       // break;
    }
    return 0;
}


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

#define endl '\n'

const int T = 1002;
bool v[T][T];
bool pegos[T][T];
int vis[T][T];
int mapa[T];
int t = 0;
int n,m;

int mov[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isIn(int i, int j) {
	return (i >= 0 and i < n and j >= 0 and j < m);
}

int dfs(int i, int j) {
	vis[i][j] = t;
	int ans = 1;

	for(int k = 0;  k < 4; k++) {
		int ii = i + mov[k][0];
		int jj = j + mov[k][1];
		if(isIn(ii,jj) and v[ii][jj] == 1 and vis[ii][jj] != t)
			ans += dfs(ii,jj);
	}

	return (ans%10);
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	char tmp;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> tmp;
			if(tmp == '.') v[i][j] = 1;
			else v[i][j] = 0;
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j]) ++t, mapa[t] = dfs(i,j);

	int track = 0;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(!v[i][j]) {
				track++;
				vis[i][j] = 1;
				for(int k = 0; k < 4; k++) {
					int ii = i + mov[k][0];
					int jj = j + mov[k][1];
					if(isIn(ii,jj) and v[ii][jj] and pegos[track][vis[ii][jj]] == 0) {
						vis[i][j] += mapa[vis[ii][jj]] % 10;
						pegos[track][vis[ii][jj]] = 1;
					}
				}
				vis[i][j] %= 10;
			}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(v[i][j]) cout << '.';
			else cout << vis[i][j];
		}
		cout << endl;
	}
    return 0;
}


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

#define endl '\n'

const int T = 1005;
bool v[T][T];
int vis[T][T];
int mapa[T*T];
int t = 0;
int n,m;

int mov[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isIn(int i, int j) {
	return (i >= 0 and i < n and j >= 0 and j < m);
}

int dfs(int i, int j) {
	vis[i][j] = t;
	int ans = 1;

	for(int k = 0;  k < 4; k++) {
		int ii = i + mov[k][0];
		int jj = j + mov[k][1];
		if(isIn(ii,jj) and v[ii][jj] and !vis[ii][jj])
			ans += dfs(ii,jj);
	}

	return (ans%10);
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	char tmp;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> tmp;
			if(tmp == '.') v[i][j] = 1;
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j] and !vis[i][j]) ++t, mapa[t] = dfs(i,j);

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(!v[i][j]) {
				vis[i][j] = 1;
				set<int>pegos;
				for(int k = 0; k < 4; k++) {
					int ii = i + mov[k][0];
					int jj = j + mov[k][1];
					if(isIn(ii,jj) and v[ii][jj] and !pegos.count(vis[ii][jj])) {
						vis[i][j] += mapa[vis[ii][jj]] % 10;
						pegos.insert(vis[ii][jj]);
					}
				}
				vis[i][j] %= 10;
			}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(v[i][j]) cout << '.';
			else cout << vis[i][j];
		}
		cout << endl;
	}

    return 0;
}


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

#define endl '\n'

const int T = 1002;
bool v[T][T];
int vis[T][T];
int mapa[T];
int t = 0;
int n,m;

int mov[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isIn(int i, int j) {
	return (i >= 0 and i < n and j >= 0 and j < m);
}

int dfs(int i, int j) {
	vis[i][j] = t;
	int ans = 1;

	for(int k = 0;  k < 4; k++) {
		int ii = i + mov[k][0];
		int jj = j + mov[k][1];
		if(isIn(ii,jj) and v[ii][jj] == 1 and vis[ii][jj] != t)
			ans += dfs(ii,jj);
	}

	return (ans%10);
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	char tmp;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> tmp;
			if(tmp == '.') v[i][j] = 1;
			else v[i][j] = 0;
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j] and !vis[i][j]) ++t, mapa[t] = dfs(i,j);

	int track = 0;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(!v[i][j]) {
				track++;
				vis[i][j] = 1;
				set<int>pegos;
				for(int k = 0; k < 4; k++) {
					int ii = i + mov[k][0];
					int jj = j + mov[k][1];
					if(isIn(ii,jj) and v[ii][jj] and !pegos.count(vis[ii][jj])) {
						vis[i][j] += mapa[vis[ii][jj]] % 10;
						pegos.insert(vis[ii][jj]);
					}
				}
				vis[i][j] %= 10;
			}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(v[i][j]) cout << '.';
			else cout << vis[i][j];
		}
		cout << endl;
	}

    return 0;
}


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

#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int T = 1005;
char v[T][T];
char ans[T][T];
int vis[T][T];
int mapa[T];
int t = 0;
int n,m;

int mov[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isIn(int i, int j) {
	return (i >= 0 and i < n and j >= 0 and j < m);
}

int dfs(int i, int j) {
	vis[i][j] = t;
	int ans = 1;

	for(int k = 0;  k < 4; k++) {
		int ii = i + mov[k][0];
		int jj = j + mov[k][1];
		if(isIn(ii,jj) and v[ii][jj] == '.'and vis[ii][jj] != t)
			ans += dfs(ii,jj);
	}

	return (ans%10);
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			cin >> v[i][j], ans[i][j] = '.';

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j] == '.') ++t, mapa[t] = dfs(i,j);

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j] == '*') {
				set<int> pegos;
				bool flag = false;
				ans[i][j] = 1;
				for(int k = 0; k < 4; k++) {
					int ii = i + mov[k][0];
					int jj = j + mov[k][1];
					if(isIn(ii,jj) and v[ii][jj] == '.' and !pegos.count(vis[ii][jj])) {
						ans[i][j] += mapa[vis[ii][jj]] % 10;
						pegos.insert(vis[ii][jj]);
						flag = true;
					}
				}
				if(!flag) ans[i][j] = '0';
				else ans[i][j] %= 10, ans[i][j] += '0';
			}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			cout << ans[i][j];
		cout << endl;
	}
    return 0;
}


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

#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);

const int T = 1005;
char v[T][T];
char ans[T][T];
int vis[T][T];
bool pegos[T][T];
int mapa[T];
int t = 0;
int n,m;

int mov[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isIn(int i, int j) {
	return (i >= 0 and i < n and j >= 0 and j < m);
}

int dfs(int i, int j) {
	vis[i][j] = t;
	int ans = 1;

	for(int k = 0;  k < 4; k++) {
		int ii = i + mov[k][0];
		int jj = j + mov[k][1];
		if(isIn(ii,jj) and v[ii][jj] == '.'and vis[ii][jj] != t)
			ans += dfs(ii,jj);
	}

	return (ans%10);
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			cin >> v[i][j], ans[i][j] = '.';

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j] == '.') ++t, mapa[t] = dfs(i,j);

	int track = 0;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j] == '*') {
				track++;
				bool flag = false;
				ans[i][j] = 1;
				for(int k = 0; k < 4; k++) {
					int ii = i + mov[k][0];
					int jj = j + mov[k][1];
					if(isIn(ii,jj) and v[ii][jj] == '.' and pegos[track][vis[ii][jj]] == 0) {
						ans[i][j] += mapa[vis[ii][jj]] % 10;
						pegos[track][vis[ii][jj]] = 1;
						flag = true;
					}
				}
				if(!flag) ans[i][j] = '0';
				else ans[i][j] %= 10, ans[i][j] += '0';
			}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			cout << ans[i][j];
		cout << endl;
	}
    return 0;
}


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

#define endl '\n'

const int T = 1002;
bool v[T][T];
bool pegos[T][T];
string ans[T];
int vis[T][T];
int mapa[T];
int t = 0;
int n,m;

int mov[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isIn(int i, int j) {
	return (i >= 0 and i < n and j >= 0 and j < m);
}

int dfs(int i, int j) {
	vis[i][j] = t;
	int ans = 1;

	for(int k = 0;  k < 4; k++) {
		int ii = i + mov[k][0];
		int jj = j + mov[k][1];
		if(isIn(ii,jj) and v[ii][jj] == 1 and vis[ii][jj] != t)
			ans += dfs(ii,jj);
	}

	return (ans%10);
}

int main() {
    ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	char tmp;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			cin >> tmp;
			if(tmp == '.') v[i][j] = 1;
			else v[i][j] = 0;
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(v[i][j]) ++t, mapa[t] = dfs(i,j);

	int track = 0;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			if(!v[i][j]) {
				track++;
				vis[i][j] = 1;
				for(int k = 0; k < 4; k++) {
					int ii = i + mov[k][0];
					int jj = j + mov[k][1];
					if(isIn(ii,jj) and v[ii][jj] and pegos[track][vis[ii][jj]] == 0) {
						vis[i][j] += mapa[vis[ii][jj]] % 10;
						pegos[track][vis[ii][jj]] = 1;
					}
				}
				vis[i][j] %= 10;
			}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if(v[i][j]) ans[i] += '.';
			else ans[i] += vis[i][j] + '0';
		}

	for(int i = 0; i < n; i++) cout << ans[i] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int n,m,ans;
string str[100010];
set<pair<int,int> >s;
void dfs(int i,int j)
{
	int vivek,ojha;
	for(int k=0;k<4;k++)
	{
		vivek=i+dy[k];
		ojha=j+dx[k];

		if(vivek<0 || vivek>=n || ojha<0 || ojha>=m)continue;

		if(str[vivek][ojha]=='*')continue;
		//cout<<vivek<<" "<<ojha<<" "<<n<<" "<<m<<endl;
		if(!(binary_search(s.begin(),s.end(),make_pair(vivek,ojha))))
		{
			//cout<<"int";
			s.insert(make_pair(vivek,ojha));
			dfs(vivek,ojha);
		//	if(i==0 && j==0)
		//	cout<<ans<<endl;
		}
	}
//	return s.size();
}
int main()
{
	cin>>n;
	cin>>m;
	int i,j;
	for(i=0;i<n;i++)
		cin>>str[i];
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{

			if(str[i][j]=='*')
			{
				s.insert(make_pair(i,j));
				dfs(i,j);
		//		return 0;
				cout<<s.size();
				s.clear();
			}
			else
				cout<<".";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int n,m,ans;
string str[100010];
set<pair<int,int> >s;
void dfs(int i,int j)
{
	int vivek,ojha;
	for(int k=0;k<4;k++)
	{
		vivek=i+dy[k];
		ojha=j+dx[k];

		if(vivek<0 || vivek>=n || ojha<0 || ojha>=m)continue;

		if(str[vivek][ojha]=='*')continue;
		//cout<<vivek<<" "<<ojha<<" "<<n<<" "<<m<<endl;
		if(!(binary_search(s.begin(),s.end(),make_pair(vivek,ojha))))
		{
			//cout<<"int";
			s.insert(make_pair(vivek,ojha));
			dfs(vivek,ojha);
		//	if(i==0 && j==0)
		//	cout<<ans<<endl;
		}
	}
//	return s.size();
}
int main()
{
	cin>>n;
	cin>>m;
	int i,j;
	for(i=0;i<n;i++)
		cin>>str[i];
	for(i=0;i<n;i++)
	{
		for(j=0;j<m;j++)
		{

			if(str[i][j]=='*')
			{
				s.insert(make_pair(i,j));
				dfs(i,j);
		//		return 0;
				cout<<s.size()%10;
				s.clear();
			}
			else
				cout<<".";
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
int n,m;
char str[1010][1010];
static vector<pair<int,int> >v;
vector<pair<int,int> >:: iterator it;
int viv[1001][1001];
static bool vis[1001][1001];
int finaly[1000010]={0};
int arr[1000010]={0};
inline int get_id(int x,int y,int cnt,int id)
{
    if(x<0 || y<0)
        return cnt;
    if(x>=n || y>=m)
        return cnt;
    vis[x][y]=true;
    if(str[x][y]=='*')
        return cnt;
    cnt++;
    viv[x][y]=id;
    if(!vis[x+1][y] && str[x+1][y]=='.')
        cnt=get_id(x+1,y,cnt,id);
    if(!vis[x-1][y] && str[x-1][y]=='.')
        cnt=get_id(x-1,y,cnt,id);
    if(!vis[x][y+1] && str[x][y+1]=='.')
        cnt=get_id(x,y+1,cnt,id);
    if(!vis[x][y-1] && str[x][y-1]=='.')
        cnt=get_id(x,y-1,cnt,id);
    return cnt;
}
int main()
{
    fast;
    int i,j,id=0;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
        scanf("%s",str[i]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            int ans=0;
            id++;
            if(str[i][j]=='.' && !vis[i][j])
            {
                ans=get_id(i,j,0,id);
                finaly[id]=ans;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                int sum=0,k=0;
                int ojha[4]={0};
                if(i>0)
                if(arr[viv[i-1][j]]==0)
                {
                    sum+=finaly[viv[i-1][j]];
                    arr[viv[i-1][j]]=1;
                    ojha[k++]=viv[i-1][j];
                }
                if(i<n-1)
                if(arr[viv[i+1][j]]==0)
                {
                    sum+=finaly[viv[i+1][j]];
                    arr[viv[i+1][j]]=1;
                    ojha[k++]=viv[i+1][j];
                }
                if(j>0)
                if(arr[viv[i][j-1]]==0)
                {
                    sum+=finaly[viv[i][j-1]];
                    arr[viv[i][j-1]]=1;
                    ojha[k++]=viv[i][j-1];
                }
                if(j<m-1)
                if(arr[viv[i][j+1]]==0)
                {
                    sum+=finaly[viv[i][j+1]];
                    arr[viv[i][j+1]]=1;
                    ojha[k++]=viv[i][j+1];
                }
                printf("%d",(sum+1)%10);
                for(int jj=0;jj<4;jj++)
                {
                    arr[ojha[jj]]=0;
                }
                continue;
            }
            printf("%c",str[i][j]);
        }
        printf("\n");
    }
}


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

static struct IO {
	char tmp[1 << 10];

	// fast input routines
	char cur;

//#define nextChar() (cur = getc_unlocked(stdin))
//#define peekChar() (cur)
	inline char nextChar() { return cur = getc_unlocked(stdin); }
	inline char peekChar() { return cur; }

	inline operator bool() { return peekChar(); }
	inline static bool isBlank(char c) { return (c < '-' && c); }
	inline bool skipBlanks() { while (isBlank(nextChar())); return peekChar() != 0; }

	inline IO& operator >> (char & c) { c = nextChar(); return *this; }

	inline IO& operator >> (char * buf) {
		if (skipBlanks()) {
			if (peekChar()) {
				*(buf++) = peekChar();
				while (!isBlank(nextChar())) *(buf++) = peekChar();
			} *(buf++) = 0; } return *this; }

	inline IO& operator >> (string & s) {
		if (skipBlanks()) {	s.clear(); s += peekChar();
			while (!isBlank(nextChar())) s += peekChar(); }
		return *this; }

	inline IO& operator >> (double & d) { if ((*this) >> tmp) sscanf(tmp, "%lf", &d); return *this;	}

#define defineInFor(intType) \
	inline IO& operator >>(intType & n) { \
		if (skipBlanks()) { \
			int sign = +1; \
			if (peekChar() == '-') { \
				sign = -1; \
				n = nextChar() - '0'; \
			} else \
				n = peekChar() - '0'; \
			while (!isBlank(nextChar())) { \
				n += n + (n << 3) + peekChar() - 48; \
			} \
			n *= sign; \
		} \
		return *this; \
	}

defineInFor(int)
defineInFor(unsigned int)
defineInFor(long long)

	// fast output routines

//#define putChar(c) putc_unlocked((c), stdout)
	inline void putChar(char c) { putc_unlocked(c, stdout); }
	inline IO& operator << (char c) { putChar(c); return *this; }
	inline IO& operator << (const char * s) { while (*s) putChar(*s++); return *this; }

	inline IO& operator << (const string & s) { for (int i = 0; i < (int)s.size(); ++i) putChar(s[i]); return *this; }

	char * toString(double d) { sprintf(tmp, "%lf%c", d, '\0'); return tmp; }
	inline IO& operator << (double d) { return (*this) << toString(d); }

#define defineOutFor(intType) \
	inline char * toString(intType n) { \
		char * p = (tmp + 30); \
		if (n) { \
			bool isNeg = 0; \
			if (n < 0) isNeg = 1, n = -n; \
			while (n) \
				*--p = (n % 10) + '0', n /= 10; \
			if (isNeg) *--p = '-'; \
		} else *--p = '0'; \
		return p; \
	} \
	inline IO& operator << (intType n) { return (*this) << toString(n); }

defineOutFor(int)
defineOutFor(long long)

#define endl ('\n')
#define cout __io__
#define cin __io__
} __io__;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
typedef long long ll;
static int counter=1;
bool vis[1010][1010]={false};
ll maintain[1010][1010]={0};
ll k=0,n,m,vivek[100010]={0};
char str[1010][1010];
void dfs(ll x,ll y)
{
 //   cout<<x<<" "<<y<<endl;
    if(vis[x][y])
        return;
    if(x<0 || x>=n ||y<0 || y>=m)
        return;
    vis[x][y]=true;
    maintain[x][y]=k;
    //if(str[x+1][y]!='*')
    //cout<<"here"<<endl;
    //cout<<str[x][y]<<endl;
//    cout<<vis[x+1][y];
    if(x<n-1)
    if(vis[x+1][y]==false && str[x+1][y]=='.')
    {
    //    cout<<"here1";
        counter++;
        dfs(x+1,y);
    }
    //cout<<"poooo"<<endl;
    //cout<<str[x-1][y]<<endl;
    if(x>0)
    if(!vis[x-1][y] && str[x-1][y]=='.')
    {
      //  cout<<"here1";
        counter++;
        dfs(x-1,y);
    }
    //cout<<str[x][y+1]<<endl;
    if(y<m-1)
    if(!vis[x][y+1] && str[x][y+1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y+1);
    }
    if(y>0)
    if(!vis[x][y-1] && str[x][y-1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y-1);
    }
}
int main()
{
    fast;
    ///string str[10001];
    scanf("%lld",&n);
    scanf("%lld",&m);
    for(ll i=0;i<n;i++)
        scanf("%s",str[i]);
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                continue;
            else if(!vis[i][j])
            {
           //     cout<<"here"<<endl;
                k++;
                dfs(i,j);
             //    cout<<counter<<endl<<endl<<endl;
                vivek[k]=counter;
                counter=1;
            }
        }

    }
 //    cout<<"done"<<endl;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                bool had_hai_bhai[100001]={false};
               ll  ans=0;
               if(i<n-1)
                if(str[i+1][j]=='.' && i<n-1)
                {
                    if(!had_hai_bhai[maintain[i+1][j]])
                    {
                        ans+=vivek[maintain[i+1][j]];
                        had_hai_bhai[maintain[i+1][j]]=true;
                    }
                }
                    if(i>0)
                if(str[i-1][j]=='.' && i>0)
                {
                    if(!had_hai_bhai[maintain[i-1][j]])
                    {
                    ans+=vivek[maintain[i-1][j]];
                     had_hai_bhai[maintain[i-1][j]]=true;
                    }
                }
                if(j<m-1)
                if(str[i][j+1]=='.' && j<m)
                {
                    if(!had_hai_bhai[maintain[i][j+1]])
                    {
                        ans+=vivek[maintain[i][j+1]];
                        had_hai_bhai[maintain[i][j+1]]=true;
                    }
                }
                    if(j>0)
                if(str[i][j-1]=='.' && j>0)
                {
                    if(!had_hai_bhai[maintain[i][j-1]])
                    {
                        ans+=vivek[maintain[i][j-1]];
                        had_hai_bhai[maintain[i][j-1]]=true;
                    }
                }
                printf("%lld",(ans+1)%10);
                //cout<<(ans+1)%10;
            }
            else
                printf(".");
               // cout<<".";
        }
        printf("\n");
       // cout<<endl;
    }
    return 0;
}


// Surprised ???
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
int n,m;
char str[1010][1010];
static vector<pair<int,int> >v;
vector<pair<int,int> >:: iterator it;
int viv[1001][1001];
static bool vis[1001][1001];
int finaly[1000010]={0};
inline int get_id(int x,int y,int cnt,int id)
{
    if(x<0 || y<0)
        return cnt;
    if(x>=n || y>=m)
        return cnt;
    vis[x][y]=true;
    if(str[x][y]=='*')
        return cnt;
    cnt++;
    viv[x][y]=id;
    if(!vis[x+1][y] && str[x+1][y]=='.')
        cnt=get_id(x+1,y,cnt,id);
    if(!vis[x-1][y] && str[x-1][y]=='.')
        cnt=get_id(x-1,y,cnt,id);
    if(!vis[x][y+1] && str[x][y+1]=='.')
        cnt=get_id(x,y+1,cnt,id);
    if(!vis[x][y-1] && str[x][y-1]=='.')
        cnt=get_id(x,y-1,cnt,id);
    return cnt;
}
int main()
{
    fast;
    int i,j,id=0;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
        scanf("%s",str[i]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            int ans=0;
            id++;
            if(str[i][j]=='.' && !vis[i][j])
            {
                ans=get_id(i,j,0,id);
                finaly[id]=ans;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                int sum=0;
                int arr[100000]={0};
                if(arr[viv[i-1][j]]==0)
                {
                    sum+=finaly[viv[i-1][j]];
                    arr[viv[i-1][j]]=1;
                }
                if(arr[viv[i+1][j]]==0)
                {
                    sum+=finaly[viv[i+1][j]];
                    arr[viv[i+1][j]]=1;
                }
                if(arr[viv[i][j-1]]==0)
                {
                    sum+=finaly[viv[i][j-1]];
                    arr[viv[i][j-1]]=1;
                }
                if(arr[viv[i][j+1]]==0)
                {
                    sum+=finaly[viv[i][j+1]];
                    arr[viv[i][j+1]]=1;
                }
                printf("%d",(sum+1)%10);
                continue;
            }
            printf("%c",str[i][j]);
        }
        printf("\n");
    }
}


// hard times :/
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
int n,m;
char str[1010][1010];
int viv[1001][1001];
static bool vis[1001][1001];
int finaly[1000010]={0};
int arr[1000010]={0};
inline int get_id(int x,int y,int cnt,int id)
{
    if(x<0 || y<0)
        return cnt;
    if(x>=n || y>=m)
        return cnt;
    vis[x][y]=true;
    if(str[x][y]=='*')
        return cnt;
    cnt++;
    viv[x][y]=id;
    if(!vis[x+1][y] && str[x+1][y]=='.')
        cnt=get_id(x+1,y,cnt,id);
    if(!vis[x-1][y] && str[x-1][y]=='.')
        cnt=get_id(x-1,y,cnt,id);
    if(!vis[x][y+1] && str[x][y+1]=='.')
        cnt=get_id(x,y+1,cnt,id);
    if(!vis[x][y-1] && str[x][y-1]=='.')
        cnt=get_id(x,y-1,cnt,id);
    return cnt;
}
int main()
{
    fast;
    int i,j,id=0;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
        scanf("%s",str[i]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            int ans=0;
            id++;
            if(str[i][j]=='.' && !vis[i][j])
            {
                ans=get_id(i,j,0,id);
                finaly[id]=ans;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                int sum=0,k=0;
                int ojha[4]={0};
                if(i>0)
                if(arr[viv[i-1][j]]==0)
                {
                    sum+=finaly[viv[i-1][j]];
                    arr[viv[i-1][j]]=1;
                    ojha[k++]=viv[i-1][j];
                }
                if(i<n-1)
                if(arr[viv[i+1][j]]==0)
                {
                    sum+=finaly[viv[i+1][j]];
                    arr[viv[i+1][j]]=1;
                    ojha[k++]=viv[i+1][j];
                }
                if(j>0)
                if(arr[viv[i][j-1]]==0)
                {
                    sum+=finaly[viv[i][j-1]];
                    arr[viv[i][j-1]]=1;
                    ojha[k++]=viv[i][j-1];
                }
                if(j<m-1)
                if(arr[viv[i][j+1]]==0)
                {
                    sum+=finaly[viv[i][j+1]];
                    arr[viv[i][j+1]]=1;
                    ojha[k++]=viv[i][j+1];
                }
                printf("%d",(sum+1)%10);
                for(int jj=0;jj<4;jj++)
                {
                    arr[ojha[jj]]=0;
                }
                continue;
            }
            printf("%c",str[i][j]);
        }
        printf("\n");
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
static int counter=1;
bool vis[1010][1010]={false};
ll maintain[1010][1010]={0};
ll k=0,n,m,vivek[100010]={0};
string str[100010];
void dfs(ll x,ll y)
{
 //   cout<<x<<" "<<y<<endl;
    if(vis[x][y])
        return;
    if(x<0 || x>=n ||y<0 || y>=m)
        return;
    vis[x][y]=true;
    maintain[x][y]=k;
    //if(str[x+1][y]!='*')
    //cout<<"here"<<endl;
    //cout<<str[x][y]<<endl;
//    cout<<vis[x+1][y];
    if(x<n-1)
    if(vis[x+1][y]==false && str[x+1][y]=='.')
    {
    //    cout<<"here1";
        counter++;
        dfs(x+1,y);
    }
    //cout<<"poooo"<<endl;
    //cout<<str[x-1][y]<<endl;
    if(x>0)
    if(!vis[x-1][y] && str[x-1][y]=='.')
    {
      //  cout<<"here1";
        counter++;
        dfs(x-1,y);
    }
    //cout<<str[x][y+1]<<endl;
    if(y<m-1)
    if(!vis[x][y+1] && str[x][y+1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y+1);
    }
    if(y>0)
    if(!vis[x][y-1] && str[x][y-1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y-1);
    }
}
int main()
{
    ///string str[10001];
    cin>>n;
    cin>>m;
    for(ll i=0;i<n;i++)
        cin>>str[i];
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                continue;
            else if(!vis[i][j])
            {
                k++;
                dfs(i,j);
                // cout<<"done"<<endl;
                vivek[k]=counter;
                counter=1;
            }
        }

    }
 //    cout<<"done"<<endl;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                bool had_hai_bhai[100001]={false};
               ll  ans=0;
               if(i<n-1)
                if(str[i+1][j]=='.' && i<n-1)
                {
                    if(!had_hai_bhai[maintain[i+1][j]])
                    {
                        ans+=vivek[maintain[i+1][j]];
                        had_hai_bhai[maintain[i+1][j]]=true;
                    }
                }
                    if(i>0)
                if(str[i-1][j]=='.' && i>0)
                {
                    if(!had_hai_bhai[maintain[i-1][j]])
                    {
                    ans+=vivek[maintain[i-1][j]];
                     had_hai_bhai[maintain[i-1][j]]=true;
                    }
                }
                if(j<m-1)
                if(str[i][j+1]=='.' && j<m)
                {
                    if(!had_hai_bhai[maintain[i][j+1]])
                    {
                        ans+=vivek[maintain[i][j+1]];
                        had_hai_bhai[maintain[i][j+1]]=true;
                    }
                }
                    if(j>0)
                if(str[i][j-1]=='.' && j>0)
                {
                    if(!had_hai_bhai[maintain[i][j-1]])
                    {
                        ans+=vivek[maintain[i][j-1]];
                        had_hai_bhai[maintain[i][j-1]]=true;
                    }
                }
                cout<<(ans+1)%10;
            }
            else
                cout<<".";
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
typedef long long ll;
static int counter=1;
bool vis[1010][1010]={false};
ll maintain[1010][1010]={0};
ll k=0,n,m,vivek[100010]={0};
char str[1010][1010];
void dfs(ll x,ll y)
{
 //   cout<<x<<" "<<y<<endl;
    if(vis[x][y])
        return;
    if(x<0 || x>=n ||y<0 || y>=m)
        return;
    vis[x][y]=true;
    maintain[x][y]=k;
    //if(str[x+1][y]!='*')
    //cout<<"here"<<endl;
    //cout<<str[x][y]<<endl;
//    cout<<vis[x+1][y];
    if(x<n-1)
    if(vis[x+1][y]==false && str[x+1][y]=='.')
    {
    //    cout<<"here1";
        counter++;
        dfs(x+1,y);
    }
    //cout<<"poooo"<<endl;
    //cout<<str[x-1][y]<<endl;
    if(x>0)
    if(!vis[x-1][y] && str[x-1][y]=='.')
    {
      //  cout<<"here1";
        counter++;
        dfs(x-1,y);
    }
    //cout<<str[x][y+1]<<endl;
    if(y<m-1)
    if(!vis[x][y+1] && str[x][y+1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y+1);
    }
    if(y>0)
    if(!vis[x][y-1] && str[x][y-1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y-1);
    }
}
int main()
{
    fast;
    ///string str[10001];
    scanf("%lld",&n);
    scanf("%lld",&m);
    for(ll i=0;i<n;i++)
        scanf("%s",str[i]);
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                continue;
            else if(!vis[i][j])
            {
           //     cout<<"here"<<endl;
                k++;
                dfs(i,j);
             //    cout<<counter<<endl<<endl<<endl;
                vivek[k]=counter;
                counter=1;
            }
        }

    }
 //    cout<<"done"<<endl;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                bool had_hai_bhai[100001]={false};
               ll  ans=0;
               if(i<n-1)
                if(str[i+1][j]=='.' && i<n-1)
                {
                    if(!had_hai_bhai[maintain[i+1][j]])
                    {
                        ans+=vivek[maintain[i+1][j]];
                        had_hai_bhai[maintain[i+1][j]]=true;
                    }
                }
                    if(i>0)
                if(str[i-1][j]=='.' && i>0)
                {
                    if(!had_hai_bhai[maintain[i-1][j]])
                    {
                    ans+=vivek[maintain[i-1][j]];
                     had_hai_bhai[maintain[i-1][j]]=true;
                    }
                }
                if(j<m-1)
                if(str[i][j+1]=='.' && j<m)
                {
                    if(!had_hai_bhai[maintain[i][j+1]])
                    {
                        ans+=vivek[maintain[i][j+1]];
                        had_hai_bhai[maintain[i][j+1]]=true;
                    }
                }
                    if(j>0)
                if(str[i][j-1]=='.' && j>0)
                {
                    if(!had_hai_bhai[maintain[i][j-1]])
                    {
                        ans+=vivek[maintain[i][j-1]];
                        had_hai_bhai[maintain[i][j-1]]=true;
                    }
                }
                printf("%lld",(ans+1)%10);
                //cout<<(ans+1)%10;
            }
            else
                printf(".");
               // cout<<".";
        }
        printf("\n");
       // cout<<endl;
    }
    return 0;
}


// ???
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
int n,m;
char str[1010][1010];
static vector<pair<int,int> >v;
vector<pair<int,int> >:: iterator it;
int viv[1001][1001];
static bool vis[1001][1001];
int finaly[1000010]={0};
inline int get_id(int x,int y,int cnt,int id)
{
    if(x<0 || y<0)
        return cnt;
    if(x>=n || y>=m)
        return cnt;
    vis[x][y]=true;
    if(str[x][y]=='*')
        return cnt;
    cnt++;
    viv[x][y]=id;
    if(!vis[x+1][y] && str[x+1][y]=='.')
        cnt=get_id(x+1,y,cnt,id);
    if(!vis[x-1][y] && str[x-1][y]=='.')
        cnt=get_id(x-1,y,cnt,id);
    if(!vis[x][y+1] && str[x][y+1]=='.')
        cnt=get_id(x,y+1,cnt,id);
    if(!vis[x][y-1] && str[x][y-1]=='.')
        cnt=get_id(x,y-1,cnt,id);
    return cnt;
}
int main()
{
    fast;
    int i,j,id=0;
    scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
        scanf("%s",str[i]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            int ans=0;
            id++;
            if(str[i][j]=='.' && !vis[i][j])
            {
                ans=get_id(i,j,0,id);
                finaly[id]=ans;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                int sum=0;
                int arr[100000]={0};
                if(i>0)
                if(arr[viv[i-1][j]]==0)
                {
                    sum+=finaly[viv[i-1][j]];
                    arr[viv[i-1][j]]=1;
                }
                if(i<n-1)
                if(arr[viv[i+1][j]]==0)
                {
                    sum+=finaly[viv[i+1][j]];
                    arr[viv[i+1][j]]=1;
                }
                if(j>0)
                if(arr[viv[i][j-1]]==0)
                {
                    sum+=finaly[viv[i][j-1]];
                    arr[viv[i][j-1]]=1;
                }
                if(j<m-1)
                if(arr[viv[i][j+1]]==0)
                {
                    sum+=finaly[viv[i][j+1]];
                    arr[viv[i][j+1]]=1;
                }
                printf("%d",(sum+1)%10);
                continue;
            }
            printf("%c",str[i][j]);
        }
        printf("\n");
    }
}


#include<bits/stdc++.h>
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int n,m,ans;
char str[1002][1002];
set<pair<int,int> >s;
void dfs(int i,int j)
{
    int vivek,ojha;
    for(int k=0;k<4;k++)
    {
        vivek=i+dy[k];
        ojha=j+dx[k];

        if(vivek<0 || vivek>=n || ojha<0 || ojha>=m)continue;

        if(str[vivek][ojha]=='*')continue;
        //cout<<vivek<<" "<<ojha<<" "<<n<<" "<<m<<endl;
        if(!(binary_search(s.begin(),s.end(),make_pair(vivek,ojha))))
        {
            //cout<<"int";
            s.insert(make_pair(vivek,ojha));
            dfs(vivek,ojha);
        //  if(i==0 && j==0)
        //  cout<<ans<<endl;
        }
    }
//  return s.size();
}
int main()
{
    cin>>n;
    cin>>m;
    int i,j;
    for(i=0;i<n;i++)
        scanf("%s",str[i]);
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {

            if(str[i][j]=='*')
            {
                s.insert(make_pair(i,j));
                dfs(i,j);
        //      return 0;
                printf("%d",s.size()%10);
                s.clear();
            }
            else
                printf(".");
        }
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
static int counter=1;
bool vis[1010][1010]={false};
ll maintain[1010][1010]={0};
ll k=0,n,m,vivek[100010]={0};
string str[100010];
void dfs(ll x,ll y)
{
 //   cout<<x<<" "<<y<<endl;
    if(vis[x][y])
        return;
    if(x<0 || x>=n ||y<0 || y>=m)
        return;
    vis[x][y]=true;
    maintain[x][y]=k;
    //if(str[x+1][y]!='*')
    //cout<<"here"<<endl;
    //cout<<str[x][y]<<endl;
//    cout<<vis[x+1][y];
    if(x<n-1)
    if(vis[x+1][y]==false && str[x+1][y]=='.')
    {
    //    cout<<"here1";
        counter++;
        dfs(x+1,y);
    }
    //cout<<"poooo"<<endl;
    //cout<<str[x-1][y]<<endl;
    if(x>0)
    if(!vis[x-1][y] && str[x-1][y]=='.')
    {
      //  cout<<"here1";
        counter++;
        dfs(x-1,y);
    }
    //cout<<str[x][y+1]<<endl;
    if(y<m-1)
    if(!vis[x][y+1] && str[x][y+1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y+1);
    }
    if(y>0)
    if(!vis[x][y-1] && str[x][y-1]==str[x][y])
    {
        //cout<<"here1";
        counter++;
        dfs(x,y-1);
    }
}
int main()
{
    ///string str[10001];
    cin>>n;
    cin>>m;
    for(ll i=0;i<n;i++)
        cin>>str[i];
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
                continue;
            else if(!vis[i][j])
            {
           //     cout<<"here"<<endl;
                k++;
                dfs(i,j);
             //    cout<<counter<<endl<<endl<<endl;
                vivek[k]=counter;
                counter=1;
            }
        }

    }
 //    cout<<"done"<<endl;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(str[i][j]=='*')
            {
                bool had_hai_bhai[100001]={false};
               ll  ans=0;
               if(i<n-1)
                if(str[i+1][j]=='.' && i<n-1)
                {
                    if(!had_hai_bhai[maintain[i+1][j]])
                    {
                        ans+=vivek[maintain[i+1][j]];
                        had_hai_bhai[maintain[i+1][j]]=true;
                    }
                }
                    if(i>0)
                if(str[i-1][j]=='.' && i>0)
                {
                    if(!had_hai_bhai[maintain[i-1][j]])
                    {
                    ans+=vivek[maintain[i-1][j]];
                     had_hai_bhai[maintain[i-1][j]]=true;
                    }
                }
                if(j<m-1)
                if(str[i][j+1]=='.' && j<m)
                {
                    if(!had_hai_bhai[maintain[i][j+1]])
                    {
                        ans+=vivek[maintain[i][j+1]];
                        had_hai_bhai[maintain[i][j+1]]=true;
                    }
                }
                    if(j>0)
                if(str[i][j-1]=='.' && j>0)
                {
                    if(!had_hai_bhai[maintain[i][j-1]])
                    {
                        ans+=vivek[maintain[i][j-1]];
                        had_hai_bhai[maintain[i][j-1]]=true;
                    }
                }
                cout<<(ans+1)%10;
            }
            else
                cout<<".";
        }
        cout<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int n,m,ans;
string str[100010];
set<pair<int,int> >s;
void dfs(int i,int j)
{
    int vivek,ojha;
    for(int k=0;k<4;k++)
    {
        vivek=i+dy[k];
        ojha=j+dx[k];

        if(vivek<0 || vivek>=n || ojha<0 || ojha>=m)continue;

        if(str[vivek][ojha]=='*')continue;
        //cout<<vivek<<" "<<ojha<<" "<<n<<" "<<m<<endl;
        if(!(binary_search(s.begin(),s.end(),make_pair(vivek,ojha))))
        {
            //cout<<"int";
            s.insert(make_pair(vivek,ojha));
            dfs(vivek,ojha);
        //  if(i==0 && j==0)
        //  cout<<ans<<endl;
        }
    }
//  return s.size();
}
int main()
{
    cin>>n;
    cin>>m;
    int i,j;
    for(i=0;i<n;i++)
        cin>>str[i];
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {

            if(str[i][j]=='*')
            {
                s.insert(make_pair(i,j));
                dfs(i,j);
        //      return 0;
                cout<<s.size()%10;
                s.clear();
            }
            else
                cout<<".";
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        forn(j, m)
            cin >> a[i][j];
    forn(i, n)
        forn(j, m)
            if (a[i][j] == '.')
            {
                ans[i][j] = '.';
            }
            else
            {
                k++;
                was[i][j] = k;
                ans[i][j] = ((bfs(i, j) + 1) % 10) + '0';
            }
    forn(i, n)
    {
        /*forn(j, m)
            cout << ans[i][j];
        cout << LN;*/
        printf("%s\n", ans[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);

/*
    forn(i, n)
    {
        forn(j, m)
            cout << ans[i][j];
        cout << LN;
        printf("%s\n", a[i]);
    }
    cout << LN;

*/
    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                cout << ((bfs(i, j) + 1) % 10);
            }
        cout << LN;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf(" %s", a[i]);

    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                cout << ((bfs(i, j) + 1) % 10);
            }
        cout << LN;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);

    forn(i, n)
    {
        forn(j, m)
            cout << a[i][j];
        cout << LN;
    }

   /* forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                cout << ((bfs(i, j) + 1) % 10);
            }
        cout << LN;
    }*/
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
  /*  ios_base::sync_with_stdio(0);
    cin.tie(NULL);  */
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);

    forn(i, n)
    {
        forn(j, m)
            cout << a[i][j];
        cout << LN;
    }

   /* forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                cout << ((bfs(i, j) + 1) % 10);
            }
        cout << LN;
    }*/
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 1, kk = 1;
char a[1007][1007];
int t[1000007];
int was[1007][1007];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

void bfs(int x,int y)
{
    kk++;
    was[x][y] = k;
    forn(i, 4)
    if (x + dx[i] > -1 && x + dx[i] < n && y + dy[i] > -1 && y + dy[i] < m && a[x + dx[i]][y + dy[i]] != '*' && was[x + dx[i]][y + dy[i]] == 0)
    {
        bfs(x + dx[i], y + dy[i]);
    }
}

int main()
{
  /*  ios_base::sync_with_stdio(0);
    cin.tie(NULL);  */
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (was[i][j] == 0 && a[i][j] != '*')
            {
                kk = 0;
                bfs(i, j);
                t[k] = kk;
                k++;
            }
    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                printf("%c", a[i][j]);
            }
            else
            {
                set<int> ss;
                int u = 1;
                if (i - 1 >= 0)
                {
                    u += t[was[i-1][j]];
                    ss.in(was[i-1][j]);
                }
                if (i + 1 < n)
                {
                    int y = ss.size();
                    ss.in(was[i+1][j]);
                    if (ss.size() != y)
                        u += t[was[i+1][j]];
                }
                if (j - 1 >= 0)
                {
                    int y = ss.size();
                    ss.in(was[i][j-1]);
                    if (ss.size() != y)
                        u += t[was[i][j-1]];
                }
                if (j + 1 < m)
                {

                    int y = ss.size();
                    ss.in(was[i][j+1]);
                    if (ss.size() != y)
                        u += t[was[i][j+1]];
                }
                printf("%d", u % 10);
            }
        printf("\n");
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (a[i][j] == '.')
            {
                ans[i][j] = '.';
            }
            else
            {
                k++;
                was[i][j] = k;
                ans[i][j] = ((bfs(i, j) + 1) % 10) + '0';
            }
    forn(i, n)
    {
        forn(j, m)
            cout << ans[i][j];
        cout << LN;
        //printf("%s\n", ans[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 1, kk = 1;
char a[1007][1007];
int t[1000007];
int was[1007][1007];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

void bfs(int x,int y)
{
    kk++;
    was[x][y] = k;
    forn(i, 4)
    if (x + dx[i] > -1 && x + dx[i] < n && y + dy[i] > -1 && y + dy[i] < m && a[x + dx[i]][y + dy[i]] != '*' && was[x + dx[i]][y + dy[i]] == 0)
    {
        bfs(x + dx[i], y + dy[i]);
    }
}

int main()
{
  /*  ios_base::sync_with_stdio(0);
    cin.tie(NULL);  */
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (was[i][j] == 0 && a[i][j] != '*')
            {
                kk = 0;
                bfs(i, j);
                t[k] = kk;
                k++;
            }

    /*forn(i, n)
    {
        forn(j, m)
            cout <<was[i][j];
        cout << LN;
    }
*/
    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << a[i][j];
            }
            else
            {
                set<int> ss;
                int u = 1;
                if (i - 1 >= 0)
                {
                    u += t[was[i-1][j]];
                    ss.in(was[i-1][j]);
                }
                if (i + 1 < n)
                {
                    int y = ss.size();
                    ss.in(was[i+1][j]);
                    if (ss.size() != y)
                        u += t[was[i+1][j]];
                }
                if (j - 1 >= 0)
                {
                    int y = ss.size();
                    ss.in(was[i][j-1]);
                    if (ss.size() != y)
                        u += t[was[i][j-1]];
                }
                if (j + 1 < m)
                {

                    int y = ss.size();
                    ss.in(was[i][j+1]);
                    if (ss.size() != y)
                        u += t[was[i][j+1]];
                }
                cout << u % 10;
            }
        cout << LN;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (a[i][j] == '.')
            {
                ans[i][j] = '.';
            }
            else
            {
                k++;
                was[i][j] = k;
                ans[i][j] = ((bfs(i, j) + 1) % 10) + '0';
            }
    forn(i, n)
    {
        forn(j, m)
            cout << a[i][j];
        cout << LN;
        //printf("%s\n", ans[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        forn(j, m)
            cin >> a[i][j];

/*
    forn(i, n)
    {
        forn(j, m)
            cout << ans[i][j];
        cout << LN;
        printf("%s\n", a[i]);
    }
    cout << LN;

*/
    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                cout << ((bfs(i, j) + 1) % 10);
            }
        cout << LN;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
  /*  ios_base::sync_with_stdio(0);
    cin.tie(NULL);  */
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);

    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                printf("%d", ((bfs(i, j) + 1) % 10));
            }
        printf("\n");
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 1, kk = 1;
char a[1007][1007];
int t[1000007];
int was[1007][1007];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};

void bfs(int x,int y)
{
    kk++;
    was[x][y] = k;
    forn(i, 4)
    if (x + dx[i] > -1 && x + dx[i] < n && y + dy[i] > -1 && y + dy[i] < m && a[x + dx[i]][y + dy[i]] != '*' && was[x + dx[i]][y + dy[i]] == 0)
    {
        bfs(x + dx[i], y + dy[i]);
    }
}

int main()
{
  /*  ios_base::sync_with_stdio(0);
    cin.tie(NULL);  */
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (was[i][j] == 0 && a[i][j] != '*')
            {
                kk = 0;
                bfs(i, j);
                t[k] = kk;
                k++;
            }
    forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                printf("%c", a[i][j]);
            }
            else
            {
                set<int> ss;
                int u = 1;
                if (i - 1 >= 0)
                {
                    u += t[was[i-1][j]];
                    ss.in(was[i-1][j]);
                }
                if (i + 1 < n)
                {
                    int y = ss.size();
                    ss.in(was[i+1][j]);
                    if (ss.size() != y)
                        u += t[was[i+1][j]];
                }
                if (j - 1 >= 0)
                {
                    int y = ss.size();
                    ss.in(was[i][j-1]);
                    if (ss.size() != y)
                        u += t[was[i][j-1]];
                }
                if (j + 1 < m)
                {

                    int y = ss.size();
                    ss.in(was[i][j+1]);
                    if (ss.size() != y)
                        u += t[was[i][j+1]];
                }
                printf("%c", u % 10);
            }
        printf("\n");
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
    {
        scanf("%s", a[i]);
    }

/*
    forn(i, n)
    {
        forn(j, m)
            cout << ans[i][j];
        cout << LN;
        printf("%s\n", a[i]);
    }
    cout << LN;

*/
    forn(i, n)
        forn(j, m)
            if (a[i][j] == '.')
            {
                ans[i][j] = '.';
            }
            else
            {
                k++;
                was[i][j] = k;
                ans[i][j] = ((bfs(i, j) + 1) % 10) + '0';
            }
    forn(i, n)
    {
        /*forn(j, m)
            cout << ans[i][j];
        cout << LN;*/
        printf("%s\n", ans[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (a[i][j] == '.')
            {
                ans[i][j] = '.';
            }
            else
            {
                k++;
                was[i][j] = k;
                ans[i][j] = ((bfs(i, j) + 1) % 10) + '0';
            }
    forn(i, n)
    {
        printf("%s\n", ans[i]);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf(" %s", a[i]);

    forn(i, n)
    {
        forn(j, m)
            cout << a[i][j];
        cout << LN;
    }

   /* forn(i, n)
    {
        forn(j, m)
            if (a[i][j] == '.')
            {
                cout << ".";
            }
            else
            {
                k++;
                was[i][j] = k;
                cout << ((bfs(i, j) + 1) % 10);
            }
        cout << LN;
    }*/
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define forn(i, n) for (int i = 0; i < n; i++)
#define form(i, n, m) for (int i = n; i <= m; i++)
//
#define read(x) scanf("%d", &x)
#define write(x) printf("%d", &x)
#define read2(x, y) scanf("%d %d", &x, &y)
//
#define sqr(x) x*x
#define mp make_pair
#define pb push_back
#define pob pop_back
#define XX first
#define YY second
#define in insert
#define db long double
#define re return
#define y1 dsfgsdfgsdfgsdfgsdfgsdfg
#define sz size
#define rs resize
#define vec vector
#define all(a) a.begin(), a.end()
#define TASK "sum"   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
const str LN = "\n";
const double eps = 1e-7;
const double pi = acos(-1.0);
const int INF = (int)1e9 + 7;
const ll LINF = (ll)9e18 + 7;
bool in(int k, int x1, int x2) {re (k >= x1 && k <= x2);}

const int MAXN = 100007;

int n, m, k = 0;
char a[1007][1007], ans[1007][1007];
int was[1007][1007];

int bfs(int i, int j)
{
    int kk = 0;
    if (i - 1 >= 0 && a[i-1][j] != '*' && was[i-1][j] != k)
    {
        was[i-1][j] = k;
        kk++;
        kk += bfs(i - 1, j);
    }
    if (j - 1 >= 0 && a[i][j-1] != '*' && was[i][j-1] != k)
    {
        was[i][j-1] = k;
        kk++;
        kk += bfs(i, j - 1);
    }
    if (i + 1 < n && a[i + 1][j] != '*' && was[i+1][j] != k)
    {
        was[i+1][j] = k;
        kk++;
        kk += bfs(i + 1, j);
    }
    if (j + 1 < m && a[i][j + 1] != '*' && was[i][j + 1] != k)
    {
        was[i][j + 1] = k;
        kk++;
        kk += bfs(i, j + 1);
    }
    re kk;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    //freopen("lock.in", "r", stdin); freopen("lock.out", "w", stdout);
    cin >> n >> m;
    forn(i, n)
        scanf("%s", a[i]);
    forn(i, n)
        forn(j, m)
            if (a[i][j] == '.')
            {
                ans[i][j] = '.';
            }
            else
            {
                k++;
                was[i][j] = k;
                ans[i][j] = ((bfs(i, j) + 1) % 10) + '0';
            }
    forn(i, n)
    {
        printf("%s", ans[i]);
        cout << LN;
    }
}