Why DFS is the wrong instinct here: perimeter is an edge-counting problem, not a traversal.
Scan each cell and count +1 for every boundary or water-facing side — solving in O(1) space without recursion.
"While this looks like a graph question, using DFS is an anti-pattern. Traversal requires $O(m \cdot n)$ auxiliary call stack space, which is entirely unnecessary."
"Every land cell starts with 4 possible perimeter edges. An edge exists if and only if the adjacent cell is off-grid or water. We simply sum these tests in two nested loops."
"Because we only read adjacent coordinates, the input grid remains completely immutable and unmodified."
"Time is $O(m \cdot n)$ as we touch each cell once. Auxiliary space is strictly $O(1)$ — no visited array, no queues, no recursion."
4 − adjacent_land_neighbors edges. No visited matrix or recursive stack needed.