You are given a terrain map—a two-dimensional array with height values for each square. Now the area gets flooded, starting with a given location, so that all locations with height ≤ 0 are submerged in water.

To compute the entire part that gets flooded, one can use a stack. Whenever a location gets flooded, push all of its neighbor locations with height ≤ 0 onto a stack. Then pop the next stack value, mark it as flooded, push its previously unvisited neighbor locations with height ≤ 0 onto the stack, and so on.

For example, consider a terrain

1 2 1 3 1
0 1 0 0 1
0 0 1 0 0
0 1 2 1 0

Suppose we start flooding at row 2, column 3. The flooded area will be

1 2 1 3 1
0 1 * * 1
0 0 1 * *
0 1 2 1 *

You are going to implement a method that carries out the algorithm. To make sure you are doing it right, you need to indicate the order in which the flooding is carried out. Mark the first location that gets flooded as 1, the second one as 2, and so on. Mark each location before you push it on the stack. In order to visit neighbors in a uniform way, use the Location.neighbors method that visits them in clockwise direction starting with North.

Return an array with the visitation orders. In this example, you would return

0 0 0 0 0
0 0 5 2 0
0 0 0 1 3
0 0 0 0 4

First, [2][3] gets visited and pushed. Then it gets popped and its neighbors, [1][3] and [2][4] get visited and pushed. Then [2][4] gets popped. Push [3][4] but not [2][3] because it was already visited. Then [3][4] gets popped. It yields no new neighbors, so [1][3] gets popped and its neighbor [1][2] gets visited and pushed. It gets popped, yielding no new locations. The stack is empty, and you return the array shown.