# [Solution] Robot and Candies solution codeforces

## Robot and Candies solution codeforces

Polycarp has a rectangular field of 𝑛×𝑚n×m cells (the size of the 𝑛𝑚n⋅m field does not exceed 106106 cells, 𝑚2m≥2), in each cell of which there can be candy. There are 𝑛n rows and 𝑚m columns in the field.

Let’s denote a cell with coordinates 𝑥x vertically and 𝑦y horizontally by (𝑥,𝑦)(x,y). Then the top-left cell will be denoted as (1,1)(1,1), and the bottom-right cell will be denoted as (𝑛,𝑚)(n,m).

If there is candy in the cell, then the cell is marked with the symbol ‘1‘, otherwise — with the symbol ‘0‘.

Polycarp made a Robot that can collect candy. The Robot can move from (𝑥,𝑦)(x,y) either to (𝑥+1,𝑦+1)(x+1,y+1), or to (𝑥+1,𝑦1)(x+1,y−1). If the Robot is in a cell that contains candy, it takes it.

Also read: ATM and Students solution codeforces

While there is at least one candy on the field, the following procedure is executed:

• Polycarp puts the Robot in an arbitrary cell on the topmost row of the field. He himself chooses in which cell to place the Robot. It is allowed to put the Robot in the same cell multiple times.
• The Robot moves across the field and collects candies. He controls the Robot.
• When the Robot leaves the field, Polycarp takes it. If there are still candies left, Polycarp repeats the procedure.

Find the minimum number of times Polycarp needs to put the Robot on the topmost row of the field in order to collect all the candies. It is guaranteed that Polycarp can always collect all the candies.

Input Robot and Candies solution codeforces

The first line of input data contains an integer 𝑡t (1𝑡1041≤t≤104) — the number of input data sets in the test.

Before each input data, there is a blank line in the test. Next is a line that contains integers 𝑛n and 𝑚m (2𝑚2≤m2𝑛𝑚1062≤n⋅m≤106) — field sizes. This is followed by 𝑛n lines, 𝑖i-th of which describes the 𝑖i-th line of the field. Each of them is a string of size 𝑚m characters: the symbol ‘1‘ corresponds to a cell with candy, the symbol ‘0‘ — an empty cell.

Also read: Make Even solution codeforces

It is guaranteed that the sum of 𝑛𝑚n⋅m values for all input data sets in the test does not exceed 106106.

Output Robot and Candies solution codeforces

Print 𝑡t lines, each line should contain the answer to the corresponding set of input data: the minimum number of times Polycarpus needs to put the Robot on the topmost row of the field in order to collect all the candies.

Example

input

Copy
4

2 2
00
00

3 3
100
000
101

4 5
01000
00001
00010
10000

3 3
111
111
111


output Robot and Candies solution codeforces

Copy
0
2
2
4

Note Robot and Candies solution codeforces

In the first set Polycarp may not put the Robot on the field at all, so the answer “0

In the second set, Polycarp will need to place the robot on the field twice. The Robot can collect candies like this: for the first time Polycarp puts the Robot in the cell (1,1)(1,1) and collects candies at the positions (1,1)(1,1) and (3,3)(3,3). The second time Polycarp can again put the Robot in (1,1)(1,1), and then the Robot will move first to (2,2)(2,2), then to (3,1)(3,1) and collect the last candy.

In the fourth set, you can show that the Robot cannot collect all the candies in three passes.