Java solution using Priority Queue
\nclass Solution {\n    public static class Pair implements Comparable<Pair>{\n        int row,col,val;\n        public Pair(int row,int col, int val){\n       
```\nclass Solution {\n    public static class Pair implements Comparable<Pair>{\n        int row,col,val;\n        public Pair(int row,int col, int val){\n            this.row = row;\n            this.col = col;\n            this.val = val;\n        }\n        public int compareTo(Pair other){\n            return this.val - other.val;\n        }\n        \n    }\n    public int trapRainWater(int[][] mat) {\n        \n        int water = 0;\n        int rowL = mat.length;\n        int colL = mat[0].length;\n        PriorityQueue<Pair> q = new PriorityQueue<Pair>();\n        int row[] = {1,-1,0,0};\n        int col[] = {0,0,1,-1};\n        \n        \n        \n        //pushing the boundary building bcz overflow occur due to boundary, find min so that overflow doesnt occur\n        for(int i = 0;i<rowL;i++){\n            if(mat[i][0] != -1){\n                q.add(new Pair(i,0,mat[i][0]));\n                mat[i][0] = -1;\n            }\n            if(mat[i][colL - 1] != -1){\n                q.add(new Pair(i,colL - 1,mat[i][colL - 1]));\n                mat[i][colL - 1] = -1;\n            }\n        }\n        for(int j = 0;j < colL;j++){\n            if(mat[0][j] != -1){\n                q.add(new Pair(0,j,mat[0][j]));\n                mat[0][j] = -1;\n            }\n            if(mat[rowL - 1][j] != -1){\n                q.add(new Pair(rowL - 1,j,mat[rowL - 1][j]));\n                 mat[rowL - 1][j] = -1;\n            }\n        }\n        \n        while(!q.isEmpty()){\n            Pair min = q.remove();\n            \n            //check all its 4 directions\n            for(int i = 0; i < 4;i++){\n                int r = min.row + row[i];\n                int c = min.col + col[i];\n                \n                //check valid r and c and they are not visited\n                \n                if(r >= 0 && r < rowL && c >= 0 && c < colL && mat[r][c] != -1){\n                    \n                    //check if it can store water\n                    if(min.val > mat[r][c]){\n                        water += (min.val - mat[r][c]);\n                        q.add(new Pair(r ,c , min.val)); //boundary will be larger one  3, 1, 0 ,100  so now boundry in that case will be 3 not 1 for zer0.\n                    }\n                    else{\n                        //cant store any water\n                        q.add(new Pair(r, c, mat[r][c]));\n                    }\n                    \n                    //mark as visited\n                    mat[r][c] = -1;\n                }\n            }\n        }\n        return water;\n    }\n}\n```
['Breadth-First Search', 'Heap (Priority Queue)', 'Java']