On a grid, mark a cell when you pop it, and your stack visits in recursion's order
The order a stack visits a grid in can differ from recursion's.
Predict
Your stack marks a cell when it pushes it. From (0,0) on an open 3 by 4 grid, when does it visit (0,1), right next to the start?
An open 3 by 4 grid from (0,0), each cell numbered by when it's visited
recursion
05611147102389
stack, mark on push
01167110582349
stack, mark on pop
05611147102389
Both stacks push the neighbors in reverse. The highlighted cell is (0,1).
On a tree, pushing the children in reverse was enough to match recursion. On a grid it isn't. Recursion goes down column 0 and snakes back. The stack that marks on push visits (0,1) last: (0,0) marked it at the very beginning, so no later cell could push it again.
Mark a cell when you pop it instead, and skip a popped cell that's already marked. Then the order matches recursion. The price is that a cell can sit on the stack more than once, at most once for each of its neighbors. For counting regions the order doesn't matter, so mark on push.