I can Guarantee you that The Cleanest Code You'll Ever See with Best in class Explanation !!!
Intuition :-
Simulation
Max Heap
DFS
Approach :-

Helper Classes :-

Pair Class :

row - To store the row of a cell.
column- To store the column of cell.


Regi
# Intuition :-\n\n- Simulation\n- Max Heap\n- DFS\n\n# Approach :-\n\n1) **Helper Classes :-**\n\n    1) ```Pair``` Class :\n        - ```row``` - To store the row of a cell.\n        - ```column```- To store the column of cell.\n    \n    2) ```Region_Data``` Class :\n        - ```walls_required_count``` - Number of walls required to contain the region.\n        - ```uninfected_cells_count``` - Number of uninfected cells adjacent to the region.\n        - ```infected_cells_list``` - List of cells which are infected in the region.\n\n2) **DFS Traversal** ```depth_first_search()``` :-\n\n    - Parameters :-\n\n        - ```current_i``` and ```current_j``` : The current position in the matrix.\n        - ```R``` and ```C``` : Dimensions of the matrix.\n        - ```matrix``` : The input matrix where ,\n            - ```1``` indicates infected cells.\n            - ```0``` indicates uninfected cells.\n            - ```-999``` indicates already contained cells.\n        - ```visited_matrix``` : Tracks whether a cell has already been visited during the current DFS traversal.\n        - ```uninfected_cells_visited_matrix``` : Tracks uninfected cells adjacent to the infected region (to avoid counting the same cell multiple times).\n        - ```region_data_object``` : Stores details about the current region (infected cells list , walls required count , uninfected cells count).\n    \n    - Explanation :-\n    \n        - Step - 1 is to Mark Current Cell as Visited\n        \n            ```\n            visited_matrix[current_i][current_j] = true;\n\n            region_data_object.infected_cells_list.add((new Pair(current_i, current_j)));\n            ```\n\n            - The current cell is marked as visited to avoid revisiting it in the DFS traversal.\n            - Add the current infected cell\'s coordinates to the list of infected cells in this region.\n        \n        - Step - 2 is to Explore All Four Directions\n\n            ```\n            for (int[] coordinates_array : four_directions_array) {\n                int next_i = current_i + coordinates_array[0];\n                int next_j = current_j + coordinates_array[1];\n            ```\n            \n            - Use ```four_directions_array``` (up , down , left , right) to compute the adjacent cells.\n        \n        - Step - 3 is Check Boundaries and Cell States\n\n            ```\n            if (is_within_bounds(next_i, next_j, R, C) && matrix[next_i][next_j] != -999 && !visited_matrix[next_i][next_j]) {\n            ```\n\n            - Ensure the next cell is:\n                - Within the matrix boundaries ```(is_within_bounds(next_i , next_j , R , C))```\n                - Not already contained ```(matrix[next_i][next_j] != -999)```\n                - Not already visited ```(!visited_matrix[next_i][next_j])```\n\n        - Step - 4 is to Handle Infected Cell ```(matrix[next_i][next_j] == 1)```\n            ```\n            if(matrix[next_i][next_j] == 1){\n                depth_first_search(next_i , next_j , R , C , matrix , visited_matrix , uninfected_cells_visited_matrix , region_data_object);\n            }\n            ```\n\n            - If the adjacent cell is also infected (1), recursively call DFS on that cell to continue exploring the current region.\n\n\n        - Step - 5 is to Handle Uninfected Cell ```(matrix[next_i][next_j] == 0)```\n            \n            ```\n            else if(matrix[next_i][next_j] == 0){\n                \n                region_data_object.walls_required_count++;\n                \n                if(!uninfected_cells_visited_matrix[next_i][next_j]){\n                    uninfected_cells_visited_matrix[next_i][next_j] = true;\n                    region_data_object.uninfected_cells_count++;\n                }\n            }\n            ```\n\n            - If the adjacent cell is uninfected (0):\n                - Increment the count of walls required for containment, as this cell is a boundary.\n                - Check if this uninfected cell was already considered for this region as ```if(!uninfected_cells_visited_matrix[next_i][next_j]){```\n                    - If the above given condition is true then ,\n                        - Mark the current cell as visited in the ```uninfected_cells_visited_matrix```.\n                        - Increment the ```uninfected_cells_count``` in the ```region_data_object```.\n\n2) **Simulation** ```containVirus()``` :-\n\n    - A ```while()``` loop is used to run the simulation logic and it stops the simulation when the ```max_heap``` is empty.\n    - The ```max_heap``` will store the ```Region_Data```\'s Object and sorts them in descending order based on the ```uninfected_cells_count``` in the object.\n    - It will go throught the ```matrix``` and finds the Region of infected part with the help of ```depth_first_search()```.\n    - After the DFS traversal the ```region_data_object``` will have the necessity details of the region.\n    - If the ```uninfected_cells_count``` in the ```region_data_object``` is greater than ```0``` then the ```region_data_object``` is added to the ```max_heap```.\n    - If the ```max_heap``` is empty then break the ```while()``` loop.\n    - Now the first ```Region_Data``` object from the ```max_heap``` is polled out and stored as ```region_data_object_with_maximum_uninfected_cells```.\n    - Add the ```walls_required_count``` from the ```region_data_object_with_maximum_uninfected_cells``` to the ```total_walls_required_count```.\n    - Now mark the cells in the ```infected_cells_list``` in the  ```region_data_object_with_maximum_uninfected_cells``` with value ```-999``` to indicate the cells are contained properly.\n    - Now make a ```while()``` loop to run until the ```max_heap``` is empty and mark the adjacent cells (Four Directions) of the ```infected_cells_list``` of the ```Region_Data```\'s Object in the ```max_heap ``` which simulates the spread of the virus in the matrix.\n    - Finally return the ```total_walls_required_count```.\n\n# Complexity\n\n- Time Complexity : $$O(T \xD7 (R \xD7 C + K logK))$$\n    - R : Number of rows in the matrix.\n    - C : Number of columns in the matrix.\n    - K : Number of infected regions per iteration.\n    - T : Number of iterations until the virus is fully contained.\n\n- Space Complexity : $$O(R x C)$$\n    - For visited matrices , region data storage , and priority queue.\n\n# Code\n\n``` java []\n\nclass Pair {\n    \n    int row;\n    \n    int column;\n    \n    Pair(int row , int column){\n        \n        this.row = row;\n        \n        this.column = column;\n    }\n}\n\nclass Region_Data {\n    \n    int walls_required_count;\n    \n    int uninfected_cells_count;\n    \n    List<Pair> infected_cells_list;\n    \n    Region_Data(){\n        \n        walls_required_count = 0;\n        \n        uninfected_cells_count = 0;\n        \n        infected_cells_list = new ArrayList<>();\n    }\n}\n\nclass Solution {\n    \n    int[][] four_directions_array = new int[][] {{-1 , 0} , {1 , 0} , {0 , -1} , {0 , 1}};\n    \n    public boolean is_within_bounds(int current_i , int current_j , int R , int C){\n        \n        return ((current_i >= 0) && (current_i < R) && (current_j >= 0) && (current_j < C));\n    }\n    \n    public void depth_first_search(int current_i , int current_j , int R , int C , int[][] matrix , boolean[][] visited_matrix , boolean[][] uninfected_cells_visited_matrix , Region_Data region_data_object){\n        \n        visited_matrix[current_i][current_j] = true;\n        \n        region_data_object.infected_cells_list.add((new Pair(current_i , current_j)));\n        \n        for(int[] coordinates_array : four_directions_array){\n            \n            int next_i = (current_i + coordinates_array[0]);\n            \n            int next_j = (current_j + coordinates_array[1]);\n            \n            if((is_within_bounds(next_i , next_j , R , C)) && (matrix[next_i][next_j] != -999) && (!visited_matrix[next_i][next_j])){\n                \n                if(matrix[next_i][next_j] == 1){\n                    depth_first_search(next_i , next_j , R , C , matrix , visited_matrix , uninfected_cells_visited_matrix , region_data_object);\n                }else if(matrix[next_i][next_j] == 0){\n                    \n                    region_data_object.walls_required_count++;\n                    \n                    if(!uninfected_cells_visited_matrix[next_i][next_j]){\n                        uninfected_cells_visited_matrix[next_i][next_j] = true;\n                        region_data_object.uninfected_cells_count++;\n                    }\n                }\n            }\n        }\n    }\n    \n    public int containVirus(int[][] matrix){\n        \n        int total_walls_required_count = 0;\n        \n        int R = matrix.length;\n        \n        int C = matrix[0].length;\n        \n        while(true){\n            \n            PriorityQueue<Region_Data> max_heap = new PriorityQueue<>((a , b) -> (b.uninfected_cells_count - a.uninfected_cells_count));\n            \n            boolean[][] visited_matrix = new boolean[R][C];\n            \n            for(int i = 0 ; (i < R) ; i++){\n                \n                for(int j = 0 ; (j < C) ; j++){\n                    \n                    if((!visited_matrix[i][j]) && (matrix[i][j] == 1)){\n                        \n                        Region_Data region_data_object = new Region_Data();\n                        \n                        depth_first_search(i , j , R , C , matrix , visited_matrix , (new boolean[R][C]) , region_data_object);\n                        \n                        if(region_data_object.uninfected_cells_count > 0){\n                            max_heap.add(region_data_object);\n                        }\n                    }\n                }\n            }\n            \n            if(max_heap.isEmpty()){\n                break;\n            }\n            \n            Region_Data region_data_object_with_maximum_uninfected_cells = max_heap.poll();\n            \n            total_walls_required_count += region_data_object_with_maximum_uninfected_cells.walls_required_count;\n            \n            for(Pair pair_object : region_data_object_with_maximum_uninfected_cells.infected_cells_list){\n                matrix[pair_object.row][pair_object.column] = -999;\n            }\n            \n            while(!max_heap.isEmpty()){\n                \n                for(Pair pair_object : max_heap.poll().infected_cells_list){\n                    \n                    int current_i = pair_object.row;\n                    \n                    int current_j = pair_object.column;\n                    \n                    for(int[] coordinates_array : four_directions_array){\n                        \n                        int next_i = (current_i + coordinates_array[0]);\n                        \n                        int next_j = (current_j + coordinates_array[1]);\n                        \n                        if((is_within_bounds(next_i , next_j , R , C)) && (matrix[next_i][next_j] == 0)){\n                            matrix[next_i][next_j] = 1;\n                        }\n                    }\n                }\n            }\n        }\n        \n        return total_walls_required_count;\n    }\n}\n```
['Depth-First Search', 'Breadth-First Search', 'Heap (Priority Queue)', 'Matrix', 'Simulation', 'Java']