GCD on a grid
Contest: Codeforces Round 938 (Div. 3)
Difficulty rating: 1900
Tags: brute force, dfs and similar, dp, implementation, math, number theory

Not long ago, Egor learned about the Euclidean algorithm for finding the greatest common divisor of two numbers. The greatest common divisor of two numbers $$$a$$$ and $$$b$$$ is the largest number that divides both $$$a$$$ and $$$b$$$ without leaving a remainder. With this knowledge, Egor can solve a problem that he once couldn't.

Vasily has a grid with $$$n$$$ rows and $$$m$$$ columns, and the integer $$${a_i}_j$$$ is located at the intersection of the $$$i$$$-th row and the $$$j$$$-th column. Egor wants to go from the top left corner (at the intersection of the first row and the first column) to the bottom right corner (at the intersection of the last row and the last column) and find the greatest common divisor of all the numbers along the path. He is only allowed to move down and to the right. Egor has written down several paths and obtained different GCD values. He became interested in finding the maximum possible GCD.

Unfortunately, Egor is tired of calculating GCDs, so he asks for your help in finding the maximum GCD of the integers along the path from the top left corner to the bottom right corner of the grid.

Input format:
The first line contains an integer $$$t$$$ ($$$1 \le t \le {10}^{4}$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 100$$$) — the number of rows and columns of the grid.

Then, there are $$$n$$$ lines, where the $$$i$$$-th line contains $$$m$$$ integers $$$(1 \le a_{i,j} \le {10}^{6}$$$) — the integers written in the $$$i$$$-th row and the $$$j$$$-th column of the grid.

It is guaranteed that the sum of $$$n \cdot m$$$ does not exceed $$$2 \cdot {10}^{5}$$$ over all test cases.

Output format:
For each test case, output the maximum possible GCD along the path from the top left cell to the bottom right cell in a separate line.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Example input:
3
2 3
30 20 30
15 25 40
3 3
12 4 9
3 12 2
8 3 12
2 4
2 4 6 8
1 3 6 9

Example output:
10
3
1

Editorial:
First, let's learn how to check for a fixed $$$x$$$ if there exists a path from $$$(1,1)$$$ to $$$(n,m)$$$ with a GCD of $$$x$$$. It is necessary for all numbers along the path to be divisible by $$$x$$$. Let's define a grid $$$b$$$ of size $$$n$$$ by $$$m$$$, where $$$b_{i,j} = 1$$$ if $$$a_{i,j}$$$ is divisible by $$$x$$$, and $$$b_{i,j} = 0$$$ otherwise. If there exists a path of ones in $$$b$$$, then there exists a path in $$$a$$$ with a GCD of $$$x$$$.
To check if there exists a path consisting entirely of ones, dynamic programming can be used. Let $$$dp_{i, j}$$$ denote whether it is possible to reach $$$(i,j)$$$ from $$$(1,1)$$$. Then the transitions are $$$dp_{i,j} = dp_{i - 1,j} \lor dp_{i,j - 1}$$$, with the base case of the dynamic programming being $$$dp_{1,1} = b_{1,1}$$$.
Since the path will definitely pass through the cells $$$(1,1)$$$ and $$$(n,m)$$$, we iterate through all divisors of the number $$$g = \gcd(a_{1,1}, a_{n,m})$$$, check for each one if there exists a path with that GCD, and take the maximum such divisor. The complexity of the solution is $$$O(n \cdot m \cdot \sqrt[3]{A})$$$, where $$$A \le 10^6$$$.