C# Solution || Fast Solution using Priority Queue
\n# Complexity\n- Time complexity:\nO(mn log(mn))\n# Code\n\n\npublic class Solution {\n    class Point{\n        public int x;\n        public int y;\n        
\n# Complexity\n- Time complexity:\nO(mn log(mn))\n# Code\n```\n\npublic class Solution {\n    class Point{\n        public int x;\n        public int y;\n        public int val;\n        public Point(int _x,int _y,int _val){\n            x = _x;\n            y=_y;\n            val = _val;\n        }\n    }\n    public int TrapRainWater(int[][] heightMap) {\n        int m = heightMap.Length, n = heightMap[0].Length;\n        PriorityQueue<Point,int> pq = new(Comparer<int>.Create((a, b) => a.CompareTo(b)));\n        // add boundry\n        for(int r = 0; r < m;r++){\n            pq.Enqueue(new Point(r,0,heightMap[r][0]),heightMap[r][0]);\n            pq.Enqueue(new Point(r,n - 1,heightMap[r][n - 1]),heightMap[r][n - 1]);\n            heightMap[r][n - 1] = -1;\n            heightMap[r][0] = -1;\n        }\n        for(int c = 1; c < n - 1;c++){\n            pq.Enqueue(new Point(0,c,heightMap[0][c]),heightMap[0][c]);\n            pq.Enqueue(new Point(m - 1,c,heightMap[m - 1][c]),heightMap[m - 1][c]);\n            heightMap[0][c] = -1;\n            heightMap[m - 1][c] = -1;\n        }\n\n        Tuple<int,int>[] dir = new Tuple<int,int>[]\n        {Tuple.Create(0,1), Tuple.Create(0,-1),Tuple.Create(1,0),Tuple.Create(-1,0)};\n        int res = 0;\n        while(pq.Count > 0){\n            var boundary = pq.Dequeue();\n            for(int i = 0;i < dir.Length;i++){\n                int x = boundary.x + dir[i].Item1, y = boundary.y + dir[i].Item2;\n                if(x < 0 || x >= m || y < 0 || y >= n || heightMap[x][y] == -1) continue;\n                res+= Math.Max(0,boundary.val - heightMap[x][y]);\n                Point newBoundary = new Point(x,y,Math.Max(   heightMap[x][y], boundary.val  ));\n                pq.Enqueue(newBoundary,newBoundary.val);\n                heightMap[x][y] = -1;\n            }\n        }\n\n        return res;\n    }\n}\n```
['Heap (Priority Queue)', 'C#']