#include <bits/stdc++.h>
using namespace std;
#define int long long
//#define ll int
typedef long long ll;
const int MAXN = 507;
int a[MAXN][MAXN], pre[MAXN][MAXN], n, m, A, B, ans = INT_MAX;

void precompute(){
	cin >> n >> m  >> A >> B;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) cin >> a[i][j];
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++) pre[i][j] = pre[i][j - 1] + a[i][j] + pre[i - 1][j] - pre[i - 1][j - 1];
}

ll get2(int x, int y, int u, int v){
	return pre[u][v] - pre[x - 1][v] - pre[u][y - 1] + pre[x - 1][y - 1];
}

ll get1(vector <int> &pre, int L, int R){
	return pre[R] - pre[L - 1];
}

signed main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	freopen("hub03.inp", "r", stdin);
	freopen("hub03.out", "w", stdout);
	precompute();
	for(int x = 1; x <= n; x++){
		for(int u = x; u <= n; u++){
		    vector <int> pre2(m + 1, 0);
			for(int v = 1; v <= m; v++)pre2[v] = get2(x, 1, u, v);
			int L = 1;
			for(int R = 1; R <= m; R++){
				while(pre2[R] - pre2[L - 1] > A and L < R) L++;
				int S1 = pre2[R] - pre2[L - 1];
				ans = min(ans, abs(A - S1) + abs(B - S1));
				if(L - 1){
					int S2 = pre2[R] - pre2[L - 2];
					ans = min(ans, abs(A - S2) + abs(B - S2));
				}
			}
		}
	}
	cout << ans;
}
