题目描述
Problem Background
"Seeking the greatest common divisor is the essence of people's democracy...."
Early autumn, the sunlight dripping from the branches is gentle, splashing on the window frames of the classroom, moistening the cheeks of the morning-reading girl.
"A Ling, A Ling," Tian Yi bends down, her pigtails drooping along the edge of the upright textbook, "what is our greatest common divisor?"
"It must be quite large," her left hand stealthily pinches Tian Yi's arm, "for example, there is a common factor called 'you like me, and I like you.'"
Problem Description
Conversely, in a diverse social circle, the greatest common divisor is pitifully small, making it seem difficult to maintain one's individuality without becoming dull.
Now, abstract the social circle into a $n \times m$ rectangle, where each person's initial individuality is $a_{i,j}$. Starting from the second day, each person will establish relationships with the people above, below, left, and right (if they exist), and their individuality will become the greatest common divisor of their own and their neighbors' personalities from the previous day. For the person at the $x$-th row and $y$-th column, in how many days will their individuality become $1$?
Simplified Problem Statement
There is a matrix $a$ of size $n \times m$. A transformation of the matrix is defined as changing all elements within the matrix to the greatest common divisor of themselves and their four neighboring elements (if they exist). Query how many minimum transformations are needed for $a_{x,y}$ to become $1$. If it is possible for $a_{x,y}$ to become $1$ through several transformations, output the smallest number of transformations; otherwise, output $-1$.
Input Format
The first line contains two integers $n, m$, representing the number of rows and columns of the matrix.
The next $n$ lines, each containing $m$ integers, describe the initial individuality of each person at position $a_{i,j}$.
The next line contains two integers $x, y$, indicating the position of a specified person.
Output Format
Output a single integer $d$, which represents the number of days after which the individuality of the person at the $x$-th row and $y$-th column will become $1$. Specifically, if it will never become $1$, output $-1$.
Sample Input and Output
Sample Input #1
2 2
2 2
1 2
2 1
Sample Output #1
0
Sample Input #2
2 2
2 2
2 2
1 1
Sample Output #2
-1
Sample Input #3
3 3
3 2 3
2 3 2
3 2 3
2 2
Sample Output #3
1
Notes
Sample Explanation 3
The individuality matrix on the first day (which is the initial matrix) is
$$
\begin{pmatrix}
3&2&3\\
2&3&2\\
3&2&3
\end{pmatrix}
$$
The individuality matrix on the second day is
$$
\begin{pmatrix}
1&1&1\\
1&1&1\\
1&1&1
\end{pmatrix}
$$
It can be seen that only one day is needed for $a_{2,2}$ to become $1$, so the answer is $1$.
Data Size and Constraints
This problem uses bundled testing.
For $100\%$ of the data, $1 \le n, m \le 2 \times 10^3$, $1 \le a_{i,j} \le 10^{18}$, $1 \le x \le n$, $1 \le y \le m$.
| Subtask | Points | $n, m$ | Special Constraints |
|---|---|---|---|
| 1 | 1 | / | Guaranteed that the personality will never become $1$ |
| 2 | 1 | / | Guaranteed $a_{x,y} = 1$ |
| 3 | 3 | $\le 2$ | / |
| 4 | 10 | $\le 10^2$ | / |
| 5 | 30 | $\le 5 \times 10^2$ | / |
| 6 | 10 | / | Guaranteed $a_{i,j} \le 2$ for all $i, j$ |
| 7 | 10 | / | Guaranteed $x$ and $y$ are both $1$ |
| 8 | 35 | / | / |
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分