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.

cs_abhi | 62 |

kya_bolti_public | 56 |

Shubhankar Sharma | 44 |

vermaankush14291 | 42 |

coderquill | 40 |

PiyushPandey4 | 856 |

john_wick | 757 |

ASWATHAMA | 565 |

akhyasharma01 | 547 |

UsfShilpa | 536 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.