<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 orangesRotting(vector<vector<int>>& grid) {
        queue<pair<pair<int, int>, int>> storer;
        int n = grid.size();
        int m = grid[0].size();
        int onecnt = 0;  // Count of fresh oranges
        
        // Traverse the grid and store initial rotten oranges, count fresh ones
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 2) {
                    storer.push({{i, j}, 0});  // Store the rotten orange with time 0
                } else if (grid[i][j] == 1) {
                    onecnt++;  // Count fresh oranges
                }
            }
        }
        
        // If there are no fresh oranges, return 0 (no time needed)
        if (onecnt == 0)
            return 0;
        
        int dirx[] = {1, 0, -1, 0};  // Directions for row
        int diry[] = {0, 1, 0, -1};  // Directions for column
        int twocnt = 0;  // Counter for how many fresh oranges turn rotten
        int t = 0;  // Time to rot all oranges
        
        // Process the grid using BFS
        while (!storer.empty()) {
            int i = storer.front().first.first;  // Current row
            int j = storer.front().first.second;  // Current column
            t = storer.front().second;  // Time step for this rotten orange
            storer.pop();
            
            // Explore the 4 adjacent directions
            for (int k = 0; k < 4; k++) {
                int new_i = i + dirx[k];
                int new_j = j + diry[k];
                
                // Check if the new position is valid and has a fresh orange
                if (new_i >= 0 && new_i < n && new_j >= 0 && new_j < m && grid[new_i][new_j] == 1) {
                    grid[new_i][new_j] = 2;  // Turn the fresh orange rotten
                    storer.push({{new_i, new_j}, t + 1});  // Add to queue with incremented time
                    twocnt++;  // Increment the count of newly rotten oranges
                }
            }
        }
        
        // If not all fresh oranges could rot, return -1
        if (onecnt != twocnt) {
            return -1;
        }
        
        return t;  // Return the time taken to rot all oranges
    }
};

16400