Coverage of all Zeros in a Binary Matrix
Submissions: 634   Accuracy:

40.84%

  Difficulty: Easy   Marks: 2

Given a binary matrix (M[][]) having n rows and m columns, your task is to find the sum of coverage of all zeros in 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.

Examples:

Input : M[][] =  0 0 0 0
                 1 0 0 1
                 0 1 1 0
                 0 1 0 0
Output : 13
First and last zeros are surrounded by only one 1's each.
Zeros in second row are surrounded by two 1's each.
Similarly counting for others, we get total count as
1 + 1 + 2 + 2 + 2 + 2 + 1 + 2 = 13

Input : M[][] =  1 1 1 0
                 1 0 0 1
Output : 6
Coverage of first zero is 2
Coverages of other two zeros is 2
Total coverage = 2 + 2 + 2 = 6


Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two space separated integer n,m denoting no of rows, no of columns of the matrix  M respectively. Then in the next line are n*m space separated values of the matrix M.

Output:
For each test case in a new line print the required output.

Constraints:
1<=T<=100
1 <= n,m <= 20

Example:
Input:

2
4 4
0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 0
2 4
1 1 1 0 1 0 0 1
Output:
13
6

** For More Input/Output Examples Use 'Expected Output' option **

Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.