For each cell, return the distance to the nearest 0. Multi-source BFS from every 0 simultaneously gives the answer in linear time — the gold-standard pattern asked at every FAANG company.
LC 286 Walls and Gates asks you to fill each empty room with its distance to the nearest gate. The key insight is to flip the direction: instead of BFS from every room, do multi-source BFS from all gates simultaneously and push distances outward in O(m*n) time.
LC 1020 Number of Enclaves asks you to count land cells that cannot reach the grid boundary. The trick is to flip the problem: flood-fill from the boundary inward, eliminating all reachable land, then count what remains.
LC 1905 Count Sub Islands asks how many islands in grid2 are subsets of islands in grid1. The critical trap is short-circuiting DFS on the first invalid cell — you must always complete the traversal to mark the whole island visited, while tracking validity separately.
Flip at most one 0 to 1 to maximize island area. The "color the islands, then try every flip" trick avoids quadratic re-DFS — a top Google interview problem.