Given a binary matrix **mat[][]**, the task is to find the size of all possible non-empty connected cells.

An empty cell is denoted by

0while a non-empty cell is denoted by1.

Two cells are said to be connected if they are adjacent to each other horizontally or vertically, i.e.mat[i][j] = mat[i][j – 1]ormat[i][j] = mat[i][j + 1]ormat[i][j] = mat[i – 1][j]ormat[i][j] = mat[i + 1][j].

**Examples:**

Input:mat[][] = {{1, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, {1, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {1, 0, 1, 0, 1}}

Output:3 3 1 1 1 1

Explanation:

{mat[0][0], mat[0][1], mat[1][1]}, {mat[1][4], mat[2][3], mat[2][4]}}, {mat[2][0]}, {mat[4][0]}, {mat[4][2]}, {mat[4[4]} are the only possible connections.

Input:mat[][] = {{1, 1, 0, 0, 0}, {1, 1, 0, 1, 1}, {1, 0, 0, 1, 1}, {0, 0, 0, 0, 0}, {0, 0, 1, 1, 1}}

Output:5 4 3

**Approach:**

The idea is to use BFS and Recursion on the matrix.

Follow the steps below:

- Initialize a Queue Data Structure and insert a cell(
**mat[i][j] = 1**). - Perform
**BFS**on the inserted cell and traverse its adjacent cells. - Check for boundary conditions and check if the current element is
**1**, then flip it to**0**. - Mark the cell visited and update the size of the connected non-empty cells.
- Finally, print all the sizes of the connections obtained.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find size of all the ` `// islands from the given matrix ` `int` `BFS(vector<vector<` `int` `> >& mat, ` ` ` `int` `row, ` `int` `col) ` `{ ` ` ` `int` `area = 0; ` ` ` ` ` `// Initialize a queue for ` ` ` `// the BFS traversal ` ` ` `queue<pair<` `int` `, ` `int` `> > Q; ` ` ` `Q.push({ row, col }); ` ` ` ` ` `// Iterate untill the ` ` ` `// queue is empty ` ` ` `while` `(!Q.empty()) { ` ` ` ` ` `// Top element of queue ` ` ` `auto` `it = Q.front(); ` ` ` ` ` `// Pop the element ` ` ` `Q.pop(); ` ` ` ` ` `int` `r = it.first, c = it.second; ` ` ` ` ` `// Check for boundaries ` ` ` `if` `(r < 0 || c < 0 || r > 4 || c > 4) ` ` ` `continue` `; ` ` ` ` ` `// Check if current element is 0 ` ` ` `if` `(mat[r] == 0) ` ` ` `continue` `; ` ` ` ` ` `// Check if current element is 1 ` ` ` `if` `(mat[r] == 1) { ` ` ` ` ` `// Mark the cell visited ` ` ` `mat[r] = 0; ` ` ` ` ` `// Incrementing the size ` ` ` `area++; ` ` ` `} ` ` ` ` ` `// Traverse all neighbors ` ` ` `Q.push({ r + 1, c }); ` ` ` `Q.push({ r - 1, c }); ` ` ` `Q.push({ r, c + 1 }); ` ` ` `Q.push({ r, c - 1 }); ` ` ` `} ` ` ` ` ` `// Return the answer ` ` ` `return` `area; ` `} ` ` ` `// Function to print size of each connections ` `void` `sizeOfConnections(vector<vector<` `int` `> > mat) ` `{ ` ` ` ` ` `// Stores the size of each ` ` ` `// connected non-empty ` ` ` `vector<` `int` `> result; ` ` ` ` ` `for` `(` `int` `row = 0; row < 5; ++row) { ` ` ` `for` `(` `int` `col = 0; col < 5; ++col) { ` ` ` ` ` `// Check if the cell is ` ` ` `// non-empty ` ` ` `if` `(mat[row][col] == 1) { ` ` ` ` ` `// Function call ` ` ` `int` `area = BFS(mat, row, col); ` ` ` `result.push_back(area); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print the answer ` ` ` `for` `(` `int` `val : result) ` ` ` `cout << val << ` `" "` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `vector<vector<` `int` `> > mat ` ` ` `= { { 1, 1, 0, 0, 0 }, ` ` ` `{ 1, 1, 0, 1, 1 }, ` ` ` `{ 1, 0, 0, 1, 1 }, ` ` ` `{ 1, 0, 0, 0, 0 }, ` ` ` `{ 0, 0, 1, 1, 1 } }; ` ` ` ` ` `sizeOfConnections(mat); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6 4 3

**Time Complexity:** O(row * col)

**Auxiliary Space:** O(row * col)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Queries to find the count of connected Non-Empty Cells in a Matrix with updates
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Color a grid such that all same color cells are connected either horizontally or vertically
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Convert undirected connected graph to strongly connected directed graph
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Print cells with same rectangular sums in a matrix
- Find whether there is path between two cells in matrix
- Unique cells in a binary matrix
- Shortest distance between two cells in a matrix or grid
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Find safe cells in a matrix
- Find Number of Even cells in a Zero Matrix after Q queries
- Number of cells in a matrix that satisfy the given condition
- Number of cells in matrix which are equidistant from given two points
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Total number of cells covered in a matrix after D days

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.