I think the worst case time complicity for this is much more than BFS, O((MN)^2) in worst case. Please let me know if this is correct.

Let the code tell:

```
class Solution {
public:
using Rooms = vector<vector<int>>;
void wallsAndGates(Rooms& rooms) {
if (rooms.empty() || rooms.back().empty()) return;
for (int i = 0; i < rooms.size(); ++i) {
for (int j = 0; j < rooms[i].size(); ++j) {
if (rooms[i][j] == 0) {
traverse(rooms, i, j, 0);
}
}
}
}
void traverse(Rooms& rooms, const int i, const int j, const int comparer) {
if (i < 0 || j < 0 || i >= rooms.size() || j >= rooms[i].size() || rooms[i][j] == -1) return;
if (comparer <= rooms[i][j]) {
rooms[i][j] = comparer;
traverse(rooms, i + 1, j, comparer + 1);
traverse(rooms, i - 1, j, comparer + 1);
traverse(rooms, i, j + 1, comparer + 1);
traverse(rooms, i, j - 1, comparer + 1);
}
}
};
```