<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:
    class Node {
    public:
        int row, col, dist;
        Node(int row, int col, int dist) {
            this->row = row;
            this->col = col;
            this->dist = dist;
        }
    };

    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, 1, -1};

    vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {

        int n = mat.size(), m = mat[0].size();

        vector<vector<int>> vis(n, vector<int>(m, 0));
        vector<vector<int>> dist(n, vector<int>(m, 1e9));

        // simple task push all the cell having 0 as the value
        queue<Node*> q;

        for (int idx = 0; idx < n; ++idx) {
            for (int jdx = 0; jdx < m; ++jdx) {
                if (mat[idx][jdx] == 0) {
                    q.push(new Node(idx, jdx, 0));
                    vis[idx][jdx] = 1;
                    dist[idx][jdx] = 0;
                }
            }
        }

        while (!q.empty()) {
            auto nd = q.front();
            q.pop();

            int curr_row = nd->row, curr_col = nd->col, curr_dist = nd->dist;
            for (int idx = 0; idx < 4; ++idx) {
                // search for 1 four directionally
                int new_row = curr_row + dx[idx];
                int new_col = curr_col + dy[idx];
                
                if (new_row >= 0 && new_col >= 0 && new_row < n &&
                    new_col < m && mat[new_row][new_col] == 1 &&
                    vis[new_row][new_col] == 0 &&
                    dist[new_row][new_col] > curr_dist + 1) {

                    dist[new_row][new_col] = curr_dist + 1;
                    q.push(new Node(new_row, new_col, curr_dist + 1));
                    vis[new_row][new_col] = 1;
                }
            }
        }
        return dist;
    }
};
44898