代码解答及思路:
#include <iostream>
using namespace std;
int main()
{
int n,m;
int a[100][100] = {0},b[100][100];//记住要开数组确数 ,这样外围就会有边 ,不能直接设置值,要记住!!!!
int i,j;
cin>>n>>m;
for(i = 1;i<=n;i++){
for(j = 1;j<=m;j++){
cin>>a[i][j];
}//输入数据
}
for(i = 1;i<=n;i++){
for(j = 1;j<=m;j++){
if(a[i][j] == 1){
b[i][j] = 9;
}
else{
if(a[i-1][j-1] == 1)
{
b[i][j]++;
}
if(a[i-1][j] == 1)
{
b[i][j]++;
}
if(a[i-1][j+1] == 1)
{
b[i][j]++;
}
if(a[i][j-1] == 1)
{
b[i][j]++;
}
if(a[i][j+1] == 1)
{
b[i][j]++;
}
if(a[i+1][j-1] == 1)
{
b[i][j]++;
}
if(a[i+1][j] == 1)
{
b[i][j]++;
}
if(a[i+1][j+1] == 1)
{
b[i][j]++;
}
}
}
}
for(i = 1;i<=n;i++){
for(j = 1;j<=m;j++){//第一列要空着输出
if(j == 1){
cout<<b[i][j];
}else{
cout<<" "<<b[i][j];
}
}
cout<<endl;
}
return 0;
}