#include<stdio.h>
#include<stdlib.h>
#define MAX 50

int bomb[MAX][MAX];

int main()
{
	int row,column;
	int number;
	scanf("%d%d",&row,&column);
	getchar();//吸收回车键 
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<column;j++)
		{ 
			scanf("%c",&bomb[i][j]);
		}
		getchar();//吸收回车键
	}//输入字符数组(二维) 
	if(row>1 && column>1)
	{
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<column;j++)
			{
				number=0;
				if(bomb[i][j]=='#') printf("#");//是雷就直接输出雷
				else if(bomb[i][j]=='.')//开始寻找周围的几个点有哪些是雷.注意边界判断. 
				{
					//需要注意的只有边界上的一圈.搞定这些之后剩下的就是绕圈找:而根据数组遍历的特性,一定是逐行进行遍历的,所以每次循环时都要留心. 
					if(i==0)//第一行
					{
						if(j==0)//左上角
						{
							if(bomb[0][1]=='#') number++;
							if(bomb[1][0]=='#') number++;
							if(bomb[1][1]=='#') number++; 
							printf("%d",number);
						} 
						else if(j==column-1)//右上角
						{
							if(bomb[0][column-2]=='#') number++;
							if(bomb[1][column-1]=='#') number++;
							if(bomb[1][column-2]=='#') number++; 
							printf("%d",number);
						} 
						else//其他点 
						{
							if(bomb[0][j-1]=='#') number++;
							if(bomb[0][j+1]=='#') number++;
							if(bomb[1][j-1]=='#') number++;
							if(bomb[1][j]=='#') number++;
							if(bomb[1][j+1]=='#') number++;
							printf("%d",number);
						}
					} 
					else if(i==row-1)//最后一行 
					{
						if(j==0)//左下角
						{
							if(bomb[row-1][1]=='#') number++;
							if(bomb[row-2][0]=='#') number++;
							if(bomb[row-2][1]=='#') number++; 
							printf("%d",number);
						} 
						else if(j==column-1)//右下角
						{
							if(bomb[row-1][column-2]=='#') number++;
							if(bomb[row-2][column-1]=='#') number++;
							if(bomb[row-2][column-2]=='#') number++; 
							printf("%d",number);
						} 
						else//其他点 
						{
							if(bomb[row-1][j-1]=='#') number++;
							if(bomb[row-1][j+1]=='#') number++;
							if(bomb[row-2][j-1]=='#') number++;
							if(bomb[row-2][j]=='#') number++;
							if(bomb[row-2][j+1]=='#') number++;
							printf("%d",number);
						}
					}
					else//不是第一行也不是最后一行 
					{
						if(j==0)//如果是第一列
						{
							if(bomb[i-1][0]=='#') number++;
							if(bomb[i+1][0]=='#') number++;
							if(bomb[i-1][1]=='#') number++;
							if(bomb[i][1]=='#') number++;
							if(bomb[i+1][1]=='#') number++;
							printf("%d",number);
						}
						else if(j==column-1)//如果是最后一列
						{
							if(bomb[i-1][column-1]=='#') number++;
							if(bomb[i+1][column-1]=='#') number++;
							if(bomb[i-1][column-2]=='#') number++;
							if(bomb[i][column-2]=='#') number++;
							if(bomb[i+1][column-2]=='#') number++;
							printf("%d",number);
						}
						else//其他点 
						{
							if(bomb[i-1][j-1]=='#') number++;
							if(bomb[i-1][j]=='#') number++;
							if(bomb[i-1][j+1]=='#') number++;
							if(bomb[i][j-1]=='#') number++;
							if(bomb[i][j+1]=='#') number++;
							if(bomb[i+1][j-1]=='#') number++;
							if(bomb[i+1][j]=='#') number++;
							if(bomb[i+1][j+1]=='#') number++;
							printf("%d",number);
						} 
					}
				} 
			}
			printf("\n");
	 	} 
	}
	else 
	{
		if(row==1 && column==1) 
		{
			if(bomb[0][0]=='#') printf("#\n");	
			else if(bomb[0][0]=='.') printf("0\n");	
		}
		else if(row==1 && column!=1) 
		{
			for(int i=0;i<column;i++)
			{
				number=0;
				if(bomb[0][i]=='#') printf("#");
				else if(i==0) 
				{
					if(bomb[0][1]=='#') number++; 
				 	printf("%d",number);
				}
				else if(i==column-1)
				{
					if(bomb[0][column-2]=='#') number++; 
				 	printf("%d",number);
				}
				else 
				{
					if(bomb[0][i-1]=='#') number++;
					if(bomb[0][i+1]=='#') number++;
					printf("%d",number);
				}
			}
			printf("\n");
		}
		else if(column==1 && row!=1) 
		{
			for(int i=0;i<row;i++)
			{
				number=0;
				if(bomb[i][0]=='#') printf("#\n");
				else if(i==0) 
				{
					if(bomb[1][0]=='#') number++; 
				 	printf("%d\n",number);
				}
				else if(i==row-1)
				{
					if(bomb[row-2][0]=='#') number++; 
				 	printf("%d\n",number);
				}
				else 
				{
					if(bomb[i-1][0]=='#') number++;
					if(bomb[i+1][0]=='#') number++;
					printf("%d\n",number);
				}
			}
		}
	}
} 

Measured memory_bytes: 128