Language: C++

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

int main(){
  int H, W;
  int counter = 0;
  cin >> H >> W;
  for(int a = 0;a < H;a++){
    cin >> S[a];
  }
  if(H == 1 && W == 1){
    if(S[0].at(0) == '#')cout << "#" << endl;
    else{
      cout << "0" << endl;
    }
  }
  else if(H == 1 && W != 1){
    for(int k = 0;k < W;k++){
      counter = 0;
      if(S[0].at(k) == '#'){
        cout << "#";
        continue;
      }
      else if(k == 0){
        if(S[0].at(k+1) == '#')counter++;
      cout << counter;
        continue;
      }
      else if(k == W-1){
        if(S[0].at(k-1))counter++;
           cout << counter << endl;
           continue;
      }
      else{
        if(S[0].at(k-1) == '#')counter++;
        if(S[0].at(k+1) == '#')counter++;
        cout << counter;
        continue;
      }
    }
    else if(H != 1 && W == 1){
      for(int n = 0;n < H;n++){
        counter = 0;
        if(S[H].at(0) == '#'){
          cout << "#" << endl;
          continue;
        }
        else if(n == 0){
          if(S[n+1].at(0) == '#')counter++;
          cout << counter << endl;
          continue;
        }
        else if(n == H-1){
          if(S[n-1].at(0) == '#')counter++;
          cout << counter << endl;
          continue;
        }
        else{
          if(S[n-1].at(0) == '#')counter++;
          if(S[n+1].at(0) == '#')counter++;
          cout << counter << endl;
          continue;
        }
      }
    }
    else{
      for(int n = 0;n < H;n++){
        for(int k = 0;k < W;k++){
          counter = 0;
          if(S[n].at(k) == '#'){
            cout << "#";
            continue;
          }
          else if(n == 0){
            if(k == 0){
              if(S[n+1].at(k) == '#')counter++;
              if(S[n].at(k+1) == '#')counter++;
              if(S[n+1].at(k+1) == '#')counter++;
              cout << counter;
              continue;
            }
            else if(k == W-1){
              if(S[n+1].at(k) == '#')counter++;
              if(S[n].at(k-1) == '#')counter++;
              if(S[n+1].at(k-1) == '#')counter++;
              cout << counter << endl;
              continue;
            }
            else{
              if(S[n].at(k-1) == '#')counter++;
              if(S[n].at(k+1) == '#')counter++;
              if(S[n+1].at(k-1) == '#')counter++;
              if(S[n+1].at(k) == '#')counter++;
              if(S[n+1].at(k+1) == '#')counter++;
              cout << counter;
              continue;
            }
          }
          else if(n == H-1){
            if(k == 0){
              if(S[n].at(k+1) == '#')counter++;
              if(S[n-1].at(k) == '#')counter++;
              if(S[n-1].at(k+1) == '#')counter++;
              cout << counter;
              continue;
            }
           else if(k == W-1){
              if(S[n].at(k-1) == '#')counter++;
              if(S[n-1].at(k) == '#')counter++;
              if(S[n-1].at(k-1) == '#')counter++;
              cout << counter;
              continue;
           }
            else{
              if(S[n].at(k-1) == '#')counter++;
              if(S[n].at(k+1) == '#')counter++;
              if(S[n-1].at(k-1) == '#')counter++;
              if(S[n-1].at(k) == '#')counter++;
              if(S[n-1].at(k+1) == '#')counter++;
              cout << counter;
              continue;
            }
          }
          else{
            if(k == 0){
              if(S[n-1].at(k) == '#')counter++;
              if(S[n].at(k+1) == '#')counter++;
              if(S[n+1].at(k+1) == '#')counter++;
              if(S[n+1].at(k) == '#')counter++;
              if(S[n+1].at(k+1) == '#')counter++;
              cout << counter;
              continue;
            }
            else if(k == W-1){
              if(S[n-1].at(k) == '#')counter++;
              if(S[n].at(k-1) == '#')counter++;
              if(S[n-1].at(k-1) == '#')counter++;
              if(S[n+1].at(k) == '#')counter++;
              if(S[n+1].at(k-1) == '#')counter++;
              cout << counter;
              continue;
            }
            else{
              if(S[n-1].at(k-1) == '#')counter++;
              if(S[n-1].at(k) == '#')counter++;
              if(S[n-1].at(k+1) == '#')counter++;
              if(S[n].at(k-1) == '#')counter++;
              if(S[n].at(k+1) == '#')counter++;
              if(S[n+1].at(k+1) == '#')counter++;
              if(S[n+1].at(k) == '#')counter++;
              if(S[n+1].at(k-1) == '#')counter++;
              cout << counter;
              continue;
            }
          }
        }
      }
    }
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:9:12: error: 'S' was not declared in this scope
    9 |     cin >> S[a];
      |            ^
a.cc:12:8: error: 'S' was not declared in this scope
   12 |     if(S[0].at(0) == '#')cout << "#" << endl;
      |        ^
a.cc:20:10: error: 'S' was not declared in this scope
   20 |       if(S[0].at(k) == '#'){
      |          ^
a.cc:41:5: error: expected '}' before 'else'
   41 |     else if(H != 1 && W == 1){
      |     ^~~~
a.cc:17:28: note: to match this '{'
   17 |   else if(H == 1 && W != 1){
      |                            ^
a.cc:44:12: error: 'S' was not declared in this scope
   44 |         if(S[H].at(0) == '#'){
      |            ^
a.cc:70:14: error: 'S' was not declared in this scope
   70 |           if(S[n].at(k) == '#'){
      |              ^
a.cc: At global scope:
a.cc:160:1: error: expected declaration before '}' token
  160 | }
      | ^