# Minimum number of operations required to set all elements of a binary matrix

Given a binary matrix** mat[][] **consisting of **1s and 0s** of dimension **M * N**, the task is to find the number of operations to convert all 0s to 1s. In each operation, all the 1s can convert their adjacent 0s to 1s.

**Note:** Diagonal elements are not considered as adjacent elements of a number in the matrix.

**Examples:**

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

{0, 1, 0, 1, 0},

{0, 0, 0, 0, 1},

{0, 0, 1, 0, 0}}

Output:2

Explanation:All the adjacent elements are in either left/right/up/down directions.

Matrix before the operations will be:

0 1 1 0 1

0 1 0 1 0

0 0 0 0 1

0 0 1 0 0In the above example, operations would be as follows:

After Operation 1, the matrix will be:

11 111

11111

01111

01111After Operation 2, the matrix will be:

1 1 1 1 1

1 1 1 1 1

11 1 1 1

11 1 1 1

Note:Numbers inboldindicate the modified 0s.

**Approach:**

- Traverse the entire matrix and push the co-ordinates of the matrix elements with value 1 into a queue.
- Save the size of the queue in variable
**x**. - Traverse the queue for
**x**iterations. - For each iteration, the position of an element with a value 1 is encountered. Convert the values in its adjacent positions to 1(only if the adjacent elements are 0) and then push the co-ordinates of the newly converted 1’s to the queue.
- In each of the
**x**iterations, dequeue the front element of the queue as soon as the step 4 is performed. - As soon as the
**x**elements of the queue are traversed, increment the count of the number of operations to 1. - Repeat step 2 to step 6 till the queue is empty.
- Return the count of the number of operations after the queue is empty. If the matrix has all 1s, no operation will be performed and the count will be 0.

Below is the implementation of the above approach.

## C++

`// C++ code for the above approach. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check whether the ` `// adjacent neighbour is a valid ` `// index or not ` `bool` `check(` `int` `row, ` `int` `col, ` ` ` `int` `r, ` `int` `c) { ` ` ` ` ` `return` `(r >= 0 && r < row ` ` ` `&& c >= 0 && c < col); ` `} ` ` ` `// function to calculate the ` `// number of operations ` `int` `bfs(` `int` `row, ` `int` `col, ` ` ` `int` `*grid) { ` ` ` ` ` `// direction matrix to find ` ` ` `// adjacent neighbours ` ` ` `int` `dir[4][2] = { { 1, 0 }, ` ` ` `{ -1, 0 }, ` ` ` `{ 0, 1 }, ` ` ` `{ 0, -1 } }; ` ` ` ` ` `// queue with pair to store ` ` ` `// the indices of elements ` ` ` `queue<pair<` `int` `, ` `int` `> > q; ` ` ` ` ` `// scanning the matrix to push ` ` ` `// initial 1 elements ` ` ` `for` `(` `int` `i = 0; i < row; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; ` ` ` `j < col; j++) { ` ` ` ` ` `if` `(*((grid+i*col) + j)) ` ` ` `q.push(make_pair(i, j)); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Step 2 to Step 6 ` ` ` `int` `op = -1; ` ` ` `while` `(!q.empty()) { ` ` ` ` ` `int` `x = q.size(); ` ` ` `for` `(` `int` `k = 0; ` ` ` `k < x; k++) { ` ` ` ` ` `pair<` `int` `, ` `int` `> p = ` ` ` `q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// adding the values of ` ` ` `// direction matrix to the ` ` ` `// current element to get ` ` ` `// 4 possible adjacent ` ` ` `// neighbours ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < 4; i++) { ` ` ` ` ` `int` `r = p.first + ` ` ` `dir[i][0]; ` ` ` `int` `c = p.second + ` ` ` `dir[i][1]; ` ` ` ` ` `// checking the validity ` ` ` `// of the neighbour ` ` ` `if` `(*((grid+r*col) + c) == 0 ` ` ` `&& check(row, col, r, c)) { ` ` ` ` ` `// pushing it to the queue ` ` ` `// and converting it to 1 ` ` ` `q.push(make_pair(r, c)); ` ` ` `*((grid+r*col) + c) = 1; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// increasing the operation by 1 ` ` ` `// after the first pass ` ` ` `op++; ` ` ` `} ` ` ` `return` `op; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `row = 4, col = 5; ` ` ` `int` `grid[][col] = ` ` ` `{ { 0, 1, 1, 0, 1 }, ` ` ` `{ 0, 1, 0, 1, 0 }, ` ` ` `{ 0, 0, 0, 0, 1 }, ` ` ` `{ 0, 0, 1, 0, 0 } }; ` ` ` ` ` `cout << bfs(row, col, ` ` ` `(` `int` `*)grid); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(M * N)

**Auxillary Space Complexity: ** O(M * N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum operations required to set all elements of binary matrix
- Minimum operations required to make each row and column of matrix equals
- Minimum flip required to make Binary Matrix symmetric
- Minimum number of given operations required to make two strings equal
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Minimum operations of given type to make all elements of a matrix equal
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum operations required to make every element greater than or equal to K
- Minimum steps required to reach the end of a matrix | Set 2
- Find minimum steps required to reach the end of a matrix | Set - 1
- Find minimum steps required to reach the end of a matrix | Set 2
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum steps to get 1 at the center of a binary matrix
- Sum of all minimum frequency elements in Matrix
- Minimum number of cuts required to pay salary from N length Gold Bar
- Find row number of a binary matrix having maximum number of 1s
- Find row with maximum and minimum number of zeroes in given Matrix
- Number of elements greater than modified mean in matrix

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.