From Pacific (top row, left column), DFS upward (to cells >= current).
Pacific-reachable cells: all cells water can flow from to reach Pacific.
From Atlantic (bottom row, right column), DFS upward.
Atlantic-reachable cells: all cells water can flow from to reach Atlantic.
Intersection: cells that can reach both.
Two DFS passes, each . Total: time. Space: for visited sets.