Language: C++

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

int main() {
  long long H,W,ans;
  cin>>H>>W;
  string c="";
  char d;
  vector<string>a(H),b(H);
  for(int i=0;i<H;i++){
    cin>>a[i];
  }
  if(H!=1&&W!=1){
	for(int i=0;i<H;i++){
      c="";
    	for(int j=0;j<W;j++){
      		ans=0;
      		if(a[i][j]=='#'){
              c+='#';
        		continue;
      		}
      		else{
      			if(i==0){
        			if(j==0){
          				if(a[i][j+1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i+1][j]=='#'){
            				ans++;
          				}
        			}
          			else  if(j==W-1){
          				if(a[i][j-1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j-1]=='#'){
            				ans++;
          				}
                    	if(a[i+1][j]=='#'){
            				ans++;
          				}			
        			} 
                	else{
                    	if(a[i][j+1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i+1][j]=='#'){
            				ans++;
          				}
						if(a[i][j-1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j-1]=='#'){
            				ans++;
          				}
					}
      			}
              	else if(i==H-1){
        			if(j==0){
          				if(a[i][j+1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i-1][j]=='#'){
            				ans++;
          				}
        			}
          			else if(j==W-1){
          				if(a[i][j-1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j-1]=='#'){
            				ans++;
          				}
                    	if(a[i-1][j]=='#'){
            				ans++;
          				}
        			}
                  	else{
                    	if(a[i][j+1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i-1][j]=='#'){
            				ans++;
          				}
                      	if(a[i][j-1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j-1]=='#'){
            				ans++;
                		}
                  	}
      			}
        		else{
                  	if(j==0){
          				if(a[i][j+1]=='#'){
            				ans++;
          				}		
          				if(a[i-1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i-1][j]=='#'){
            				ans++;
          				}
						if(a[i+1][j+1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j]=='#'){
            				ans++;
          				}
        			}
           			else  if(j==W-1){
          				if(a[i][j-1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j-1]=='#'){
            				ans++;
          				}	
                    	if(a[i-1][j]=='#'){
            				ans++;
          				}
            			if(a[i+1][j-1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j-1]=='#'){
            				ans++;
          				}
        			}
 					else{
                    	if(a[i][j-1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j-1]=='#'){
            				ans++;
          				}
                    	if(a[i-1][j]=='#'){
            				ans++;
          				}
               			if(a[i][j+1]=='#'){
            				ans++;
          				}
          				if(a[i-1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i+1][j-1]=='#'){
            				ans++;
          				}
          				if(a[i+1][j+1]=='#'){
            				ans++;
          				}
                    	if(a[i+1][j]=='#'){
            				ans++;
          				}
        			}
                }
            }
          d='0'+ans;
          c+=d;
        }
      b[i]=c;
    }
  }
  else if(H==1&&W!=1){
    c="";
    for(int j=0;j<W;j++){
      if(a[0][j]=='#'){
        c+='#';
        continue;
      }
      if(j==0){
          				if(a[0][j+1]=='#'){
            				ans++;
          				}
        			}
          			else  if(j==W-1){
          				if(a[0][j-1]=='#'){
            				ans++;
          				}
        			} 
                	else{
                    	if(a[0][j+1]=='#'){
            				ans++;
          				}
   
						if(a[0][j-1]=='#'){
            				ans++;
          				}
          				
					}
      d='0'+ans;
      c+=d;
    }
    b[0]=c;
  }
    else if(H!=1&&W==1){
    c="";
    for(int j=0;j<H;j++){
      if(a[j][0]=='#'){
        c+='#';
        continue;
      }
      if(j==0){
          				if(a[1][0]=='#'){
            				ans++;
          				}
        			}
          			else  if(j==H-1){
          				if(a[j-1][0]=='#'){
            				ans++;
          				}
        			} 
                	else{
                    	if(a[j+1][0]=='#'){
            				ans++;
          				}
   
						if(a[j-1][0]=='#'){
            				ans++;
          				}
          				
					}
      d='0'+ans;
      c+=d;
    }
    b[0]=c;
  }
  else{
    if(a[0]="#"){
      b[0]="#";
    }
    else{
      b[0]=0;
    }
  }
for(int i=0;i<H;i++){
  cout<<b[i]<<endl;
}
}

Compiler output:
a.cc: In function 'int main()':
a.cc:240:12: error: could not convert '(& a.std::vector<std::__cxx11::basic_string<char> >::operator[](0))->std::__cxx11::basic_string<char>::operator=(((const char*)"#"))' from 'std::__cxx11::basic_string<char>' to 'bool'
  240 |     if(a[0]="#"){
a.cc:244:12: error: ambiguous overload for 'operator=' (operand types are '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>'} and 'int')
  244 |       b[0]=0;
      |            ^
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:817:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  817 |       operator=(const basic_string& __str)
      |       ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:828:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  828 |       operator=(const _CharT* __s)
      |       ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:840:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  840 |       operator=(_CharT __c)
      |       ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:858:7: note: candidate: 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'
  858 |       operator=(basic_string&& __str)
      |       ^~~~~~~~