// C Program to find area of the largest region of 1s#include<stdio.h>#include<stdlib.h>// Define constants for rows and columns#define ROWS 5#define COLS 7// A function to check if cell(r, c) can be included in DFSintisSafe(intM[ROWS][COLS],intr,intc,introws,intcols){// Row number is in range, column number is in range,// and value is 1return(r>=0&&r<rows)&&(c>=0&&c<cols)&&(M[r][c]==1);}// Depth-First-Search to visit all cells in the current islandvoidDFS(intM[ROWS][COLS],intr,intc,introws,intcols,int*area){// These arrays are used to get row and column numbers of 8// neighbours of a given cellintdirR[]={-1,-1,-1,0,0,1,1,1};intdirC[]={-1,0,1,-1,1,-1,0,1};// Increment area of region by 1(*area)++;// Mark this cell as visitedM[r][c]=0;// Recur for all connected neighboursfor(inti=0;i<8;i++){intnewR=r+dirR[i];intnewC=c+dirC[i];if(isSafe(M,newR,newC,rows,cols)){DFS(M,newR,newC,rows,cols,area);}}}// Function to find area of the largest region of 1sintlargestRegion(intM[ROWS][COLS],introws,intcols){// Initialize result as 0 and traverse through// all cells of given matrixintmaxArea=0;for(inti=0;i<rows;i++){for(intj=0;j<cols;j++){// If a cell with value 1 is foundif(M[i][j]==1){intarea=0;DFS(M,i,j,rows,cols,&area);// Maximize the areaif(area>maxArea){maxArea=area;}}}}returnmaxArea;}intmain(){intM[ROWS][COLS]={{1,0,0,0,1,0,0},{0,1,0,0,1,1,1},{1,1,0,0,0,0,0},{1,0,0,1,1,0,0},{1,0,0,1,0,1,1}};printf("%d\n",largestRegion(M,ROWS,COLS));return0;}