Given a matrix of order NXM, composed of only 0's and 1's, find the number of 1's in the matrix that are surrounded by an even number (>0) of 0's. The surrounding of a cell in the matrix is defined as the elements above, below, on left, on right as well as the 4 diagonal elements around the cell of the matrix. Hence, the surrounding of any matrix elements is composed of at max 8 elements. Print the number of such 1's as output on a new line.

**Input:**

First line of input is an integer T, denoting the number of test cases. For each test case, first line comprises of two space-separated elements N and M, denoting no. of rows and columns in the matrix. The second line of test case comprises of space separated elements of the matrix in 0's and 1's, in row-wise order.

**Output:**

The only line of output for each test case is an integer denoting the number 1's that are surrounded by an even number of 0's (It should be an even number greater than zero).

**Constraints:**

1<=T<=100

1<=N,M<=5

**Example:**

**Input:**

3

3 3

1 0 0 1 1 0 0 1 0

1 1

1

2 4

1 1 0 1 0 0 1 0

**Output:**

1

0

2

**Explanation:**

In the first test case, 1 that occurs in the 1st row and 1st column, has 3 surrounding elements 0,1 and 1. The occurrence of zero is odd. 1 that occurs in 2nd row and 1st column has 5 surrounding elements 1,0,1,1 and 0. The occurrence of zero is even. 1 that occurs in 2nd row and 2nd column has 8 surrounding elements. The occurrence of 0 is odd. Similarly, for the 1 that occurs in 3rd row and 2nd column, the occurrence of zero in it's 5 surrounding elements is odd. Hence, the output is 1.

In the second test case, there is only 1 element in the matrix. Hence, it has no surroundings, so it's count for even 0's is 0 for the whole matrix. 0 is even but we want occurrence of a zero in the surrounding at least once. Hence, output is 0.

Similarly, for the third test case, 1 at 1st row, 1st column and 1 at 1st row, 4th column have even number of 0's surrounding them. hence output is 2.

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.

akhyasharma01 | 467 |

ASWATHAMA | 399 |

john_wick | 360 |

mmzeineddine | 295 |

PiyushPandey4 | 271 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

rahulnishad96 | 442 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.