#P1933F. Turtle Mission: Robot and the Earthquake

    ID: 9006 Type: RemoteJudge 3000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>dfs and similardpgraphsshortest paths

Turtle Mission: Robot and the Earthquake

No submission language available for this problem.

Description

The world is a grid with nn rows and mm columns. The rows are numbered 0,1,,n10, 1, \ldots, n-1, while the columns are numbered 0,1,,m10, 1, \ldots, m-1. In this world, the columns are cyclic (i.e. the top and the bottom cells in each column are adjacent). The cell on the ii-th row and the jj-th column (0i<n,0j<m0 \le i < n, 0 \le j < m) is denoted as (i,j)(i,j).

At time 00, the cell (i,j)(i,j) (where 0i<n,0j<m0 \le i < n, 0 \le j < m) contains either a rock or nothing. The state of cell (i,j)(i,j) can be described using the integer ai,ja_{i,j}:

  • If ai,j=1a_{i,j} = 1, there is a rock at (i,j)(i,j).
  • If ai,j=0a_{i,j} = 0, there is nothing at (i,j)(i,j).

As a result of aftershocks from the earthquake, the columns follow tectonic plate movements: each column moves cyclically upwards at a velocity of 11 cell per unit of time. Formally, for some 0i<n,0j<m0 \le i < n, 0 \le j < m, if (i,j)(i,j) contains a rock at the moment, it will move from (i,j)(i, j) to (i1,j)(i - 1, j) (or to (n1,j)(n - 1, j) if i=0i=0).

The robot called RT is initially positioned at (0,0)(0,0). It has to go to (n1,m1)(n-1,m-1) to carry out an earthquake rescue operation (to the bottom rightmost cell). The earthquake doesn't change the position of the robot, they only change the position of rocks in the world.

Let RT's current position be (x,y)(x,y) (0x<n,0y<m0 \le x < n, 0 \le y < m), it can perform the following operations:

  • Go one cell cyclically upwards, i.e. from (x,y)(x,y) to ((x+n1)modn,y)((x+n-1) \bmod n, y) using 11 unit of time.
  • Go one cell cyclically downwards, i.e. (x,y)(x,y) to ((x+1)modn,y)((x+1) \bmod n, y) using 11 unit of time.
  • Go one cell to the right, i.e. (x,y)(x,y) to (x,y+1)(x, y+1) using 11 unit of time. (RT may perform this operation only if y<m1y < m-1.)

Note that RT cannot go left using the operations nor can he stay at a position.

Unfortunately, RT will explode upon colliding with a rock. As such, when RT is at (x,y)(x,y) and there is a rock at ((x+1)modn,y)((x+1) \bmod n, y) or ((x+2)modn,y)((x+2) \bmod n, y), RT cannot move down or it will be hit by the rock.

Similarly, if y+1<my+1 < m and there is a rock at ((x+1)modn,y+1)((x+1) \bmod n, y+1), RT cannot move right or it will be hit by the rock.

However, it is worth noting that if there is a rock at (xmodn,y+1)(x \bmod n, y+1) and ((x+1)modn,y)((x+1) \bmod n, y), RT can still move right safely.

Find the minimum amount of time RT needs to reach (n1,m1)(n-1,m-1) without colliding with any rocks. If it is impossible to do so, output 1-1.

The first line of the input contains one integer tt (1t1041 \le t \le 10^4) — the number of test cases.

In each test case, the first line contains two integers nn, mm (3n,m1033 \le n, m \le 10^3) — the size of the planet's boundaries.

Each of the next nn lines contains mm integers. The (j+1)(j+1)-th integer on the (i+1)(i+1)-th line (0i<n,0j<m0 \le i < n, 0 \le j < m) is ai,ja_{i,j} (0ai,j10 \le a_{i,j} \le 1), which denotes whether or not there is a rock at (i,j)(i,j) at time 00.

Additionally, it is guaranteed that a0,0=0a_{0,0} = 0, and ai,m1=0a_{i, m-1} = 0 for 0i<n0 \le i < n. In other words, there is no rock at RT's initial position as well as column m1m-1.

The sum of nmn \cdot m over all test cases does not exceed 10610^6.

For each test case:

  • If the destination can be reached without colliding with any rocks, output a single integer — the minimum amount of time RT needs to reach (n1,m1)(n-1,m-1).
  • Otherwise, output 1-1.

Input

The first line of the input contains one integer tt (1t1041 \le t \le 10^4) — the number of test cases.

In each test case, the first line contains two integers nn, mm (3n,m1033 \le n, m \le 10^3) — the size of the planet's boundaries.

Each of the next nn lines contains mm integers. The (j+1)(j+1)-th integer on the (i+1)(i+1)-th line (0i<n,0j<m0 \le i < n, 0 \le j < m) is ai,ja_{i,j} (0ai,j10 \le a_{i,j} \le 1), which denotes whether or not there is a rock at (i,j)(i,j) at time 00.

Additionally, it is guaranteed that a0,0=0a_{0,0} = 0, and ai,m1=0a_{i, m-1} = 0 for 0i<n0 \le i < n. In other words, there is no rock at RT's initial position as well as column m1m-1.

The sum of nmn \cdot m over all test cases does not exceed 10610^6.

Output

For each test case:

  • If the destination can be reached without colliding with any rocks, output a single integer — the minimum amount of time RT needs to reach (n1,m1)(n-1,m-1).
  • Otherwise, output 1-1.

Sample Input 1

6
4 5
0 1 0 0 0
0 0 1 0 0
1 0 1 1 0
0 0 0 0 0
3 3
0 0 0
1 0 0
0 0 0
5 3
0 0 0
0 0 0
1 0 0
0 0 0
1 0 0
3 7
0 0 1 0 0 1 0
1 0 1 0 1 0 0
0 1 0 0 0 0 0
3 4
0 1 0 0
1 0 0 0
0 1 1 0
5 5
0 0 0 0 0
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0

Sample Output 1

7
3
3
8
-1
12

Sample Input 2

6
3 3
0 0 0
0 0 0
0 0 0
4 3
0 1 0
1 0 0
0 1 0
1 0 0
4 3
0 1 0
0 1 0
0 1 0
0 1 0
3 3
0 0 0
1 1 0
0 0 0
3 3
0 1 0
0 0 0
0 1 0
5 5
0 0 0 0 0
0 1 1 0 0
0 1 1 0 0
0 0 0 0 0
0 0 1 0 0

Sample Output 2

3
3
-1
-1
3
8

Note

Visual explanation of the first test case in the example: