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.

agness | 122 |

aman1931998 | 102 |

Survivor | 100 |

uchitnitk | 96 |

lonecoder | 95 |

KshatriyaYash | 1066 |

nikhil_sojan | 1032 |

lonecoder | 1030 |

mazumderrohit8 | 899 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4492 |

Quandray | 4444 |

Login to report an issue on this page.