public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0)
            return 0;
        g=grid;

        boolean[][] visited = new boolean[grid.length][grid[0].length];
        for (int i=0; i<g.length; i++) {
            for (int j=0; j<g[0].length; j++) {
                visited[i][j] = false;
            }
        }
        this.visited = visited;

        int islands = 0;
        for (int i=0; i<grid.length; i++) {
            for (int j=0; j<grid[0].length; j++) {
                if (grid[i][j] == '1' && !visited[i][j]) {
                    islands++;
                    conquer(i,j);
                }
            }
        }

        return islands;
    }