#P2716. 和谐的雪花

和谐的雪花

Description

There are n×mn\times m snowflakes placed in an n×mn\times m matrix. Each snowflake has a beauty value. The harmony of a square is defined as the difference between the maximum beauty value and the minimum beauty value in that square. The greater the harmony value, the more harmonious the square is. Given this matrix and a non-negative integer kk, zzs and zzy want you to tell them the smallest side length among all squares with harmony at least kk (that is, find the smallest side length aa such that there exists a square of side length aa whose harmony is at least kk). If there is no solution, output 1-1.

Input Format

The first line contains two positive integers and a non-negative integer, n,mn, m and kk. The next nn lines each contain mm non-negative integers, representing the matrix.

Output Format

If there is a solution, output a single positive integer representing the answer. If there is no solution, output 1-1.

3 5 7
3 4 2 8 7
6 5 2 4 6
3 1 4 0 9
2

Hint

Constraints

  • For 20%20\% of the testdata, 1n,m201 \le n, m \le 20.
  • For 100%100\% of the testdata, 1n,m5001 \le n, m \le 500.
  • For 100%100\% of the testdata, all numbers in the matrix do not exceed 100000100000.

Translated by ChatGPT 5