content: class Solution {
      public int closedIsland(int[][] grid) {
          int m = grid.length;
          int n = grid[0].length;
          boolean[][] visit = new boolean[m][n];
          int count = 0;
          for (int i = 0; i < grid.length; i++) {
              for (int j = 0; j < grid[0].length; j++) {
                  if (grid[i][j] == 0 && !visit[i][j] && dfs(i, j, m, n, grid, visit)) {
                      count++;
                  }
              }
          }
          return count;
      }
  
      public boolean dfs(int x, int y, int m, int n, int[][] grid, boolean[][] visit) {
          if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length) {
              return false;
          }
          if (grid[x][y] == 1 || visit[x][y]) {
              return true;
          }
  
          visit[x][y] = true;
          bool isClosed = true;
          int[] dirx = {0, 1, 0, -1};
          int[] diry = {-1, 0, 1, 0};
  
          for (int i = 0; i < 4; i++) {
              int r = x + dirx[i];
              int c = y + diry[i];
              if (!dfs(r, c, m, n, grid, visit)) {
                  isClosed = false;
              }
          }
  
          return isClosed;
      }
  }