Python | Heap | Concise | O(n*m*log(n*m)); O(n*m)
```\nclass Solution:\n    def trapRainWater(self, heightMap: List[List[int]]) -> int:\n        n, m, ans = len(heightMap), len(heightMap[0]), 0\n        v, heap
```\nclass Solution:\n    def trapRainWater(self, heightMap: List[List[int]]) -> int:\n        n, m, ans = len(heightMap), len(heightMap[0]), 0\n        v, heap = [[False] * m for _ in range(n)], []\n        for i in range(n):\n            for j in range(m):\n                if i in (0, n - 1) or j in (0, m - 1):\n                    v[i][j] = True\n                    heapq.heappush(heap, (heightMap[i][j], i, j))\n        while heap:\n            val, i, j = heapq.heappop(heap)\n            for i_n, j_n in [[i, j + 1], [i + 1, j], [i, j - 1], [i - 1, j]]:\n                if 0 <= i_n < n and 0 <= j_n < m and not v[i_n][j_n]:\n                    ans, v[i_n][j_n] = ans + max(0, val - heightMap[i_n][j_n]), True\n                    heapq.heappush(heap, (max(val, heightMap[i_n][j_n]), i_n, j_n))\n        return ans
[]