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

#BS0000446. Bản đồ mới của Valiant (Valiant's New Map)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Bản đồ mới của Valiant (Valiant's New Map)

Valiant's New Map

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

DbZ Games is designing a map named Panvel for Valiant, inspired by Mumbai. The city is represented by a grid with nn rows and mm columns. The building at row ii, column jj has height ai,ja_{i,j}.

The studio wants a square region of ll consecutive rows and ll consecutive columns. Every building in this region must have height at least ll. The square is aligned with the rows and columns of the grid.

Find the greatest side length ll of a valid square.

Input

  • The first line contains integers n,mn,m, the row and column counts.
  • Each of the next nn lines contains mm integers ai,1,ai,2,…,ai,ma_{i,1},a_{i,2},\ldots,a_{i,m}, the building heights in row ii.

Output

Print one integer, the maximum side length of a valid square.

Subtasks

  • Subtask 1 (20 points): 1≤n≤m1\le n\le m; 1≤nm≤1001\le nm\le 100; 1≤ai,j≤10000001\le a_{i,j}\le 1000000 for all 1≤i≤n1\le i\le n, 1≤j≤m1\le j\le m.
  • Subtask 2 (30 points): 1≤n≤m1\le n\le m; 1≤nm≤10000001\le nm\le 1000000; 1≤ai,j≤101\le a_{i,j}\le 10 for all 1≤i≤n1\le i\le n, 1≤j≤m1\le j\le m.
  • Subtask 3 (50 points): 1≤n≤m1\le n\le m; 1≤nm≤10000001\le nm\le 1000000; 1≤ai,j≤10000001\le a_{i,j}\le 1000000 for all 1≤i≤n1\le i\le n, 1≤j≤m1\le j\le m.

Examples

Example 1

Input

2 2
2 3
4 5

Output

2

Explanation

The whole grid has side 2 and every height is at least 2. A larger square is impossible with two rows.

Example 2

Input

1 3
1 2 3

Output

1

Explanation

There is only one row, so the side is at most 1. The first cell has height 1 and qualifies.

Example 3

Input

2 3
4 4 3
2 1 4

Output

1

Explanation

Both squares of side 2 contain the height-1 cell in row 2, column 2. Neither qualifies; every cell qualifies for side 1.

Example 4

Input

5 6
1 9 4 6 5 8
10 9 5 8 11 6
24 42 32 8 11 1
23 1 9 69 13 3
13 22 60 12 14 17

Output

3

Explanation

Choose rows 1–3 and columns 2–4: the nine heights are 9, 4, 6, 9, 5, 8, 42, 32, 8, with minimum 4. Every side-4 square starting in column 1 or 2 contains the height-1 cell at row 4 column 2; starting in column 3 includes the height-1 cell at row 3 column 6. Side 5 is also impossible since it contains a side-4 square.