LeetCode 417 Pacific Atlantic Water Flow - Why Naive Fails

The trap

From each cell, do two DFS/BFS: one to check if it reaches Pacific, one for Atlantic.

For each of mnm \cdot n cells, you might explore the entire grid twice. Total: O((mn)2)O((mn)^2).

Can you reverse the problem?