---
# System prepended metadata

title: 0200. Number Of Islands
tags: [Leetcode, DFS, Island, Medium]

---

# 0200. Number Of Islands
###### tags: `Leetcode` `Medium` `DFS` `Island`
Link:https://leetcode.com/problems/number-of-islands/
## 思路
DFS
## Code
```java=
class Solution {
    public int numIslands(char[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int count = 0;
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == '1'){
                    dfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
    public static void dfs(char[][] grid, int i, int j){
        grid[i][j] = '0';
        int m = grid.length;
        int n = grid[0].length;
        if(i>0 && grid[i-1][j]=='1') dfs(grid, i-1,j);
        if(i<m-1 && grid[i+1][j]=='1') dfs(grid,i+1,j);
        if(j>0 && grid[i][j-1]=='1') dfs(grid,i,j-1);
        if(j<n-1 && grid[i][j+1]=='1') dfs(grid,i,j+1);
    }
}
```