OI VI - bit

// https://szkopul.edu.pl/problemset/problem/fIwYfy0zzmVZJJXTShFq2ICC/site/?key=statement
// OI VI (2 etap)

#include <algorithm>
#include <bitset>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

constexpr int sizik = 100 * 1000;

int n, m;
// vector<pair<int, int>> kra[sizik];
std::bitset<sizik> isBialy;
int odl[sizik];
std::bitset<sizik> visited;

int generateCord(int x, int y) {
    return y * m + x;
}

void BFS() {
    std::queue<pair<int, int>> kol;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            int curr = generateCord(j, i);

            if (!isBialy[curr]) {
                continue;
            }

            visited[curr] = true;
            kol.push({j, i});
        }
    }
    int d = 0;

    while (!kol.empty()) {
        const auto& p = kol.front();
        kol.pop();

        int lx = p.first, ly = p.second;

        int temp0 = generateCord(lx, ly);
        int temp1 = generateCord(lx - 1, ly);
        int temp2 = generateCord(lx + 1, ly);
        int temp3 = generateCord(lx, ly - 1);
        int temp4 = generateCord(lx, ly + 1);

        d = odl[temp0] + 1;

        if (lx != 1 && visited[temp1] != true) {
            kol.push({lx - 1, ly});
            visited[temp1] = true;
            odl[temp1] = d;
        }

        if (lx != m && visited[temp2] != true) {
            kol.push({lx + 1, ly});
            visited[temp2] = true;
            odl[temp2] = d;
        }

        if (ly != 1 && visited[temp3] != true) {
            kol.push({lx, ly - 1});
            visited[temp3] = true;
            odl[temp3] = d;
        }

        if (ly != n && visited[temp4] != true) {
            kol.push({lx, ly + 1});
            visited[temp4] = true;
            odl[temp4] = d;
        }
    }
}

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    std::cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            char c;
            std::cin >> c;

            // c -= '0';
            isBialy[generateCord(j, i)] = c == '1';
        }
    }

    BFS();

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            std::cout << odl[generateCord(j, i)] << ' ';
        }
        std::cout << '\n';
    }

    return 0;
}