Language: C++

#include <bits/stdc++.h>
using namespace std;
int main() {
  //入力
  int H, W;
  cin >> H >> W;
  //一つの文字列に
  string s;
  for(int i = 0; i < H; i++) {
    string f;
    cin >> f;
    s += f;
  }
  //二重ループでＡ、数える Ｂ、入れ替え
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      //Ａ、数える
      //１、四隅 ２、国構え ３、中身  s.at(i*W+j)が基本
      int count = 0;
      //１、四隅 左上 ３か所をチェック
      if(i==0 && j==0) {
        if(s.at(1) == '#') count++;
        if(s.at(W) == '#') count++;
        if(s.at(W+1) == '#') count++;
      }
      //１、四隅 右上
      if(i==0 && j==W-1) {
        if(s.at(j-1) == '#') count++;
        if(s.at(W+j-1) == '#') count++;
        if(s.at(W+j) == '#') count++;
      }
      //１、四隅 左下
      if(i==H-1 && j==0) {
        if(s.at((i-1)*W) == '#') count++;
        if(s.at((i-1)*W+1) == '#') count++;
        if(s.at(i*W+1) == '#') count++;
      }
      //１、四隅 右下
      if(i==H-1 && j==W-1) {
        if(s.at((i-1)*W+j-1) == '#') count++;
        if(s.at((i-1)*W+j) == '#') count++;
        if(s.at(i*W-1) == '#') count++;
      }
      //２、国構え 上 ５か所をチェック s.at(i*W+j)が基本
      if(i==0 && j!=0 && j!=W-1) {
        if(s.at(j-1) == '#') count++;
        if(s.at(j+1) == '#') count++;
        if(s.at(W+j-1) == '#') count++;
        if(s.at(W+j) == '#') count++;
        if(s.at(W+j+1) == '#') count++
      }
      //２、国構え 下
      if(i==H-1 && j!=0 && j!=W-1) {
        if(s.at((i-1)*W+j-1) == '#') count++;
        if(s.at((i-1)*W+j) == '#') count++;
        if(s.at((i-1)*W+j+1) == '#') count++;
        if(s.at(i*W+j-1) == '#') count++;
        if(s.at(i*W+j+1) == '#') count++
      }
      //２、国構え 左
      if(j==0 && i!=0 && i!=H-1) {
        if(s.at((i-1)*W) == '#') count++;
        if(s.at((i-1)*W+1) == '#') count++;
        if(s.at(i*W+1) == '#') count++;
        if(s.at((i+1)*W) == '#') count++;
        if(s.at((i+1)*W+1) == '#') count++;
      }
      //２、国構え 右
      if(j==W-1 && i!=0 && i!=H-1) {
        if(s.at((i-1)*W+j-1) == '#') count++;
        if(s.at((i-1)*W+j) == '#') count++;
        if(s.at(i*W+j-1) == '#') count++;
        if(s.at((i+1)*W+j-1) == '#') count++;
        if(s.at((i+1)*W+j) == '#') count++;
      }
      //３、中身 ８か所をチェック s.at(i*W+j)が基本
      if(i!=0 && i!=H-1 && j!=0 && j!=W-1) {
        if(s.at((i-1)*W+j-1) == '#') count++;
        if(s.at((i-1)*W+j) == '#') count++;
        if(s.at((i-1)*W+j+1 == '#') count++;
        if(s.at(i*W+j-1) =='#') count++;
        if(s.at(i*W+j+1) =='#') count++;
        if(s.at((i+1)*W+j-1) =='#') count++;
        if(s.at((i+1)*W+j) =='#') count++;
        if(s.at((i+1)*W+j+1) =='#') count++;
      }
      //Ｂ、入れ替え   ＃、１～８、０の順番
      if(s.at(i*W+j) == '#') s.at(i*W+j) = '#';
      else if(count == 1) s.at(i*W+j) = '1';
      else if(count == 2) s.at(i*W+j) = '2';
      else if(count == 3) s.at(i*W+j) = '3';
      else if(count == 4) s.at(i*W+j) = '4';
      else if(count == 5) s.at(i*W+j) = '5';
      else if(count == 6) s.at(i*W+j) = '6';
      else if(count == 7) s.at(i*W+j) = '7';
      else if(count == 8) s.at(i*W+j) = '8';
      else if(s.at(i*W+j) == '.') s.at(i*W+j) = '0';
    }
  }
  //改行して出力
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; J++) {
      cout << s.at(i*W+j);
      if(j == W-1)
        cout << endl;
    }
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:50:39: error: expected ';' before '}' token
   50 |         if(s.at(W+j+1) == '#') count++
      |                                       ^
      |                                       ;
   51 |       }
      |       ~                                
a.cc:58:41: error: expected ';' before '}' token
   58 |         if(s.at(i*W+j+1) == '#') count++
      |                                         ^
      |                                         ;
   59 |       }
      |       ~                                  
a.cc:80:36: error: expected ';' before 'count'
   80 |         if(s.at((i-1)*W+j+1 == '#') count++;
      |                                    ^~~~~~
      |                                    ;
a.cc:80:16: warning: ignoring return value of 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::at(size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; reference = char&; size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
   80 |         if(s.at((i-1)*W+j+1 == '#') count++;
      |            ~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/basic_string.h:1309:7: note: declared here
 1309 |       at(size_type __n)
      |       ^~
a.cc:81:9: error: expected primary-expression before 'if'
   81 |         if(s.at(i*W+j-1) =='#') count++;
      |         ^~
a.cc:80:45: error: expected ')' before 'if'
   80 |         if(s.at((i-1)*W+j+1 == '#') count++;
      |           ~                                 ^
      |                                             )
   81 |         if(s.at(i*W+j-1) =='#') count++;
      |         ~~                                   
a.cc:102:27: error: 'J' was not declared in this scope
  102 |     for(int j = 0; j < W; J++) {
      |                           ^