Beecrowd 2552

 avatar
kaziamir
c_cpp
a year ago
2.6 kB
0
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

int main(){
	int N,M;
	while(cin>>N>>M){
		int arr[N][M];
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				cin>>arr[i][j];
			}
		}
		int arr2[N][M];

		for(int i=0;i<N;i++){
            for(int j=0;j<M;j++){
                cout<<arr2[i][j];
            }
            cout<<endl;
        }


		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				if(arr[i][j]==1){
					arr2[i][j]=9;
				}
				/******Four Corners Start******/
				else{
					if(i==0 && j==0 ){
						if(arr[i+1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j+1] == 1){
							arr2[i][j]++;
						}
					}
					else if(i==0  && j==(M-1)){
						if(arr[i+1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j-1] == 1){
							arr2[i][j]++;
						}
					}
					else if(i==(N-1)  && j==0){
						if(arr[i-1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j+1] == 1){
							arr2[i][j]++;
						}
					}
					else if(i==(N-1)  && j==(M-1)){
						if(arr[i-1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j-1] == 1){
							arr2[i][j]++;
						}
					}
					/******Four Corner Ends******/

					/****** Row and Column******/
						/***Row Start***/
					else if(i==0 && j!=0){
						if(arr[i][j-1] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j+1] == 1){
							arr2[i][j]++;
						}
						if(arr[i+1][j] == 1){
							arr2[i][j]++;
						}
					}
					else if(i==(N-1) && j!=0){
						if(arr[i][j-1] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j+1] == 1){
							arr2[i][j]++;
						}
						if(arr[i-1][j] == 1){
							arr2[i][j]++;
						}
					}
						/***Row Ends***/
						/***Column Starts***/
					else if(i!=0 && j==0){
						if(arr[i][j+1] == 1){
							arr2[i][j]++;
						}
						if(arr[i-1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i+1][j] == 1){
							arr2[i][j]++;
						}
					}
					else if(i!=0 && j==(M-1)){
						if(arr[i][j-1] == 1){
							arr2[i][j]++;
						}
						if(arr[i-1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i+1][j] == 1){
							arr2[i][j]++;
						}
					}
						/***Column Ends***/
					/******Row Column Ends******/
					/******Else Case******/
					else if(i!=0 && j!=0 && i!=(N-1) && j!=(M-1)){
						if(arr[i][j-1] == 1){
							arr2[i][j]++;
						}
						if(arr[i][j+1] == 1){
							arr2[i][j]++;
						}
						if(arr[i-1][j] == 1){
							arr2[i][j]++;
						}
						if(arr[i+1][j] == 1){
							arr2[i][j]++;
						}
					}
				}
			}
			for(int i=0;i<N;i++){
				for(int j=0;j<M;j++){
					cout<<arr2[i][j];
				}
                cout<<endl;
			}
		}
	}
		return 0;
}