# Total coverage of all zeros in a binary matrix

Given a binary matrix that is, it contains 0s and 1s only, we need to find sum of coverage of all zeros of the matrix where coverage for a particular 0 is defined as total number of ones around a zero in left, right, up and bottom directions. The ones can be anywhere till corner point in a direction.

**Examples:**

Input : mat[][] = {0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0} Output : 20 First four zeros are surrounded by only one 1. So coverage for zeros in first row is 1 + 1 + 1 + 1 Zeros in second row are surrounded by three 1's. Note that there is no 1 above. There are 1's in all other three directions. Coverage of zeros in second row = 3 + 3. Similarly counting for others also, we get overall count as below. 1 + 1 + 1 + 1 + 3 + 3 + 2 + 2 + 2 + 2 + 2 = 20 Input : mat[][] = {1 1 1 0 1 0 0 1} Output : 8 Coverage of first zero is 2 Coverages of other two zeros is 3 Total coverage = 2 + 3 + 3 = 8

A **simple solution** to solve this problem is by counting ones around zeros independently i.e. we run loop four times in each direction for each cell for the given matrix. Whenever we find a 1 in any loop, we break the loop and increment result by 1.

An **efficient solution** is to do following.

- Traverse all rows from left to right, increment result if a 1 is already seen (in current traversal) and current element is 0.
- Traverse all rows from right to left, increment result if a 1 is already seen (in current traversal) and current element is 0.
- Traverse all columns from top to bottom, increment result if a 1 is already seen (in current traversal) and current element is 0.
- Traverse all columns from bottom to top, increment result if a 1 is already seen (in current traversal) and current element is 0.

In below code a Boolean variable isOne is taken, which is made true as soon as a one is encountered in current traversal, for all zeros after that iteration, result is incremented by one, same procedure is applied in all four directions to get final answer. We reset isOne to false after every traversal.

## C++

