I think this question of yours is similar to the question have been asked plz check out the link mentioned below.. link to your question.
if the meaning of your question is same please check out the program written below.
Program:
#include<iostream>
#define k 6
void check(int arr[][k], int aux[][k], int i, int j)
{
if(aux[i][j] == 1)
return;
aux[i][j] =1;
if(i<0 || i>=k || j<0 || j>=k)
return;
for(int p=i-1; p<=i+1; p++)
{
for(int q = j-1; q<=j+1; q++)
{
if(arr[p][q] == 1)
{
if(p==i && q ==j)
continue;
check(arr, aux, p, q);
}
}
}
}
int main()
{
int count =0;
int arr[][k] = {{1, 0, 0, 1, 0, 0},
{0, 0, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 0},
{1, 0, 1, 1, 0, 0}};
int aux[k][k];
for(int i=0; i<k; i++)
{
for(int j=0; j<k; j++)
aux[i][j]=0;
}
for(int i=0; i<k; i++)
{
for(int j=0; j<k; j++)
{
if(arr[i][j] == 1 && aux[i][j] == 0)
{
count++;
check(arr, aux, i, j);
}
}
}
cout<<"Number of clusters containing ones :" ;
cout<<count<<endl;
}
Output:
Number of clusters containing ones : 3