Language: C++

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

int main(){
  int H, W;
  int counter = 0;
  cin >> H >> W;
  String S[H];
  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:8:3: error: 'String' was not declared in this scope
    8 |   String S[H];
      |   ^~~~~~
a.cc:10:12: error: 'S' was not declared in this scope
   10 |     cin >> S[a];
      |            ^
a.cc:13:8: error: 'S' was not declared in this scope
   13 |     if(S[0].at(0) == '#')cout << "#" << endl;
      |        ^
a.cc:21:10: error: 'S' was not declared in this scope
   21 |       if(S[0].at(k) == '#'){
      |          ^
a.cc:42:5: error: expected '}' before 'else'
   42 |     else if(H != 1 && W == 1){
      |     ^~~~
a.cc:18:28: note: to match this '{'
   18 |   else if(H == 1 && W != 1){
      |                            ^
a.cc:45:12: error: 'S' was not declared in this scope
   45 |         if(S[H].at(0) == '#'){
      |            ^
a.cc:71:14: error: 'S' was not declared in this scope
   71 |           if(S[n].at(k) == '#'){
      |              ^
a.cc: At global scope:
a.cc:161:1: error: expected declaration before '}' token
  161 | }
      | ^