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

int main() {
    int H, W;
    int n = 0;
    cin >> H >> W;
    vector<vector<char>> list(H, vector<char>(W));
    for (int i = 0; i < H; i++){
        for (int j = 0; j < W; j++){
            cin >> list.at(i).at(j);
        }
    }
    if (H == 1 || W == 1){
        if (H == 1 && W != 1){
         for(int j = 1; j < W-1; j++){
            if (list.at(0).at(j) == '.'){
              if (list.at(0).at(j-1) == '#')
                n++;
              if (list.at(0).at(j+1) == '#')
                n++;
              char c = '0' + n;
              list.at(0).at(j) = c;
              n = 0;
            }
         }
         if (list.at(0).at(0) == '.'){
            if (list.at(0).at(1) == '#')
            list.at(0).at(0) = '1';
            else
            list.at(0).at(0) = '0';
         }
         if (list.at(0).at(W-1) == '.'){
            if (list.at(0).at(W-2) == '#')
            list.at(0).at(W-1) = '1';
            else
            list.at(0).at(W-1)  = '0';
         }
        }   
        if (H != 1 && W == 1){
         for(int i = 1; i < H-1; i++){
            if (list.at(i).at(0) == '.'){
              if (list.at(i-1).at(0) == '#')
                n++;
              if (list.at(i+1).at(0) == '#')
                n++;
              char c = '0' + n;
              list.at(i).at(0) = c;
              n = 0;
            }            
         }
         if (list.at(0).at(0) == '.'){
            if (list.at(1).at(0) == '#')
            list.at(0).at(0) = '1';
            else
            list.at(0).at(0) = '0';
         }
         if (list.at(H-1).at(0) == '.'){
            if (list.at(H-2).at(0) == '#')
            list.at(H-1).at(0) = '1';
            else
            list.at(H-1).at(0) = '0';
         }
        }
        if (H == 1 && W == 1){
         if (list.at(0).at(0) == '.')
            list.at(0).at(0) = '0';
        }
    }
    else{
        for (int i = 1; i < H-1; i++){
         for (int j = 1; j < W-1; j++){
            if (list.at(i).at(j) == '.'){
                if (list.at(i-1).at(j-1) == '#')
                    n++;
                if (list.at(i).at(j-1) == '#')
                    n++;
                if (list.at(i+1).at(j-1) == '#')
                    n++;
                if (list.at(i-1).at(j) == '#')
                    n++;
                if (list.at(i+1).at(j) == '#')
                    n++;
                if (list.at(i-1).at(j+1) == '#')
                    n++;
                if (list.at(i).at(j+1) == '#')
                    n++;
                if (list.at(i+1).at(j+1) == '#')
                    n++;
                char c = '0' + n;
                list.at(i).at(j) = c;
                n = 0;
            }
         }
        }
        for (int i = 1; i < H-1; i++){
         if (list.at(i).at(0) == '.'){
            if (list.at(i-1).at(0) == '#')
                    n++;
            if (list.at(i+1).at(0) == '#')
                    n++;
            if (list.at(i+1).at(1) == '#')
                    n++;
            if (list.at(i).at(1) == '#')
                    n++;
            if (list.at(i-1).at(1) == '#')
                    n++;
            char c = '0' + n;
            list.at(i).at(0) = c;
            n = 0;
         }
         if (list.at(i).at(W-1) == '.'){
            if (list.at(i-1).at(W-2) == '#')
                    n++;
            if (list.at(i+1).at(W-2) == '#')
                    n++;
            if (list.at(i+1).at(W-1) == '#')
                    n++;
            if (list.at(i).at(W-2) == '#')
                    n++;
            if (list.at(i-1).at(W-1) == '#')
                    n++;
            char c = '0' + n;
            list.at(i).at(W-1) = c;
            n = 0;
         }
        }
        for (int j = 1; j < W-1; j++){
         if (list.at(0).at(j) == '.'){
            if (list.at(0).at(j-1) == '#')
                    n++;
            if (list.at(0).at(j+1) == '#')
                    n++;
            if (list.at(1).at(j-1) == '#')
                    n++;
            if (list.at(1).at(j) == '#')
                    n++;
            if (list.at(1).at(j+1) == '#')
                    n++;
            char c = '0' + n;
            list.at(0).at(j) = c;
            n = 0;
         }
         if (list.at(H-1).at(j) == '.'){
            if (list.at(H-1).at(j-1) == '#')
                    n++;
            if (list.at(H-2).at(j) == '#')
                    n++;
            if (list.at(H-2).at(j-1) == '#')
                    n++;
            if (list.at(H-2).at(j+1) == '#')
                    n++;
            if (list.at(H-1).at(j+1) == '#')
                    n++;
            char c = '0' + n;
            list.at(H-1).at(j) = c;
            n = 0;
         }
        }
        if (list.at(0).at(0) == '.'){
          if (list.at(0).at(1) == '#')
            n++;
          if (list.at(1).at(0) == '#')
            n++;
          if (list.at(1).at(1) == '#')
            n++;
          char c = '0' + n;
          list.at(0).at(0) = c;
          n = 0;
        }
        if (list.at(0).at(W-1) == '.'){
          if (list.at(0).at(W-2) == '#')
            n++;
          if (list.at(1).at(W-1) == '#')
            n++;
          if (list.at(1).at(W-2) == '#')
            n++;
          char c = '0' + n;
          list.at(0).at(W-1) = c;
          n = 0;
        }
        if (list.at(H-1).at(W-1) == '.'){
          if (list.at(H-2).at(W-1) == '#')
            n++;
          if (list.at(H-2).at(W-2) == '#')
            n++;
          if (list.at(H-1).at(W-2) == '#')
            n++;
          char c = '0' + n;
          list.at(H-1).at(W-1) = c;
          n = 0;
        }
        if (list.at(H-1).at(0) == '.'){
          if (list.at(H-2).at(1) == '#')
            n++;
          if (list.at(H-2).at(0) == '#')
            n++;
          if (list.at(H-1).at(1) == '#')
            n++;
          char c = '0' + n;
          list.at(H-1).at(0) = c;
          n = 0;
        }
    }
    for (int i = 0; i < H; i++){
        for (int j = 0; j < W; j++){
            cout << list.at(i).at(j);
        }
        cout << endl;
    }
}

Measured memory_bytes: 256