longest solution I've ever written on leetcode. 1A, love u all
\nclass Pos {\n    int x;\n    int y;\n\n    Pos(int x, int y) {\n        this.x = x;\n        this.y = y;\n    }\n}\n\nclass Solution {\n    private static fin
```\nclass Pos {\n    int x;\n    int y;\n\n    Pos(int x, int y) {\n        this.x = x;\n        this.y = y;\n    }\n}\n\nclass Solution {\n    private static final int MAX_N = 50;\n    private static final int dr[] = {-1, 1, 0, 0};\n    private static final int dc[] = {0, 0, -1, 1};\n\n    private static boolean[][] vis = new boolean[MAX_N][MAX_N];\n\n    private int numVirus(int[][] grid, int x, int y) {\n        int row = grid.length;\n        int col = grid[0].length;\n\n        int res = 0;\n        vis[x][y] = true;\n        for (int i = 0; i < 4; ++i) {\n            int nx = x + dr[i];\n            int ny = y + dc[i];\n            if (nx >= 0 && nx < row && ny >= 0 && ny < col ) {\n                if (grid[nx][ny] == 0) {\n                    ++res;\n                    grid[nx][ny] = 3;\n                } else if (grid[nx][ny] == 1 && !vis[nx][ny]) {\n                    res += numVirus(grid, nx, ny);\n                }\n            }\n        }\n        return res;\n    }\n\n    private Pos maxRegion(int[][] grid) {\n        int x = -1;\n        int y = -1;\n        int cnt = 0;\n\n        for (int i = 0; i < MAX_N; ++i) {\n            for (int j = 0; j < MAX_N; ++j) {\n                vis[i][j] = false;\n            }\n        }\n\n        int row = grid.length;\n        int col = grid[0].length;\n        for (int i = 0; i < row; ++i) {\n            for (int j = 0; j < col; ++j) {\n                if (grid[i][j] == 1 && !vis[i][j]) {\n                    int num = numVirus(grid, i, j);\n                    if (num > cnt) {\n                        x = i;\n                        y = j;\n                        cnt = num;\n                    }\n                    for (int a = 0; a < row; ++a) {\n                        for (int b = 0; b < col; ++b) {\n                            if (grid[a][b] == 3) {\n                                grid[a][b] = 0;\n                            }\n                        }\n                    }\n                }\n            }\n        }\n\n        return new Pos(x, y);\n    }\n\n    private int buildWalls(int[][] grid, int x, int y) {\n        int row = grid.length;\n        int col = grid[0].length;\n\n        grid[x][y] = 2;\n        int res = 0;\n        for (int i = 0; i < 4; ++i) {\n            int nx = x + dr[i];\n            int ny = y + dc[i];\n            if (nx >= 0 && nx < row && ny >= 0 && ny < col) {\n                if (grid[nx][ny] == 0) {\n                    ++res;\n                } else if (grid[nx][ny] == 1) {\n                    res += buildWalls(grid, nx, ny);\n                }\n            }\n        }\n\n        return res;\n    }\n\n    private void expand(int[][] grid) {\n        int row = grid.length;\n        int col = grid[0].length;\n        for (int i = 0; i < row; ++i) {\n            for (int j = 0; j < col; ++j) {\n                if (grid[i][j] == 1) {\n                    for (int d = 0; d < 4; ++d) {\n                        int ni = i + dr[d];\n                        int nj = j + dc[d];\n                        if (ni >= 0 && ni < row && nj >= 0 && nj < col && grid[ni][nj] == 0) {\n                            grid[ni][nj] = 3;\n                        }\n                    }\n                }\n            }\n        }\n\n        for (int i = 0; i < row; ++i) {\n            for (int j = 0; j < col; ++j) {\n                if (grid[i][j] == 3) {\n                    grid[i][j] = 1;\n                }\n            } \n        }\n    }\n\n    public int containVirus(int[][] grid) {\n        int res = 0;\n        Pos pos = maxRegion(grid);\n\n        while (pos.x != -1 && pos.y != -1) {\n            res += buildWalls(grid, pos.x, pos.y);\n            expand(grid);\n            pos = maxRegion(grid);\n        }\n\n        return res;\n    }\n}\n\n\n```
[]