easiest  solution by NIKHIL DWIVEDI
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach: Key Concepts\nThe solution revolves around several key concepts:\n\nGrid 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach: Key Concepts\nThe solution revolves around several key concepts:\n\nGrid Representation: The grid is represented as a 2D array where:\n\n0 indicates an uninfected cell,\n1 indicates an infected cell,\n2 indicates a cell that has been contained.\nRegions: A region is defined as a cluster of infected cells. The algorithm identifies these regions and assesses the number of uninfected neighboring cells.\n\nPriority Queue: This data structure is used to prioritize regions based on the number of uninfected cells adjacent to them, allowing the algorithm to contain the most threatening regions first.\n\nDepth-First Search (DFS): This technique is employed to explore the grid and gather information about infected and uninfected cells within a region.\n\nCode Structure\nThe code consists of two main classes: Solution and Region. The Solution class contains the primary logic for containing the virus, while the Region class encapsulates the properties of each infected region.\n\nSolution Class\ncontainVirus Method: This method orchestrates the containment process by identifying regions, calculating walls needed, and updating the grid.\ndfs Method: A helper method that performs a depth-first search to explore the grid and gather information about the infected and uninfected cells.\nRegion Class\nAttributes: It maintains lists of infected and uninfected cells, as well as a count of walls required for containment.\nMethods: It includes methods to add infected and uninfected cells and implements the Comparable interface to facilitate sorting in the priority queue.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    \n    private static final int[][] DIR = new int[][]{\n        {1, 0}, {-1, 0}, {0, 1}, {0, -1}\n    };\n    \n    public int containVirus(int[][] isInfected) {\n        int m = isInfected.length, n = isInfected[0].length;\n        int ans = 0;\n        \n        while( true ) {\n            // infected regions, sorted desc according to the number of nearby \n            // uninfected nodes\n            PriorityQueue<Region> pq = new PriorityQueue<Region>();\n            // already visited cells\n            boolean[][] visited = new boolean[m][n];\n            \n            // find regions\n            for(int i=0; i<m; i++) {\n                for(int j=0; j<n; j++) {\n                    \n                    // if current cell is infected, and it\'s not visited\n                    if( isInfected[i][j] != 1 || visited[i][j] ) \n                        continue;\n                    \n                    // we found a new region, dfs to find all the infected\n                    // and uninfected cells in the current region\n                    Region reg = new Region();\n                    dfs(i, j, reg, isInfected, visited, new boolean[m][n], m, n);\n                    \n                    // if there are some uninfected nodes in this region, \n                    // we can contain it, so add it to priority queue\n                    if( reg.uninfected.size() != 0)\n                        pq.offer(reg);\n                }\n            }\n            \n            // if there are no regions to contain, break\n            if( pq.isEmpty() )\n                break;\n\n            // Contain region with most uninfected nodes\n            Region containReg = pq.poll();\n            ans += containReg.wallsRequired;\n            \n            // use (2) to mark a cell as contained\n            for(int[] cell : containReg.infected)\n                isInfected[cell[0]][cell[1]] = 2;\n            \n            // Spread infection to uninfected nodes in other regions\n            while( !pq.isEmpty() ) {\n                Region spreadReg = pq.poll();\n                \n                for(int[] cell : spreadReg.uninfected)\n                    isInfected[cell[0]][cell[1]] = 1;\n            }\n        }\n        return ans;\n    }\n    \n    private void dfs(int i, int j, Region reg, int[][] grid, boolean[][] visited, boolean[][] uninfectedVis, int m, int n) {\n        visited[i][j] = true;\n        reg.addInfected(i, j);\n        \n        for(int[] dir : DIR) {\n            int di = i + dir[0];\n            int dj = j + dir[1];\n            \n            // continue, if out of bounds OR contained OR already visited\n            if( di < 0 || dj < 0 || di == m || dj == n || grid[di][dj] == 2 || visited[di][dj] )\n                continue;\n            \n            // if neighbour node is not infected\n            if( grid[di][dj] == 0 ) {\n                // a wall will require to stop the spread from cell (i,j) to (di, dj)\n                reg.wallsRequired++;\n                \n                // if this uninfected node is not already visited for current region\n                if( !uninfectedVis[di][dj] ) {\n                    uninfectedVis[di][dj] = true;\n                    reg.addUninfected(di, dj);\n                }\n            } else \n                dfs(di, dj, reg, grid, visited, uninfectedVis, m, n);\n        }\n    }\n}\nclass Region implements Comparable<Region> {\n    public List<int[]> infected;\n    public List<int[]> uninfected;\n    public int wallsRequired;\n    \n    public Region() {\n        infected = new ArrayList();\n        uninfected = new ArrayList();\n    }\n    \n    public void addInfected(int row, int col) {\n        infected.add(new int[]{ row, col });\n    }\n    \n    public void addUninfected(int row, int col) {\n        uninfected.add(new int[]{ row, col });\n    }\n    \n    @Override\n    public int compareTo(Region r2) {\n        return Integer.compare(r2.uninfected.size(), uninfected.size());\n    }\n}\n```
['Java']