For this Question: A group of connected 1's forms an island. The task is to print the number of islands present. We have a boolean matrix A of size NxM.
Note: if two vertices are diagonally connected they belong to the same connected componentThe first line contains two space separated integers N and M. Then in the next line are the NxM inputs of the matrix A separated by space.

Constraints:
1 <= N, M <= 100
0 <= A[i][j] <= 1For each testcase in a new line, print the number of islands present.Input
3 3
1 1 0 0 0 1 1 0 1

Output
2

Input
4 4
1 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0

Output
2

Explanation:
Testcase 1: The graph will look like
1 1 0
0 0 1
1 0 1
Here, two islands will be formed
First island will be formed by elements {A[0][0] ,  A[0][1], A[1][2], A[2][2]}
Second island will be formed by {A[2][0]}., I have written this Solution Code: import java.util.*;
import java.io.*;
class Main {
	public static void main (String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		int a[][]=new int[n][m];
		for(int i=0;i<n;i++)
		{

			for(int j=0;j<m;j++)
			{
				a[i][j]=sc.nextInt();
			}

		}
		 boolean [][]vis = new boolean[n][m];
		   int res = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (a[i][j]==1 && !vis[i][j])
            {
                bfs(a, vis, i, j,n,m);
                res++;
            }
        }
    }
	System.out.println(res);
	}
	static class pair
{
    int first, second;
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
static boolean isSafe(int mat[][], int i, int j,
                       boolean vis[][],int n,int m)
{
    return (i >= 0) && (i < n) &&
        (j >= 0) && (j < m) &&
        (mat[i][j]==1 && !vis[i][j]);
}

static void bfs(int mat[][], boolean vis[][],
                int si, int sj,int n,int m)
{
    int row[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    int col[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
    Queue<pair> q = new LinkedList<pair>();
    q.add(new pair(si, sj));
    vis[si][sj] = true;

    while (!q.isEmpty())
    {

        int i = q.peek().first;
        int j = q.peek().second;
        q.remove();


        for (int k = 0; k < 8; k++)
        {
            if (isSafe(mat, i + row[k],
                    j + col[k], vis,n,m))
            {
                vis[i + row[k]][j + col[k]] = true;
                q.add(new pair(i + row[k], j + col[k]));
            }
        }
    }
}





}, In this Programming Language: Java, Now tell me if this Code is compilable or not?