Amusement Park

Time limit2sMemory limit128 MB

Problem

Haein is separated from friends in an amusement park arranged as an N x M grid. The upper-left cell is (1, 1), and the lower-right cell is (N, M).

Each cell (i, j) contains a positive integer Cij, and passing through that cell once costs 1/Cij. From the current cell, Haein can move instantly to an adjacent cell up, down, left, or right, so movement itself takes no time.

Time is divided into one-minute intervals: minute 0 to minute 1, minute 1 to minute 2, minute 2 to minute 3, and so on. During one interval, the total cost of all cells Haein passes through in that interval must be at most 1. When a new interval begins, the cost sum is reset, and the current cell where the new interval starts is counted again.

Given the start position (Sx, Sy), the destination (Dx, Dy), and the matrix C, find the minimum time needed for Haein to reach the destination from the start.

Input

The first line contains the number of test cases T.

For each test case, the first line contains the grid size N and M. (1 <= N, M <= 50)

The next N lines contain the matrix C. Each line consists of M digits without spaces, and the j-th digit is Cij for that row.

The last line contains four integers Sx, Sy, Dx, Dy. (1 <= Sx, Dx <= N, 1 <= Sy, Dy <= M)

Output

For each test case, print the minimum time needed to reach the destination from the start. If the destination cannot be reached, print -1. If the start is already the destination, print 0.