#include <iostream>
#include <queue>

using namespace std;

int dy[8] = {-1,-1,-1,0,0,1,1,1};
int dx[8] = {-1,0,1,-1,1,-1,0,1};
int main() {
    int n,m;
    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] = {0};
    queue<pair<int,int>> q;
    for (int i = 0;i < n;i++) {
        for (int j = 0;j < m;j++) {
            if (arr[i][j] == 1) {
                arr2[i][j] = 0;
                q.push(i,j);
            }
            
        }
    }
    
    while (!q.empty()) {
        int y = q.first();
        int x = q.second(); 
        q.pop();
        for (int i = 0;i < 8;i++) {
            int ny = y+dy[i];
            int nx = x+dx[i];
            if (0 <= ny && 0 <= nx && ny < n && nx < n) {
                arr2[ny][nx] = 1;
            }
        }
    }
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: