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

13

6

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.

_Shubham_Jain_06 | 404 |

maggiiiii | 315 |

Savage_19 | 310 |

ritikdagar0203 | 241 |

thanuvinu94 | 233 |

Bishnu Dev Panda | 810 |

_Shubham_Jain_06 | 715 |

okayboss | 652 |

maggiiiii | 598 |

Savage_19 | 576 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.