class Solution {
public:int orangesRotting(vector<vector<int>>& grid) {std::queue<std::pair<int, int>> qe; int fresh = 0; for (int i=0; i<grid.size(); i++) {for (int j=0; j<grid[i].size(); j++) {if (grid[i][j]==2) qe.push({i,j});else if (grid[i][j] == 1) fresh++;}}int count = 0; std::vector<std::pair<int, int>> dirts{{-1,0}, {1,0}, {0,1}, {0,-1}};while(!qe.empty()) {int size = qe.size(); bool flag = false; while(size>0){auto pair = qe.front();qe.pop();for (auto direct: dirts) {int i = pair.first + direct.first;int j = pair.second + direct.second;if (i>=0 && i<grid.size() && j>=0 && j<grid[0].size() && grid[i][j] == 1) {grid[i][j] = 2; qe.push({i,j}); fresh--; flag = true; }}size--;}if (flag) count++; }return fresh ? -1 : count;}
};