# 1254. Number of Closed Islands

```java
class Solution {
    private static final int[][] DIRS = {{0,1},{0,-1},{1,0},{-1,0}};
    public int closedIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        
        // exclude island connected with edge 
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ( (i == 0 || i == m-1 || j == 0 || j == n-1) && grid[i][j] == 0) {
                    dfs(grid, i, j);    
                }
            }
        }
        
        // count island numbers
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    dfs(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }
    private void dfs(int[][] grid, int i, int j) {
        grid[i][j] = 2;
        for (int[] dir : DIRS) {
            int x = i + dir[0];
            int y = j + dir[1];
            if (!isValid(grid, x, y) || grid[x][y] != 0) {
                continue;
            }
            dfs(grid, x, y);
        }
    }
    private boolean isValid(int[][] grid, int i, int j) {
        return i >=0 && i < grid.length && j >= 0 && j < grid[0].length;
    }
}


/*
0s (land) and 1s (water)

island(0) surrouded by water(1)

how to check is sourrouded by water?

1. 
island connected with edge, cant be sorround water, 
so exclude island connected with edge
dfs from these islands, and 
mark these islands to 1(water) or flood fill (mark 2, also ok)

2. 
last is to dfs, cal the island number inside this board

in conclution: only cal islands are "not connected to edge"
*/
```

### new version (filter in outside

````java
```java
class Solution {
    private int[][] DIRECTIONS = {{0,1}, {1,0}, {0,-1}, {-1,0}};
    public int closedIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;

        // exclude land from edge
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((i == 0 || i == m-1 || j == 0 || j == n-1) && grid[i][j] == 0) {
                    dfs(grid, i, j);
                }
            }
        }

        int count = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
    private void dfs(int[][] grid, int i, int j) {
        int m = grid.length;
        int n = grid[0].length;
        if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] != 0) {
            return;
        }
        grid[i][j] = 1;
        for (int[] direction : DIRECTIONS) {
            int nextI = i + direction[0];
            int nextJ = j + direction[1];
            dfs(grid, nextI, nextJ);
        }
    }
}
/*
mark all edge to 2 

count 0 
*/
```
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/dfs-and-bfs/dfs/1254.-number-of-closed-islands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
