<p>Given an <code>m x n</code> integer matrix <code>heightMap</code> representing the height of each unit cell in a 2D elevation map, return <em>the volume of water it can trap after raining</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg" style="width: 361px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg" style="width: 401px; height: 321px;" />
<pre>
<strong>Input:</strong> heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
<strong>Output:</strong> 10
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == heightMap.length</code></li>
	<li><code>n == heightMap[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 200</code></li>
	<li><code>0 &lt;= heightMap[i][j] &lt;= 2 * 10<sup>4</sup></code></li>
</ul>

# - don't think 1d problem is helpful.
# - a region traps water if it has a boundary of cells strictly higher
# than the "middle". (can maintain the current "depth" considered?)
# - better to think about it "from the outside in", which gaps don't get 
# filled with water?
# - no "boundary" cell of the entire grid can be filled with water.
# - cells with air are "reachable" from the boundary.
# - "BFS" in from the outer boundary, look at lowest cell v in the "search"
# range, for it's unvisited neighbours if one has a lower value v1 than v, 
# then it is filled with v - v1 litres of water. (however not all cells are
# reached this way)
# - maintain the current "level" of water as well, allow the search to reach
# these "low" cells.
# - very tricky.

import heapq

class Solution:
    def getBoundaryHeap(self, heightMap: List[List[int]]):
        working = []
        for r in range(self.m):
            if r == 0 or r == self.m - 1:
                for c in range(self.n):
                    heapq.heappush(working, (heightMap[r][c], r, c))

            else:
                heapq.heappush(working, (heightMap[r][0], r, 0))
                heapq.heappush(working, (heightMap[r][-1], r, self.n - 1))
        return working

    def trapRainWater(self, heightMap: List[List[int]]) -> int:
        self.m, self.n = len(heightMap), len(heightMap[0])
        working = self.getBoundaryHeap(heightMap)
        seen = set(working)
        vol_trapped, level = 0, 0
        while working:
            h, r, c = heapq.heappop(working)
            seen.add((h, r, c))
            level = max(level, h)
            neigs = [(r-1,c), (r+1,c), (r,c-1), (r,c+1)]
            for R, C in neigs:
                if 0 <= R < self.m and 0 <= C < self.n and (heightMap[R][C], R, C) not in seen:
                    if heightMap[R][C] < level:
                        vol_trapped += level - heightMap[R][C]
                    heapq.heappush(working, (heightMap[R][C], R, C))
                    seen.add((heightMap[R][C], R, C))
        return vol_trapped
23200