<p>You are given an <code>m x n</code> <code>grid</code> where each cell can have one of three values:</p>

<ul>
	<li><code>0</code> representing an empty cell,</li>
	<li><code>1</code> representing a fresh orange, or</li>
	<li><code>2</code> representing a rotten orange.</li>
</ul>

<p>Every minute, any fresh orange that is <strong>4-directionally adjacent</strong> to a rotten orange becomes rotten.</p>

<p>Return <em>the minimum number of minutes that must elapse until no cell has a fresh orange</em>. If <em>this is impossible, return</em> <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/16/oranges.png" style="width: 650px; height: 137px;" />
<pre>
<strong>Input:</strong> grid = [[2,1,1],[1,1,0],[0,1,1]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[2,1,1],[0,1,1],[1,0,1]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,2]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since there are already no fresh oranges at minute 0, the answer is just 0.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10</code></li>
	<li><code>grid[i][j]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</li>
</ul>

class Solution {
public:
    int n, m, stack = 0;
    
    void dfs(int i, int j, vector<int> &steps, vector<vector<int>>& grid) {
        int right = i * m + j + 1, left = i * m + j - 1, down = (i + 1) * m + j, up = (i - 1) * m + j;
        
        if (j < m - 1 && grid[i][j + 1] == 1 && steps[right] > stack + 1) {
            stack++;
            if (steps[right] > stack) {
                steps[right] = stack;
                dfs(i, j + 1, steps, grid);
            }
            stack--;
        }
        if (i < n - 1 && grid[i + 1][j] == 1 && steps[down] > stack + 1) {
            stack++;
            if (steps[down] > stack) {
                steps[down] = stack;
                dfs(i + 1, j, steps, grid);
            }
            stack--;
        }
        if (j > 0 && grid[i][j - 1] == 1 && steps[left] > stack + 1) {
            stack++;
            if (steps[left] > stack) {
                steps[left] = stack;
                dfs(i, j - 1, steps, grid);
            }
            stack--;
        }
        if (i > 0 && grid[i - 1][j] == 1 && steps[up] > stack + 1) {
            stack++;
            if (steps[up] > stack) {
                steps[up] = stack;
                dfs(i - 1, j, steps, grid);
            }
            stack--;
        }
    }
    
    
    int orangesRotting(vector<vector<int>>& grid) {
        n = grid.size(), m = grid[0].size();
        vector<int> steps(n * m, n * m);
        
        for (int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if (grid[i][j] == 2)
                    dfs(i , j, steps, grid);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++)
                cout << steps[i * m + j] << " ";
            cout << endl;
        }
        int max_steps = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    if (steps[i * m + j] == n * m)
                        return -1;
                    else if(steps[i * m + j] > max_steps)
                        max_steps = steps[i * m + j];
                }
            }
        }
        return max_steps;
    }
};
16000