import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 判断岛屿数量
     * @param grid char字符型二维数组 
     * @return int整型
     */
    
    static int cnt;
    // static boolean vis[][] = new boolean[201][201];
    static int[][] direction = new int[][]{{-1, 0}, {0, 1}, {1, 0},{0 ,-1}};
    public int solve (char[][] grid) {
        // write code here
        if (grid.length == 0){
            return 0;
        }
        if(grid[0].length == 0){
            return 0;
        }
	  // vis数组,表示这个点有没有被访问过
        boolean[][] vis = new boolean[grid.length][grid[0].length];
	  // 遍历整张图上的每一个点,如果是为1且没有被访问过,结果加1,并且进行dfs
        for(int i = 0; i < grid.length; i++){
            for(int j = 0; j < grid[0].length; j++){
                if(!vis[i][j] && grid[i][j] == '1'){
                    cnt++;
				  dfs(grid, vis, i, j);
                }
            }
        }
        
        return cnt;
    }

    void dfs(char[][] grid, boolean[][] vis, int x, int y){
        if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || vis[x][y] || grid[x][y] == '0'){
            return;
        }

        // vis
        for(int i = 0; i < direction.length; i++){
            vis[x][y] = true;
            int xi = direction[i][0] + x;
            int yi = direction[i][1] + y;
            dfs(grid, vis, xi, yi);
            // vis[x][y] = false;
        }
    }
}