Bulbs

Find the fewest laser shots needed so every bulb is on, where each shot also toggles bulbs below it and to its left.

Medium5GreedyImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Greg has an m×nm \times n grid of lightbulbs that he wants to turn on. Initially, some of the bulbs are on and some are off. Greg can toggle bulbs by shooting his laser at them. When he shoots a bulb, that bulb switches between on and off. The shot also toggles every bulb below it in the same column and every bulb to its left in the same row.

Find the smallest number of shots Greg needs to turn all the bulbs on.

Input

The first line contains a single integer TT (1T101 \le T \le 10), the number of test cases. Each test case starts with a line containing two space-separated integers mm and nn (1m,n4001 \le m, n \le 400). Each of the next mm lines is a string of length nn made of 1s and 0s. A 1 is a bulb that is on, and a 0 is a bulb that is off.

Output

For each test case, print on its own line the minimum number of shots Greg needs to turn on all the bulbs.

Hint

In the first test case, one shot at the top right bulb turns on every bulb that is off and does not toggle any bulb that is on.

In the second test case, shooting the top left and top right bulbs is enough.