`// C++ program to get total coverage of all zeros in ` `// a binary matrix ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define R 4 ` `#define C 4 ` ` ` `// Returns total coverage of all zeros in mat[][] ` `int` `getTotalCoverageOfMatrix(` `int` `mat[R][C]) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// looping for all rows of matrix ` ` ` `for` `(` `int` `i = 0; i < R; i++) ` ` ` `{ ` ` ` `bool` `isOne = ` `false` `; ` `// 1 is not seen yet ` ` ` ` ` `// looping in columns from left to right ` ` ` `// direction to get left ones ` ` ` `for` `(` `int` `j = 0; j < C; j++) ` ` ` `{ ` ` ` `// If one is found from left ` ` ` `if` `(mat[i][j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` ` ` `// If 0 is found and we have found ` ` ` `// a 1 before. ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` ` ` `// Repeat the above process for right to ` ` ` `// left direction. ` ` ` `isOne = ` `false` `; ` ` ` `for` `(` `int` `j = C-1; j >= 0; j--) ` ` ` `{ ` ` ` `if` `(mat[i][j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Traversing across columms for up and down ` ` ` `// directions. ` ` ` `for` `(` `int` `j = 0; j < C; j++) ` ` ` `{ ` ` ` `bool` `isOne = ` `false` `; ` `// 1 is not seen yet ` ` ` `for` `(` `int` `i = 0; i < R; i++) ` ` ` `{ ` ` ` `if` `(mat[i][j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` ` ` `isOne = ` `false` `; ` ` ` `for` `(` `int` `i = R-1; i >= 0; i--) ` ` ` `{ ` ` ` `if` `(mat[i][j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `mat[R][C] = {{0, 0, 0, 0}, ` ` ` `{1, 0, 0, 1}, ` ` ` `{0, 1, 1, 0}, ` ` ` `{0, 1, 0, 0} ` ` ` `}; ` ` ` ` ` `cout << getTotalCoverageOfMatrix(mat); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to get total ` `// coverage of all zeros in ` `// a binary matrix ` `import` `java .io.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `R = ` `4` `; ` `static` `int` `C = ` `4` `; ` ` ` `// Returns total coverage ` `// of all zeros in mat[][] ` `static` `int` `getTotalCoverageOfMatrix(` `int` `[][]mat) ` `{ ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `// looping for all ` ` ` `// rows of matrix ` ` ` `for` `(` `int` `i = ` `0` `; i < R; i++) ` ` ` `{ ` ` ` `// 1 is not seen yet ` ` ` `boolean` `isOne = ` `false` `; ` ` ` ` ` `// looping in columns from ` ` ` `// left to right direction ` ` ` `// to get left ones ` ` ` `for` `(` `int` `j = ` `0` `; j < C; j++) ` ` ` `{ ` ` ` `// If one is found ` ` ` `// from left ` ` ` `if` `(mat[i][j] == ` `1` `) ` ` ` `isOne = ` `true` `; ` ` ` ` ` `// If 0 is found and we ` ` ` `// have found a 1 before. ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` ` ` `// Repeat the above ` ` ` `// process for right ` ` ` `// to left direction. ` ` ` `isOne = ` `false` `; ` ` ` `for` `(` `int` `j = C - ` `1` `; j >= ` `0` `; j--) ` ` ` `{ ` ` ` `if` `(mat[i][j] == ` `1` `) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Traversing across columms ` ` ` `// for up and down directions. ` ` ` `for` `(` `int` `j = ` `0` `; j < C; j++) ` ` ` `{ ` ` ` `// 1 is not seen yet ` ` ` `boolean` `isOne = ` `false` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < R; i++) ` ` ` `{ ` ` ` `if` `(mat[i][j] == ` `1` `) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` ` ` `isOne = ` `false` `; ` ` ` `for` `(` `int` `i = R - ` `1` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` `if` `(mat[i][j] == ` `1` `) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `static` `public` `void` `main (String[] args) ` `{ ` ` ` `int` `[][]mat = {{` `0` `, ` `0` `, ` `0` `, ` `0` `}, ` ` ` `{` `1` `, ` `0` `, ` `0` `, ` `1` `}, ` ` ` `{` `0` `, ` `1` `, ` `1` `, ` `0` `}, ` ` ` `{` `0` `, ` `1` `, ` `0` `, ` `0` `}}; ` ` ` `System.out.println( ` ` ` `getTotalCoverageOfMatrix(mat)); ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to get total coverage of all zeros in ` `# a binary matrix ` `R ` `=` `4` `C ` `=` `4` ` ` `# Returns total coverage of all zeros in mat[][] ` `def` `getTotalCoverageOfMatrix(mat): ` ` ` `res ` `=` `0` ` ` ` ` `# looping for all rows of matrix ` ` ` `for` `i ` `in` `range` `(R): ` ` ` ` ` `isOne ` `=` `False` `# 1 is not seen yet ` ` ` ` ` `# looping in columns from left to right ` ` ` `# direction to get left ones ` ` ` `for` `j ` `in` `range` `(C): ` ` ` ` ` `# If one is found from left ` ` ` `if` `(mat[i][j] ` `=` `=` `1` `): ` ` ` `isOne ` `=` `True` ` ` ` ` `# If 0 is found and we have found ` ` ` `# a 1 before. ` ` ` `elif` `(isOne): ` ` ` `res ` `+` `=` `1` ` ` ` ` `# Repeat the above process for right to ` ` ` `# left direction. ` ` ` `isOne ` `=` `False` ` ` `for` `j ` `in` `range` `(C ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `if` `(mat[i][j] ` `=` `=` `1` `): ` ` ` `isOne ` `=` `True` ` ` `elif` `(isOne): ` ` ` `res ` `+` `=` `1` ` ` ` ` `# Traversing across columms for up and down ` ` ` `# directions. ` ` ` `for` `j ` `in` `range` `(C): ` ` ` `isOne ` `=` `False` `# 1 is not seen yet ` ` ` `for` `i ` `in` `range` `(R): ` ` ` ` ` `if` `(mat[i][j] ` `=` `=` `1` `): ` ` ` `isOne ` `=` `True` ` ` `elif` `(isOne): ` ` ` `res ` `+` `=` `1` ` ` ` ` `isOne ` `=` `False` ` ` `for` `i ` `in` `range` `(R ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `if` `(mat[i][j] ` `=` `=` `1` `): ` ` ` `isOne ` `=` `True` ` ` `elif` `(isOne): ` ` ` `res ` `+` `=` `1` ` ` ` ` `return` `res ` ` ` `# Driver code ` `mat ` `=` `[[` `0` `, ` `0` `, ` `0` `, ` `0` `],[` `1` `, ` `0` `, ` `0` `, ` `1` `],[` `0` `, ` `1` `, ` `1` `, ` `0` `],[` `0` `, ` `1` `, ` `0` `, ` `0` `]] ` `print` `(getTotalCoverageOfMatrix(mat)) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to get total coverage ` `// of all zeros in a binary matrix ` `using` `System; ` ` ` `class` `GFG { ` ` ` `static` `int` `R = 4; ` `static` `int` `C = 4; ` ` ` `// Returns total coverage of all zeros in mat[][] ` `static` `int` `getTotalCoverageOfMatrix(` `int` `[,]mat) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// looping for all rows of matrix ` ` ` `for` `(` `int` `i = 0; i < R; i++) ` ` ` `{ ` ` ` `// 1 is not seen yet ` ` ` `bool` `isOne = ` `false` `; ` ` ` ` ` `// looping in columns from left to ` ` ` `// right direction to get left ones ` ` ` `for` `(` `int` `j = 0; j < C; j++) ` ` ` `{ ` ` ` `// If one is found from left ` ` ` `if` `(mat[i,j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` ` ` `// If 0 is found and we ` ` ` `// have found a 1 before. ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` ` ` `// Repeat the above process for ` ` ` `// right to left direction. ` ` ` `isOne = ` `false` `; ` ` ` `for` `(` `int` `j = C-1; j >= 0; j--) ` ` ` `{ ` ` ` `if` `(mat[i,j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Traversing across columms ` ` ` `// for up and down directions. ` ` ` `for` `(` `int` `j = 0; j < C; j++) ` ` ` `{ ` ` ` `// 1 is not seen yet ` ` ` `bool` `isOne = ` `false` `; ` ` ` `for` `(` `int` `i = 0; i < R; i++) ` ` ` `{ ` ` ` `if` `(mat[i,j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` ` ` `isOne = ` `false` `; ` ` ` `for` `(` `int` `i = R-1; i >= 0; i--) ` ` ` `{ ` ` ` `if` `(mat[i,j] == 1) ` ` ` `isOne = ` `true` `; ` ` ` `else` `if` `(isOne) ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code to test above methods ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `[,]mat = {{0, 0, 0, 0}, ` ` ` `{1, 0, 0, 1}, ` ` ` `{0, 1, 1, 0}, ` ` ` `{0, 1, 0, 0}}; ` ` ` ` ` `Console.WriteLine(getTotalCoverageOfMatrix(mat)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

**Output:**

20

**Time Complexity:** O(n^{2})

**Auxiliary Space:** O(1)

This article is contributed by **Utkarsh Trivedi**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Total number of decreasing paths in a matrix
- Total number of cells covered in a matrix after D days
- Count zeros in a row wise and column wise sorted matrix
- Program to check if a matrix is Binary matrix or not
- Nearest 1 in a binary matrix
- Count all 0s which are blocked by 1s in binary matrix
- Unique cells in a binary matrix
- Counting sets of 1s and 0s in a binary matrix
- Distance of nearest cell having 1 in a binary matrix
- Find if there is a rectangle in binary matrix with corners as 1
- Maximum decimal value path in a binary matrix
- Maximum size rectangle binary sub-matrix with all 1s
- Construct Ancestor Matrix from a Given Binary Tree
- Maximize the binary matrix by filpping submatrix once
- Find duplicate rows in a binary matrix
- Minimum steps to get 1 at the center of a binary matrix
- Find if a binary matrix exists with given row and column sums
- Find perimeter of shapes formed with 1s in binary matrix
- Largest Square in a Binary Matrix with at most K 1s for multiple Queries
- Check horizontal and vertical symmetry in binary matrix