PivotOJ

Zemljište

시간 제한: 2000ms메모리 제한: 512MB출처: COCI 2021-2022BOJ 25040

문제

Matej is, as is well known, the biggest Croatian innovator and businessman. His company is expanding, which is why he decided to buy a plot of land near Velika Gorica. The available land is a rectangle-shaped region consisting of r×sr \times s square unit cells. Each cell has its own price and it’s not possible to purchase only a part of a cell. Matej is an experienced businessman and he knows that the key to success is not simply to buy the biggest plot of land or the least expensive one. Instead, he should buy a plot of land whose price is as close as possible to the magic numbers given to him by Milan the psychic.

At the beginning of his career, Milan revealed to Matej two magic numbers aa and bb crucial for commercial success. Therefore, Matej wishes to buy a (nonempty) rectangular plot of land so that the distance between its price and the magic numbers is as small as possible. The distance between the price and a single magic number is just the absolute value of their difference, and the distance between the price and both of the magic numbers is the sum of these absolute differences. Help Matej and determine the minimum possible distance between the price of the plot of land and the two magic numbers.

입력

The first line contains positive integers rr, ss, aa and bb (1 ≤ r, s ≤ 500, 1 ≤ a, b ≤ 10^9) from the problem.

The ii-th of the following rr lines contains a sequence of s positive integers cijc_{ij} (1 ≤ c_{ij} ≤ 10^9), the prices of the individual cells, in order.

출력

In the only line print the minimum possible distance between the price of the plot of land and the two magic numbers.

힌트

Clarification of the second example: Matej can buy a plot of land consisting of two adjacent cells of cost 11. The total price is then 1+1=21 + 1 = 2, and the distance between it and the magic numbers is 32+42=3|3 - 2| + |4 - 2| = 3.

예제

예제 1

입력
2 2 10 10
1 3
4 1
출력
2

예제 2

입력
3 2 3 4
1 9
1 1
8 1
출력
3

예제 3

입력
3 4 5 3
1 1 1 1
9 6 7 6
8 1 9 7
출력
2
코드를 제출하려면 로그인하세요.