using System.Collections.Generic;
public static void Main()
List<List<int>> matrix= new List<List<int>>(){
new List<int>(){ 1 ,1 , 1 ,1 },
new List<int>(){ 1 ,0 , 0 ,1 },
new List<int>(){ 1 ,0 , 0 ,0 },
new List<int>(){ 1 ,1 , 0 ,1 }
int result = connectedCell(matrix);
Console.WriteLine("Connected Cells: " + result);
public static int getRegionSize(List<List<int>> matrix,int row,int col){
Console.WriteLine("({0}, {1})",row,col);
if(row < 0 || col < 0 || row >= matrix.Count || col >= matrix[row].Count)
if(matrix[row][col] == 0)
for(int r= row-1; r <= row+1; r++){
for(int c = col-1; c <= col +1 ; c++){
if(r != row || c != col){
size += getRegionSize(matrix, r, c);
public static int connectedCell(List<List<int>> matrix)
for(int row=0; row < matrix.Count; row++)
for (int col = 0 ; col< matrix[row].Count; col++)
if(matrix[row][col] == 1)
int size = getRegionSize(matrix, row, col);
maxRegion = Math.Max(size, maxRegion);