You have built an above-ground pool and you want to know how much water it will hold. The pool structure is built out of stacks of cubes aligned on a grid. layout gives an overhead view showing the height of the cubes in each position of the grid. Here is an example pool:
16661
61116
16661
This pool can hold a total of 15 units of water: 5 units on each of the 3 middle grid locations. After that, any water added to the middle would flow out over the walls (the grid locations of height 6), and any water added to the walls or corners would flow out onto the surrounding ground. When it can, water will always flow to areas of lower height, and no water will "stand" on surfaces such as the pool walls shown here. Water cannot flow through diagonals, so it won't leak out of the middle via the corners. The ground surrounding the pool is at height 0 and can absorb an infinite amount of water. Return the total number of water units that can be held by the pool. |