PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000339. GCD lớn nhất trên lưới (GCD on a Grid)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

GCD lớn nhất trên lưới (GCD on a Grid)

GCD on a Grid

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given an n×mn\times m grid of positive integers, start at cell (1,1)(1,1) and reach (n,m)(n,m) using only moves right or down.

For a path, take the GCD of all grid values visited, including both endpoints. Find the maximum GCD attainable over all valid paths.

Each PHOJ file contains one grid, with no initial test count.

Input

The first line contains n,mn,m, followed by nn lines of mm positive integers ai,ja_{i,j}.

Output

Print the maximum possible path GCD.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤51\le n,m\le5, 1≤ai,j≤1001\le a_{i,j}\le100.

  • Subtask 2 (30 points): 1≤n,m≤301\le n,m\le30, 1≤ai,j≤1041\le a_{i,j}\le10^4.

  • Subtask 3 (50 points): 1≤n,m≤1001\le n,m\le100, 1≤ai,j≤1061\le a_{i,j}\le10^6.

Examples

Example 1

Input

2 3
30 20 30
15 25 40

Output

10

Explanation

Move right twice and then down: the values 30, 20, 30, 40 have gcd 10. No path yields a greater gcd.

Example 2

Input

3 3
12 4 9
3 12 2
8 3 12

Output

3

Explanation

The path 12→3→12→3→12 has gcd 3. Despite equal endpoints, no full path is divisible by 4, 6, or 12.