<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p>

<p>The distance between two adjacent cells is <code>1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[0,0,0]]
<strong>Output:</strong> [[0,0,0],[0,1,0],[0,0,0]]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;" />
<pre>
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]]
<strong>Output:</strong> [[0,0,0],[0,1,0],[1,2,1]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == mat.length</code></li>
	<li><code>n == mat[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>4</sup></code></li>
	<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
	<li>There is at least one <code>0</code> in <code>mat</code>.</li>
</ul>

class Solution {
public:
    bool update(vector<vector<int>>& mat, vector<vector<int>>& r, int i, int j, int m, int n, int d) {
        if (i < 0 || i >= m || j < 0 || j >= n) {
            return false;
        }
        if (mat[i][j] != 0) {
             r[i][j] = std::min(d + 1, r[i][j]);
            return true;
        }
        return false;
    }

    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
        const int m = mat.size();
        const int n = mat[0].size();
        vector<vector<int>> r(m, vector<int>(n, std::numeric_limits<int>::max()));
        queue<pair<int, int>> q;
        set<pair<int, int>> visited;

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (mat[i][j] == 0) q.push({i, j});
            }
        }
        while (!q.empty()) {
            auto p = q.front();
            q.pop();
            visited.insert(p);

            auto left = make_pair(p.first - 1, p.second);
            auto right = make_pair(p.first + 1, p.second);
            auto up = make_pair(p.first, p.second - 1);
            auto down = make_pair(p.first, p.second + 1);

            if (mat[p.first][p.second] == 0) {
                r[p.first][p.second] = 0;
            }

            if (update(mat, r, left.first, left.second, m, n, r[p.first][p.second]) && !visited.count(left)) {
                q.push(left);
            }
            if (update(mat, r, right.first, right.second, m, n, r[p.first][p.second]) && !visited.count(right)) {
                q.push(right);
            }
            if (update(mat, r, up.first, up.second, m, n, r[p.first][p.second]) && !visited.count(up)) {
                q.push(up);
            }
            if (update(mat, r, down.first, down.second, m, n, r[p.first][p.second]) && !visited.count(down)) {
                q.push(down);
            }
        }
        return r;

    }
};
54940