Concise Python heapq 30 lines solution with detailed footnotes
\n\timport heapq\n\tclass Solution:\n\t\tdef trapRainWater(self, heightMap: List[List[int]]) -> int:\n\t\t\t# number of rows\n\t\t\tm = len(heightMap)\n\t\t\t# 
\n\timport heapq\n\tclass Solution:\n\t\tdef trapRainWater(self, heightMap: List[List[int]]) -> int:\n\t\t\t# number of rows\n\t\t\tm = len(heightMap)\n\t\t\t# number of columns\n\t\t\tn = len(heightMap[0])\n\t\t\t#initialize visited nodes and heapq\n\t\t\tflag = [[1 for _ in range(n)] for _ in range(m)]\n\t\t\tminheap = []\n\t\t\tfor i in range(m):\n\t\t\t\tfor j in range(n):\n\t\t\t\t\t#first add to the minheap the border of the enclosed rain water area\n\t\t\t\t\tif i == 0 or j == 0 or i == m - 1 or j == n - 1:\n\t\t\t\t\t\t# push into the heap (height, i,j) since we need to know the index of each height for later\n\t\t\t\t\t\theapq.heappush(minheap,(heightMap[i][j],i,j))\n\t\t\t\t\telse:\n\t\t\t\t\t\tflag[i][j] = 0\n\t\t\t#directional vector\n\t\t\tdx = [1,0,-1,0]\n\t\t\tdy = [0,1,0,-1]\n\t\t\tans = 0\n\t\t\twhile minheap:\n\t\t\t\t#the height of the water is determined by the lowest point in the border\n\t\t\t\tcurr = heapq.heappop(minheap)\n\t\t\t\tfor i in range(4):\n\t\t\t\t\tnewi = curr[1] + dx[i]\n\t\t\t\t\tnewj = curr[2] + dy[i]\n\t\t\t\t\t# check out of bound condition or if the node is already visited\n\t\t\t\t\tif newi < 0 or newi >= m or newj < 0 or newj >= n or flag[newi][newj]:\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t# if the new location has a smaller height than the current one (being popped), then it can at least accumulate as much rain water as the current location\n\t\t\t\t\tif heightMap[newi][newj] <= curr[0]:\n\t\t\t\t\t\t#update the new border with new height (after accumulating water)\n\t\t\t\t\t\theapq.heappush(minheap, (curr[0], newi, newj))\n\t\t\t\t\telse:\n\t\t\t\t\t\theapq.heappush(minheap, (heightMap[newi][newj], newi, newj))\n\t\t\t\t\t#mark the visited node\n\t\t\t\t\tflag[newi][newj] = 1\n\t\t\t\t\tans += max(0, curr[0] - heightMap[newi][newj])\n\t\t\treturn ans\n\t\t
['Heap (Priority Queue)', 'Python']