Searching from every empty room to find its nearest gate takes $O((m \cdot n)^2)$.
Instead, push all gates (0) into the queue at once and radiate outward.
Overwriting an empty room's INF value immediately seals it as visited,
producing the global distance map in a clean $O(m \cdot n)$ single sweep.
"Searching forward from empty rooms to find gates costs $O((m \cdot n)^2)$. By reversing perspective and flooding from all gates outward simultaneously, every cell is visited at most once in $O(m \cdot n)$."
"Scan the entire grid initially. Whenever rooms[r][c] == 0, add [r, c] to the queue. All gates begin at distance 0 together before any pop occurs."
"We do not need a separate visited[][] matrix. An empty room holds Integer.MAX_VALUE (INF). If rooms[nr][nc] == INF, we set rooms[nr][nc] = rooms[r][c] + 1 on push, immediately guarding it from duplicate visits."
"Rooms surrounded by walls are never reached by the BFS wave. They naturally retain their initial INF value as mandated by the problem specification."