#include <bits/stdc++.h>
#define maxn 1000006

using namespace std;

int N,Dp[maxn],T[maxn],T2[maxn],ult1,ult2,sol,cantX,P,arr[maxn],cont1,cont2,pos,pes,ini,fin;
char S[maxn];

bool cumpl(int d)
{
  int pos1,pos2,pos22;
  bool b=0;
  Dp[0]=1;
  for(int i=1;i<=N;i++){
    if(S[i]=='.')
      Dp[i]=Dp[i-1];
    else
       if(S[i]=='*'){
         pos=lower_bound(arr+1,arr+1+P,i-d)-arr;
         pos2=lower_bound(arr+1,arr+1+P,i)-arr;
         pos2--;
         pos1=pos;
         pos22=arr[pos2-1];
         pos2=arr[pos2];
         pos1=arr[pos1];
         if(pos1>i)
            goto tab;
         if(Dp[pos1-1]){
            Dp[i]=1;
            goto tab;
           }
         if(pos1==pos2)
            goto tab;
         if(pos2-d-1 <=0 ){
            Dp[i]=1;
            goto tab;
           }
         pos1=arr[pos+1];
         Dp[i]=( T[pos22-1]-T[pos1-d-2] != 0);
        }
       else
         if(S[i]=='P'){
            if((i-d-1)<=0)
               Dp[i]=1;
            else
               Dp[i]=(T[i-1]-T[i-d-2]!=0);
         }
     tab:
     T[i]=T[i-1]+Dp[i];
    }

  if(Dp[N])
        b=1;

  for(int i=1;i<=N;i++)
    Dp[i]=T[i]=T2[i]=0;
  return b;
}

int main()
{
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> (S+1);

    for(int i=1;i<=N;i++){
        if(S[i]=='P')
          pos=i,arr[++P]=i;
        if(S[i]=='*')
          cantX++;
      }

    if(P==1)
    {
       for(int i=pos-1;i>=1;i--)
          if(S[i]=='*')
            cont1++,ult1=i;
       for(int i=pos+1;i<=N;i++)
          if(S[i]=='*')
            cont2++,ult2=i;

       if(cont1>cont2)
          sol=pos-ult1,cantX=cont1;
       else
          if(cont1<cont2)
            sol=ult2-pos,cantX=cont2;
          else
            sol=min(pos-ult1,ult2-pos),cantX=cont1;

      goto fin;
    }

    ini=0,fin=N;
    while(ini<=fin)
    {
     int mid=(ini+fin)/2;
     if(cumpl(mid))
        fin=mid-1,sol=mid;
     else
        ini=mid+1;
    }

    fin:
    cout << cantX << " " <<sol << '\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,x[1100001],sum[1100001],dp[1100001],cnt=0;
char s[1100001];
bool none(int l,int r){return l>r||sum[l-1]==sum[r];}
void update(int &a,int b){if(a<b)a=b;}
bool check(int t)
{
	memset(dp,0,sizeof dp);
	for(int i=0;i<cnt;i++)
	{
		if(none(dp[i]+1,x[i]-t-1))update(dp[i+1],x[i]);
		if(none(dp[i]+1,x[i]-1))update(dp[i+1],x[i]+t);
		if(i&&none(dp[i-1]+1,x[i]-t-1))update(dp[i+1],x[i-1]+t);
	}
	return none(dp[cnt]+1,n);
}
int main()
{
	scanf("%d%s",&n,s+1);
	int pos=-1;
	for(int i=1;i<=n;i++)
		if(s[i]=='P')pos=x[cnt++]=i;
	if(cnt==1)
	{
		int L=0,R=0,lpos=-1,rpos=-1;;
		for(int i=pos-1;i;i--)
			if(s[i]=='*')L++,lpos=i;
		for(int i=pos+1;i<=n;i++)
			if(s[i]=='*')R++,rpos=i;
		if(L!=R)printf("%d %d\n",max(L,R),L>R?pos-lpos:rpos-pos);
		else printf("%d %d\n",L,min(pos-lpos,rpos-pos));
		return 0;
	}
	for(int i=1;i<=n;i++)sum[i]=sum[i-1]+(s[i]=='*');
	int l=0,r=n,ans=n;
	while(l<=r)
	{
		int m=(l+r)/2;
		if(check(m))r=m-1,ans=m;
		else l=m+1;
	}
	printf("%d %d\n",sum[n],ans);
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(x) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }
template<class T> T gcd(T a, T b) { re a ? gcd (b % a, a) : b; }
template<class T> T sqr(T a) { re a * a; }
template<class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

#define filename ""

int n;
int m;

const int N = 1e6 + 10;
char s[N];
int p[N], l[N], r[N], f[N][5];

void upd(int & res, int val, int cur = 0)
{
	res = min (res, max (val, cur));
}

int get (int i, int j)
{
	if (i > j) swap (i, j);
	if (j == m) {
		re i == m ? 0 : l[n - 1] - p[i];
	}
	int pos = (p[i] + p[j]) / 2;
	int t1 = max (l[pos] - p[i], p[j] - r[pos + 1]), t2 = 0;
	if (pos > 0) {
		t2 = max (l[pos - 1] - p[i], p[j] - r[pos]);
	}
//	cout << "get " << i << " " << j << endl;
	int res = max (0, min (t1, t2));
	re res;
}

int getr (int i, int j)
{
//	cout << "getr " << i << " " << j << endl;
	int res = max (0, l[max (0, p[j])] - p[i]);
//	cout << res << endl;
	re res;
}

int getl (int i, int j)
{
//	cout << "getl " << i << " " << j << endl;
	int res = max (0, p[j] - r[max (0, p[i])]);
//	cout << res << endl;
	re res;
}

int go (int i, int state)
{
	if (f[i][state - 1] != -1) re f[i][state - 1];
	int res = n;
	switch (state) {
		case 1:
			if (i == m) upd(res, getr(i - 1, m));
			else {
				upd (res, go (i + 1, 4));
				upd (res, go (i + 1, 3), get(i - 1, i));
				upd (res, go (i + 1, 1), getr(i - 1, i));
				if (i < m - 1) upd (res, go (i + 2, 4), get(i - 1, i + 1));
			}
			break;
		case 2:
			if (i == m) res = getl (i - 2, i - 1) > 0 ? n : getr(i - 1, m);
			else {
				upd (res, go (i + 1, 4), getl(i - 2, i));
				if (getl(i - 2, i - 1) == 0) {
					upd (res, go (i + 1, 1), getr(i - 1, i));
					upd (res, go (i + 1, 3), get(i - 1, i));
					if (i < m - 1) upd (res, go (i + 2, 4), get(i - 1, i + 1));
				}
			}
			break;
		case 3:
			if (i == m) res = getr (i - 1, m) > 0 ? n : 0;
			else {
				upd (res, go (i + 1, 2));
				upd (res, go (i + 1, 3), getl(i - 1, i));
			}
			break;
		case 4:
			if (i == m) res = getr (i - 2, m);
			else {
				upd (res, go (i + 1, 3), get(i - 2, i));
				upd (res, go (i + 1, 1), getr(i - 2, i));
				if (i < m - 1) upd (res, go (i + 2, 4), get(i - 2, i + 1));
			}
			break;
	}
//	cout << i << " " << state << " " << res << endl;
	re f[i][state - 1] = res;
}

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	scanf ("%d ", &n);
	gets (s);
	int pos = -1;
	int ast = 0;
	for (int i = 0; i <= n; i++) {
		if (s[i] == '*') pos = i, ast++;
		l[i] = pos;
	}
	pos = n;
	for (int i = n; i >= 0; i--) {
		if (s[i] == '*') pos = i;
		r[i] = pos;
	}
	p[m++] = -1; p[m++] = -1;
	for (int i = 0; i < n; i++)
		if (s[i] == 'P') p[m++] = i;
	p[m] = n;
	if (m == 3) {
		int pos[2] = {p[2], p[2]}, cnt[2] = {0, 0};
		for (int i = 0; i < n; i++) {
			if (s[i] == '*') cnt[i >= p[2]]++, pos[i >= p[2]] = i < p[2] ? min (pos[i >= p[2]], i) : i;
		}
		if (cnt[0] == cnt[1]) {
			cout << cnt[0] << " " << min (p[2] - pos[0], pos[1] - p[2]) << endl;
		} else if (cnt[0] > cnt[1]) {
			cout << cnt[0] << " " << p[2] - pos[0] << endl;
		} else if (cnt[1] > cnt[0]) {
			cout << cnt[1] << " " << pos[1] - p[2] << endl;
		}
		re 0;
	}
	memset (f, -1, sizeof (f));
	cout << ast << " " << go (2, 3) << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define makeunique(x) sort(all(x)), (x).resize(unique(x) - (x).begin())
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define j0 j5743892
#define j1 j542893

typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }
template<class T> T gcd(T a, T b) { re a ? gcd (b % a, a) : b; }
template<class T> T sqr(T a) { re a * a; }
template<class T> T sgn(T a) { re a > 0 ? 1 : (a < 0 ? -1 : 0); }

#define filename ""

int n;
int m;

const int N = 1e6 + 10;
char s[N];
int p[N], l[N], r[N], f[N][5];

void upd(int & res, int val, int cur = 0)
{
	res = min (res, max (val, cur));
}

int get (int i, int j)
{
	if (i > j) swap (i, j);
	if (j == m) {
		re i == m ? 0 : l[n - 1] - p[i];
	}
	int pos = (p[i] + p[j]) / 2;
	int t1 = max (l[pos] - p[i], p[j] - r[pos + 1]), t2 = 0;
	if (pos > 0) {
		t2 = max (l[pos - 1] - p[i], p[j] - r[pos]);
	}
//	cout << "get " << i << " " << j << endl;
	int res = max (0, min (t1, t2));
	re res;
}

int getr (int i, int j)
{
//	cout << "getr " << i << " " << j << endl;
	int res = max (0, l[max (0, p[j])] - p[i]);
//	cout << res << endl;
	re res;
}

int getl (int i, int j)
{
//	cout << "getl " << i << " " << j << endl;
	int res = max (0, p[j] - r[max (0, p[i])]);
//	cout << res << endl;
	re res;
}

int go (int i, int state)
{
	if (f[i][state - 1] != -1) re f[i][state - 1];
	int res = n;
	switch (state) {
		case 1:
			if (i == m) upd(res, getr(i - 1, m));
			else {
				upd (res, go (i + 1, 4));
				upd (res, go (i + 1, 3), get(i - 1, i));
				upd (res, go (i + 1, 1), getr(i - 1, i));
			}
			break;
		case 2:
			if (i == m) res = getl (i - 2, i - 1) > 0 ? n : getr(i - 1, m);
			else {
				upd (res, go (i + 1, 4), getl(i - 2, i));
				if (getl(i - 2, i - 1) == 0) {
					upd (res, go (i + 1, 1), getr(i - 1, i));
					upd (res, go (i + 1, 3), get(i - 1, i));
				}
			}
			break;
		case 3:
			if (i == m) res = getr (i - 1, m) > 0 ? n : 0;
			else {
				upd (res, go (i + 1, 2));
				upd (res, go (i + 1, 3), getl(i - 1, i));
			}
			break;
		case 4:
			if (i == m) res = getr (i - 2, m);
			else {
				upd (res, go (i + 1, 3), get(i - 2, i));
				upd (res, go (i + 1, 1), getr(i - 2, i));
				if (i < m - 1) upd (res, go (i + 2, 4), get(i - 2, i + 1));
			}
			break;
	}
//	cout << i << " " << state << " " << res << endl;
	re f[i][state - 1] = res;
}

int main () {
//	freopen (filename".in", "r", stdin);
//	freopen (filename".out", "w", stdout);
	scanf ("%d ", &n);
	gets (s);
	int pos = -1;
	int ast = 0;
	for (int i = 0; i <= n; i++) {
		if (s[i] == '*') pos = i, ast++;
		l[i] = pos;
	}
	pos = n;
	for (int i = n; i >= 0; i--) {
		if (s[i] == '*') pos = i;
		r[i] = pos;
	}
	p[m++] = -1; p[m++] = -1;
	for (int i = 0; i < n; i++)
		if (s[i] == 'P') p[m++] = i;
	p[m] = n;
	if (m == 3) {
		int pos[2] = {p[2], p[2]}, cnt[2] = {0, 0};
		for (int i = 0; i < n; i++) {
			if (s[i] == '*') cnt[i >= p[2]]++, pos[i >= p[2]] = i < p[2] ? min (pos[i >= p[2]], i) : i;
		}
		if (cnt[0] == cnt[1]) {
			cout << cnt[0] << " " << min (p[2] - pos[0], pos[1] - p[2]) << endl;
		} else if (cnt[0] > cnt[1]) {
			cout << cnt[0] << " " << p[2] - pos[0] << endl;
		} else if (cnt[1] > cnt[0]) {
			cout << cnt[1] << " " << pos[1] - p[2] << endl;
		}
		re 0;
	}
	memset (f, -1, sizeof (f));
	cout << ast << " " << go (2, 3) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <cstring>

#define forr(i,x,n) for(int i=(x); i<(n); i++)
#define forn(i,n) forr(i,0,n)

using namespace std;

const int MAXN = 1e6 + 10;
vector<int> P, A;

int N;
string S;
int sum[MAXN], dp[MAXN];

int isempty(int x, int y) {
    x = max(x,0);
    return (x>y) ? 1 : !(sum[y] - sum[x]);
}

bool check(int t){
    forn(i,N+1) dp[i] = 0;
    forn(i,P.size()){
        if(isempty(dp[i], P[i]-t))         dp[i+1] = max(dp[i+1], P[i]);
        if(isempty(dp[i], P[i]))           dp[i+1] = max(dp[i+1], P[i]+t);
        if(i and isempty(dp[i-1], P[i]-t)) dp[i+1] = max(dp[i+1], P[i-1]+t);
    }
    //cout << "tiempo: " << t << endl << "dp[i+1]: " << dp[1] << endl << endl;
    return dp[P.size()] >= A.back();
}

int main() {
    //freopen("input.txt", "r", stdin);
    cin >> N;
    cin >> S;

    sum[0] = 0;
    forn(i,N){
        sum[i+1] = sum[i];
        if(S[i] == 'P') P.push_back(i);
        if(S[i] == '*') A.push_back(i), sum[i+1]++;
    }

    if(P.size() == 1) {
        int eat, tim;

        if(sum[P[0]] > sum[N]-sum[P[0]]){
            eat = sum[P[0]];
            tim = max(P[0]-A[0],0);
        } else {
            eat = sum[N]-sum[P[0]];
            tim = max(A.back()-P[0], 0);
        }
        cout << eat << ' ' << tim << endl;

        return 0;
    }

    int l=0, r=N;
    while(l<r){
        int mid = (l + r)/2;
        if(check(mid))  r = mid;
            else        l = mid + 1;
    }

    cout << A.size() << ' ' << l << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <cstring>

#define forr(i,x,n) for(int i=(x); i<(n); i++)
#define forn(i,n) forr(i,0,n)

using namespace std;

const int MAXN = 1e6 + 10;
vector<int> P, A;

int N;
string S;
int sum[MAXN], dp[MAXN];

int isempty(int x, int y) { return (x>y) ? 1 : !(sum[y] - sum[x]); }

bool check(int t){
    forn(i,N+1) dp[i] = 0;
    forn(i,P.size()){
        if(isempty(dp[i], P[i]-t))         dp[i+1] = max(dp[i+1], P[i]);
        if(isempty(dp[i], P[i]))           dp[i+1] = max(dp[i+1], P[i]+t);
        if(i and isempty(dp[i-1], P[i]-t)) dp[i+1] = max(dp[i+1], P[i-1]+t);
    }
    //cout << "tiempo: " << t << endl << "dp[i+1]: " << dp[1] << endl << endl;
    return dp[P.size()] >= A.back();
}

int main() {
    //freopen("input.txt", "r", stdin);
    cin >> N;
    cin >> S;

    sum[0] = 0;
    forn(i,N){
        sum[i+1] = sum[i];
        if(S[i] == 'P') P.push_back(i);
        if(S[i] == '*') A.push_back(i), sum[i+1]++;
    }

    if(P.size() == 1) {
        int eat, tim;

        if(sum[P[0]] > sum[N]-sum[P[0]]){
            eat = sum[P[0]];
            tim = max(P[0]-A[0],0);
        } else {
            eat = sum[N]-sum[P[0]];
            tim = max(A.back()-P[0], 0);
        }
        cout << eat << ' ' << tim << endl;

        return 0;
    }

    int l=0, r=N;
    while(l<r){
        int mid = (l + r)/2;
        if(check(mid))  r = mid;
            else        l = mid + 1;
    }

    cout << A.size() << ' ' << l << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <cstring>

#define forr(i,x,n) for(int i=(x); i<(n); i++)
#define forn(i,n) forr(i,0,n)

using namespace std;

const int MAXN = 1e6 + 10;
vector<int> P, A;

int N;
string S;
int sum[MAXN], dp[MAXN];

int isempty(int x, int y) {
    x = max(x,0);
    return (x>y) ? 1 : !(sum[y] - sum[x]);
}

bool check(int t){
    forn(i,N+1) dp[i] = -1;
    forn(i,P.size()){
        if(isempty(dp[i]+1, P[i]-t))         dp[i+1] = max(dp[i+1], P[i]);
        if(isempty(dp[i]+1, P[i]))           dp[i+1] = max(dp[i+1], P[i]+t);
        if(i and isempty(dp[i-1]+1, P[i]-t)) dp[i+1] = max(dp[i+1], P[i-1]+t);
    }
    //cout << "tiempo: " << t << endl << "dp[i+1]: " << dp[1] << endl << endl;
    return dp[P.size()] >= A.back();
}

int main() {
    //freopen("input.txt", "r", stdin);
    cin >> N;
    cin >> S;

    sum[0] = 0;
    forn(i,N){
        sum[i+1] = sum[i];
        if(S[i] == 'P') P.push_back(i);
        if(S[i] == '*') A.push_back(i), sum[i+1]++;
    }

    if(P.size() == 1) {
        int eat, tim;

        if(sum[P[0]] > sum[N]-sum[P[0]]){
            eat = sum[P[0]];
            tim = max(P[0]-A[0],0);
        } else {
            eat = sum[N]-sum[P[0]];
            tim = max(A.back()-P[0], 0);
        }
        cout << eat << ' ' << tim << endl;

        return 0;
    }

    int l=0, r=N;
    while(l<r){
        int mid = (l + r)/2;
        if(check(mid))  r = mid;
            else        l = mid + 1;
    }

    cout << A.size() << ' ' << l << endl;
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
char s[1000002];
int pos[1000002],sum[1000002],cnt,n;
int dp[1000002];
bool check(int mid)
{
	dp[0]=0;
	//一定要确保每一处sum下标在0到n之间
	for(int i=1;i<=cnt;i++){
		if(dp[i-1]!=-1){
			if(sum[pos[i]]>sum[dp[i-1]]){
				if(pos[i]<=mid||sum[pos[i]-mid-1]<=sum[dp[i-1]])
					dp[i]=pos[i];
				else dp[i]=-1;
			}
			else dp[i]=pos[i]+mid;
		}
		else dp[i]=-1;
		if(i>1&&dp[i-2]!=-1){
			if(pos[i]-pos[i-1]<mid&&(pos[i]<=mid||sum[pos[i]-mid-1]<=sum[dp[i-2]]))
				dp[i]=max(dp[i],pos[i-1]+mid);
		}
		if(dp[i]>n)return true;
	}
	return dp[cnt]!=-1&&sum[dp[cnt]]==sum[n];
}
int main()
{
	scanf("%d%s",&n,s+1);
	for(int i=1;i<=n;i++){
		sum[i]=sum[i-1]+(s[i]=='*');
		if(s[i]=='P')pos[++cnt]=i;
	}
	if(cnt==1){
		int c1=sum[pos[1]],c2=sum[n]-sum[pos[1]];
		int t1=pos[1]-(lower_bound(sum+1,sum+n+1,1)-sum);
		int t2=lower_bound(sum+1,sum+n+1,sum[n])-sum-pos[1];
		if(c1<c2)printf("%d %d",c2,t2);
		else if(c1>c2)printf("%d %d",c1,t1);
		else printf("%d %d",c1,min(t1,t2));
	}
	else{
		int l=0,r=n;
		while(l<r){
			int mid=(l+r)>>1;
			if(check(mid))r=mid;
			else l=mid+1;
		}
		printf("%d %d",sum[n],l);
	}
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define Max(a,b) a>b?a:b
struct node
{
    int v;
    //node (int a,int b):v(b){}
    /*bool operator <(const node a) const
    {
        if (v==a.v)
            return id<a.id;
        return v<a.v;
    }*/
};
int r[1010],w[1010];
int main()
{
    int n,m,k,p;
    priority_queue <int> q;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    int i,j,t;
    memset(r,0,sizeof(r));
    memset(w,0,sizeof(w));
    for (i=1;i<=n;i++) //w代表列，r代表行
    {
        for (j=1;j<=m;j++)
        {
            scanf("%d",&t);
            r[i]+=t;
            w[j]+=t;
        }
    }
    while (!q.empty()) q.pop();
    for (i=1;i<=n;i++)
    {
        q.push(r[i]);
        r[i]=0;
    }
    for (i=1;i<=k;i++)
    {
        int h=q.top();
        q.pop();
        r[i]=r[i-1]+h;
        h-=p*m;
        q.push(h);
    }
    while (!q.empty())q.pop();
    for (i=1;i<=m;i++)
    {
        q.push(w[i]);
        w[i]=0;
    }
    for (i=1;i<=k;i++)
    {
        int h=q.top();
        q.pop();
        w[i]=w[i-1]+h;
        h-=p*n;
        q.push(h);
    }
    int ans=-0x3fffffff;
    for (i=0;i<=k;i++)
    {
        ans=Max(ans,r[i]+w[k-i]-i*(k-i)*p);
    }
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
struct node
{
    int id,v;
    node (int a,int b):id(a),v(b){}
    bool operator <(const node a) const
    {
        return v<a.v;
    }
};
int a[1010][1010];
int main()
{
    int n,m,k,p;
    priority_queue <node> q;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    int i,j,t;
    for (i=1;i<=n;i++) //0代表列，1代表行
    {
        t=0;
        for (j=1;j<=m;j++)
        {
            scanf("%d",&a[i][j]);
            t+=a[i][j];
        }
        q.push(node(1,t));
    }
    for (i=1;i<=m;i++)
    {
        t=0;
        for (j=1;j<=n;j++)
            t+=a[j][i];
        q.push(node(0,t));
    }
    long long ans=0,r=0,w=0;
    while (k--)
    {
        node h=q.top();
        q.pop();
        ans+=h.v;
        if (h.id==0)
        {
            w++;
            h.v-=n*p;
        }
        else
        {
            r++;
            h.v-=m*p;
        }
        q.push(h);
    }
    ans=ans-w*r*p;
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
struct node
{
    __int64 id,v;
    node (__int64 a,__int64 b):id(a),v(b){}
    bool operator <(const node a) const
    {
        if (v==a.v)
            return id<a.id;
        return v<a.v;
    }
};
int a[1010][1010];
int main()
{
    __int64 n,m,k,p;
    priority_queue <node> q;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);
    __int64 i,j,t;
    for (i=1;i<=n;i++) //0代表列，1代表行
    {
        t=0;
        for (j=1;j<=m;j++)
        {
            scanf("%I64d",&a[i][j]);
            t+=a[i][j];
        }
        q.push(node(1,t));
    }
    for (i=1;i<=m;i++)
    {
        t=0;
        for (j=1;j<=n;j++)
            t+=a[j][i];
        q.push(node(0,t));
    }
    __int64 ans=0,r=0,w=0;
    while (k--)
    {
        node h=q.top();
        q.pop();
        ans+=h.v;
        if (h.id==0)
        {
            w++;
            h.v-=n*p;
        }
        else
        {
            r++;
            h.v-=m*p;
        }
        q.push(h);
    }
    ans=ans-w*r*p;
    printf("%I64d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
struct node
{
    int id,v;
    node (int a,int b):id(a),v(b){}
    bool operator <(const node a) const
    {
        return v<a.v;
    }
};
int a[1010][1010];
int main()
{
    int n,m,k,p;
    priority_queue <node> q;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    int i,j,t;
    for (i=1;i<=n;i++) //0代表列，1代表行
    {
        t=0;
        for (j=1;j<=m;j++)
        {
            scanf("%d",&a[i][j]);
            t+=a[i][j];
        }
        q.push(node(1,t));
    }
    for (i=1;i<=m;i++)
    {
        t=0;
        for (j=1;j<=n;j++)
            t+=a[j][i];
        q.push(node(0,t));
    }
    int ans=0,r=0,w=0;
    while (k--)
    {
        node h=q.top();
        q.pop();
        ans+=h.v;
        if (h.id==0)
        {
            w++;
            h.v-=n*p;
        }
        else
        {
            r++;
            h.v-=m*p;
        }
        q.push(h);
    }
    ans=ans-w*r*p;
    cout<<ans<<endl;
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <queue>
#include <ctime>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

const int N=3e6,INF=1e9;

int j,n,m,k,i;
long long ans,x,a[N],b[N],fa[N],fb[N],p,ta,tb;
priority_queue<long long>qa,qb;

int main() {
    //ios_base::sync_with_stdio(0),cin.tie(0);
    //freopen("input.txt","r",stdin);
    scanf("%d%d%d%lld",&n,&m,&k,&p);
    memset(a,0,sizeof a),memset(b,0,sizeof b);
    for(i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&x),a[i]+=x,b[j]+=x;
    memset(fa,0,sizeof fa),memset(fb,0,sizeof fb);
    for(i=1;i<=n;++i)qa.push(a[i]);for(i=1;i<=m;++i)qb.push(b[i]);
    for(i=1;i<=k;++i)ta=qa.top(),fa[i]=fa[i-1]+ta,qa.pop(),qa.push(ta-p*m);
    for(i=1;i<=k;++i)tb=qb.top(),fb[i]=fb[i-1]+tb,qb.pop(),qb.push(tb-p*n);
    ans=0;for(i=0;i<=k;++i)ans=max(ans,fa[i]+fb[k-i]-(1ll*p*i*(k-i)));
    printf("%lld\n",ans);
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <queue>
#include <ctime>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

const int N=3e6,INF=1e9;

int j,n,m,k,i;
long long ans,x,a[N],b[N],fa[N],fb[N],p,ta,tb;
priority_queue<long long>qa,qb;

int main() {
    //ios_base::sync_with_stdio(0),cin.tie(0);
    //freopen("input.txt","r",stdin);
    scanf("%d%d%d%lld",&n,&m,&k,&p);
    memset(a,0,sizeof a),memset(b,0,sizeof b);
    for(i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&x),a[i]+=x,b[j]+=x;
    memset(fa,0,sizeof fa),memset(fb,0,sizeof fb);
    for(i=1;i<=n;++i)qa.push(a[i]);for(i=1;i<=m;++i)qb.push(b[i]);
    for(i=1;i<=k;++i)ta=qa.top(),fa[i]=fa[i-1]+ta,qa.pop(),qa.push(ta-p*1LL*m);
    for(i=1;i<=k;++i)tb=qb.top(),fb[i]=fb[i-1]+tb,qb.pop(),qb.push(tb-p*1LL*n);
    ans=0;for(i=0;i<=k;++i)ans=max(ans,fa[i]+fb[k-i]-(1LL*p*i*1LL*(k-1LL*i)));
    printf("%lld\n",ans);
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <queue>
#include <ctime>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

const long long N=3e6,INF=(1<<62);

long long j,n,m,k,i;
long long ans,x,a[N],b[N],fa[N],fb[N],p,ta,tb;
priority_queue<long long>qa,qb;

int main() {
    //ios_base::sync_with_stdio(0),cin.tie(0);
    //freopen("input.txt","r",stdin);
    scanf("%lld%lld%lld%lld",&n,&m,&k,&p);
    memset(a,0,sizeof a),memset(b,0,sizeof b);
    for(i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&x),a[i]+=x,b[j]+=x;
    memset(fa,0,sizeof fa),memset(fb,0,sizeof fb);
    for(i=1;i<=n;++i)qa.push(a[i]);for(i=1;i<=m;++i)qb.push(b[i]);
    for(i=1;i<=k;++i)ta=qa.top(),fa[i]=fa[i-1]+ta,qa.pop(),qa.push(ta-1LL*p*1LL*m);
    for(i=1;i<=k;++i)tb=qb.top(),fb[i]=fb[i-1]+tb,qb.pop(),qb.push(tb-1LL*p*1LL*n);
    ans=-(INF);for(i=0;i<=k;++i)ans=max(ans,fa[i]+fb[k-i]-(1LL*p*i*1LL*(k-1LL*i)));
    printf("%lld\n",ans);
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <queue>
#include <ctime>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

const int N=3e5,INF=1e9;

int j,n,m,k,i;
long long ans,x,a[N],b[N],fa[N],fb[N],p,ta,tb;
priority_queue<long long>qa,qb;

int main() {
    //ios_base::sync_with_stdio(0),cin.tie(0);
    //freopen("input.txt","r",stdin);
    scanf("%d%d%d%lld",&n,&m,&k,&p);
    memset(a,0,sizeof a),memset(b,0,sizeof b);
    for(i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&x),a[i]+=x,b[j]+=x;
    memset(fa,0,sizeof fa),memset(fb,0,sizeof fb);
    for(i=1;i<=n;++i)qa.push(a[i]);for(i=1;i<=m;++i)qb.push(b[i]);
    for(i=1;i<=k;++i)ta=qa.top(),fa[i]=fa[i-1]+ta,qa.pop(),qa.push(ta-p*m);
    for(i=1;i<=k;++i)tb=qb.top(),fb[i]=fb[i-1]+tb,qb.pop(),qb.push(tb-p*n);
    ans=0;for(i=0;i<=k;++i)ans=max(ans,fa[i]+fb[k-i]-(1ll*p*i*(k-i)));
    printf("%lld\n",ans);
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <queue>
#include <ctime>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

const long long N=3e6;

long long j,n,m,k,i;
long long ans,x,a[N],b[N],fa[N],fb[N],p,ta,tb;
priority_queue<long long>qa,qb;

int main() {
    //ios_base::sync_with_stdio(0),cin.tie(0);
    //freopen("input.txt","r",stdin);
    scanf("%lld%lld%lld%lld",&n,&m,&k,&p);
    memset(a,0,sizeof a),memset(b,0,sizeof b);
    for(i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&x),a[i]+=x,b[j]+=x;
    memset(fa,0,sizeof fa),memset(fb,0,sizeof fb);
    for(i=1;i<=n;++i)qa.push(a[i]);for(i=1;i<=m;++i)qb.push(b[i]);
    for(i=1;i<=k;++i)ta=qa.top(),fa[i]=fa[i-1]+ta,qa.pop(),qa.push(ta-1LL*p*1LL*m);
    for(i=1;i<=k;++i)tb=qb.top(),fb[i]=fb[i-1]+tb,qb.pop(),qb.push(tb-1LL*p*1LL*n);
    ans=0;for(i=0;i<=k;++i)ans=max(ans,fa[i]+fb[k-i]-(1LL*p*i*1LL*(k-1LL*i)));
    printf("%lld\n",ans);
    return 0;
}


#include <set>
#include <map>
#include <list>
#include <queue>
#include <ctime>
#include <stack>
#include <cmath>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

const long long N=3e6,INF=1e14;

long long j,n,m,k,i;
long long ans,x,a[N],b[N],fa[N],fb[N],p,ta,tb;
priority_queue<long long>qa,qb;

int main() {
    //ios_base::sync_with_stdio(0),cin.tie(0);
    //freopen("input.txt","r",stdin);
    scanf("%lld%lld%lld%lld",&n,&m,&k,&p);
    memset(a,0,sizeof a),memset(b,0,sizeof b);
    for(i=1;i<=n;++i)for(j=1;j<=m;++j)scanf("%lld",&x),a[i]+=x,b[j]+=x;
    memset(fa,0,sizeof fa),memset(fb,0,sizeof fb);
    for(i=1;i<=n;++i)qa.push(a[i]);for(i=1;i<=m;++i)qb.push(b[i]);
    for(i=1;i<=k;++i)ta=qa.top(),fa[i]=fa[i-1]+ta,qa.pop(),qa.push(ta-1LL*p*1LL*m);
    for(i=1;i<=k;++i)tb=qb.top(),fb[i]=fb[i-1]+tb,qb.pop(),qb.push(tb-1LL*p*1LL*n);
    ans=-(INF);for(i=0;i<=k;++i)ans=max(ans,fa[i]+fb[k-i]-(1LL*p*i*1LL*(k-1LL*i)));
    printf("%lld\n",ans);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define INF 0x3f3f3f3f
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn];
LL r[maxn];
LL dpc[maxn];
LL dpr[maxn];
priority_queue<LL> a;

int main()
{
   // freopen("input.txt","r",stdin);
    memset(c,0,sizeof(c));
    memset(r,0,sizeof(r));
    memset(dpr,0,sizeof(dpr));
    memset(dpc,0,sizeof(dpc));
    int n,m,k,p;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-m*p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-n*p);
        }
        LL fin=dpr[0]+dpc[k];
        for(int i=1;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-(LL)i*(LL)(k-i)*(LL)p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define INF 0x3f3f3f3f
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn];
LL r[maxn];
LL dpc[maxn];
LL dpr[maxn];
priority_queue<LL> a;

int main()
{

    memset(c,0,sizeof(c));
    memset(r,0,sizeof(r));
    memset(dpr,0,sizeof(dpr));
    memset(dpc,0,sizeof(dpc));
    int n,m,k,p;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-(LL)m*(LL)p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-(LL)n*(LL)p);
        }
        LL fin=dpr[0]+dpc[k];
        for(int i=1;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-(LL)i*(LL)(k-i)*(LL)p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn];
LL r[maxn];
LL dpc[1000005];
LL dpr[1000005];
priority_queue<LL> a;

int main()
{
    memset(c,0,sizeof(c));
    memset(r,0,sizeof(r));
    memset(dpr,0,sizeof(dpr));
    memset(dpc,0,sizeof(dpc));
    int n,m,k;
    LL p;
    scanf("%d%d%d%I64d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-(LL)m*(LL)p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-(LL)n*(LL)p);
        }
        LL fin=dpr[0]+dpc[k];
        for(int i=1;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-(LL)i*(LL)(k-i)*(LL)p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define INF 0x3f3f3f3f
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn]={0};
LL r[maxn]={0};
LL dpc[maxn]={0};
LL dpr[maxn]={0};
priority_queue<LL> a;

int main()
{

    int n,m,k,p;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-m*p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-n*p);
        }
        LL fin=dpr[0]+dpc[k];
        for(int i=1;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-(LL)i*(LL)(k-i)*(LL)p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
int ma[maxn][maxn];
int c[maxn]={0};
int r[maxn]={0};
int dpc[maxn]={0};
int dpr[maxn]={0};
priority_queue<int> a;

int main()
{

    int n,m,k,p;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        int temp=0;
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            int now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-m*p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            int now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-n*p);
        }
        int fin=0;
        for(int i=0;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-i*(k-i)*p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define INF 0x3f3f3f3f
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn]={0};
LL r[maxn]={0};
LL dpc[maxn]={0};
LL dpr[maxn]={0};
priority_queue<LL> a;

int main()
{

    int n,m,k,p;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-m*p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            int now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-n*p);
        }
        LL fin=-INF;
        for(int i=0;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-i*(k-i)*p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define INF 0x3f3f3f3f
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn]={0};
LL r[maxn]={0};
LL dpc[maxn]={0};
LL dpr[maxn]={0};
priority_queue<LL> a;

int main()
{

    int n,m,k,p;
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-m*p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            int now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-n*p);
        }
        LL fin=-INF;
        for(int i=0;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-(LL)i*(k-i)*p,fin);
        cout<<fin<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define maxn 1005
#define  LL  long long
LL ma[maxn][maxn];
LL c[maxn];
LL r[maxn];
LL dpc[maxn];
LL dpr[maxn];
priority_queue<LL> a;

int main()
{

    memset(c,0,sizeof(c));
    memset(r,0,sizeof(r));
    memset(dpr,0,sizeof(dpr));
    memset(dpc,0,sizeof(dpc));
    int n,m,k;
    LL p;
    scanf("%d%d%d%I64d",&n,&m,&k,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            {
                scanf("%I64d",ma[i]+j);
                r[i]+=ma[i][j];
                c[j]+=ma[i][j];
            }
        for(int i=1;i<=n;i++)
            a.push(r[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpr[i]+=dpr[i-1]+now;
            a.push(now-(LL)m*(LL)p);
        }
        while(!a.empty())   a.pop();
        for(int i=1;i<=m;i++)
            a.push(c[i]);
        for(int i=1;i<=k;i++)
        {
            LL now=a.top();
            a.pop();
            dpc[i]+=dpc[i-1]+now;
            a.push(now-(LL)n*(LL)p);
        }
        LL fin=dpr[0]+dpc[k];
        for(int i=1;i<=k;i++) fin=max(dpr[i]+dpc[k-i]-(LL)i*(LL)(k-i)*(LL)p,fin);
        cout<<fin<<endl;
}


#include <cstdio>
#include <queue>
#include <climits>
#include <algorithm>
long long ra[1000001]={},ca[1000001]={};
int main()
{
    int n,m,k,p,a,r[1000]={},c[1000]={};
    long long ans=LLONG_MIN,rm,cm;

    scanf("%d%d%d%d",&n,&m,&k,&p);
    for (int i=0; i<n; ++i) {
        for (int j=0; j<m; ++j) {
            scanf("%d",&a);
            r[i]+=a;
            c[j]+=a;
        }
    }
    int pm=p*m,pn=p*n;
    std::priority_queue<long long> sr(r,r+n),sc(c,c+m);

    for (int i=1; i<=k; ++i) {
        rm=sr.top();
        cm=sc.top();
        sr.pop();
        sc.pop();
        ra[i]=ra[i-1]+rm;
        ca[i]=ca[i-1]+cm;
        sr.push(rm-pm);
        sc.push(cm-pn);
    }

    for (int i=0; k>=0; ++i,--k)
        ans=std::max(ans, ra[i]+ca[k]-((long long)(p*i))*k);
    printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include<iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const int INF=2e9;
int a[N][N],row[2*N],con[2*N];
priority_queue<int> R,C;
int main()
{
    int n,m,k,p,x;
    cin>>n>>m>>k>>p;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         cin>>a[i][j];
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=n;i++)
    {
        x=0;
        for(int j=1;j<=m;j++)
            x+=a[i][j];
        R.push(x);
       // cout<<x<<" ";
    }
    for(int j=1;j<=m;j++)
    {
        x=0;
        for(int i=1;i<=n;i++)
            x+=a[i][j];
        C.push(x);
       // cout<<x<<" ";
    }
    int Rmax,Cmax;
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top(),Cmax=C.top();
        R.pop(),C.pop();
       // cout<<Rmax<<" "<<Cmax<<endl;
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
    int ans=-INF;
    for(int i=0;i<=k;i++)
    {
        ans=max(ans,row[i]+con[k-i]-(k-i)*i*p);
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include<climits>
#include <algorithm>
#include <string>
#include <cstring>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const LL INF=2e18;
LL row[1000005],con[1000005];
int main()
{
    LL n,m,k,p,a,r[1005]={},c[1005]={};
    long long ans=LLONG_MIN,rm,cm;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);
    for (int i=0; i<n; ++i)
    {
        for (int j=0; j<m; ++j)
        {
            scanf("%I64d",&a);
            r[i]+=a;
            c[j]+=a;
        }
    }
    std::priority_queue<long long> R(r,r+n),C(c,c+m);
    LL Rmax,Cmax;
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top();
        Cmax=C.top();
        R.pop();
        C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
     for (int i=0; k>=0; ++i,--k)
        ans=std::max(ans, row[i]+con[k]-((long long)(p*i))*k);
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include<climits>
#include <algorithm>
#include <string>
#include <cstring>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL row[2*N],con[2*N];
priority_queue<LL> R,C;
int main()
{
    LL n,m,k,p,a,r[1000]={},c[1000]={};
    long long ans=LLONG_MIN,rm,cm;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);
    for (int i=0; i<n; ++i)
    {
        for (int j=0; j<m; ++j)
        {
            scanf("%I64d",&a);
            r[i]+=a;
            c[j]+=a;
        }
    }
    std::priority_queue<long long> R(r,r+n),C(c,c+m);
    LL Rmax,Cmax;
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top();
        Cmax=C.top();
        R.pop();
        C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
     for (int i=0; k>=0; ++i,--k)
        ans=std::max(ans, row[i]+con[k]-((long long)(p*i))*k);
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL a[N][N],row[2*N],con[2*N];
priority_queue<LL> R,C;
int main()
{
    LL n,m,k,p,x;
    cin>>n>>m>>k>>p;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         cin>>a[i][j];
    while(!R.empty()) R.pop();
    while(!C.empty()) C.pop();
    for(int i=1;i<=n;i++)
    {
        x=0;
        for(int j=1;j<=m;j++)
            x+=a[i][j];
        R.push(x);
    }
    for(int j=1;j<=m;j++)
    {
        x=0;
        for(int i=1;i<=n;i++)
            x+=a[i][j];
        C.push(x);
    }
    LL Rmax,Cmax;
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top(),Cmax=C.top();
        R.pop(),C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
    LL ans=-INF;
    for(int i=0;i<=k;i++)
    {
        ans=max(ans,row[i]+con[k-i]-(LL)(k-i)*i*p);
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL a[N][N],row[2*N],con[2*N];
priority_queue<LL> R,C;
int main()
{
    LL n,m,k,p,x;
    cin>>n>>m>>k>>p;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         cin>>a[i][j];
    while(!R.empty()) R.pop();
    while(!C.empty()) C.pop();
    for(int i=1;i<=n;i++)
    {
        x=0;
        for(int j=1;j<=m;j++)
            x+=a[i][j];
        R.push(x);
    }
    for(int j=1;j<=m;j++)
    {
        x=0;
        for(int i=1;i<=n;i++)
            x+=a[i][j];
        C.push(x);
    }
    LL Rmax,Cmax;
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top(),Cmax=C.top();
        R.pop(),C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
    LL ans=-INF;
    for(int i=0;i<=k;i++)
    {
        ans=max(ans,row[i]+con[k-i]-(k-i)*i*p);
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL a[N][N],row[2*N],con[2*N];
priority_queue<LL> R,C;
int main()
{
    LL n,m,k,p,x;
    cin>>n>>m>>k>>p;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         cin>>a[i][j];
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=n;i++)
    {
        x=0;
        for(int j=1;j<=m;j++)
            x+=a[i][j];
        R.push(x);
       // cout<<x<<" ";
    }
    for(int j=1;j<=m;j++)
    {
        x=0;
        for(int i=1;i<=n;i++)
            x+=a[i][j];
        C.push(x);
       // cout<<x<<" ";
    }
    LL Rmax,Cmax;
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top(),Cmax=C.top();
        R.pop(),C.pop();
       // cout<<Rmax<<" "<<Cmax<<endl;
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
    LL ans=-INF;
    for(int i=0;i<=k;i++)
    {
        ans=max(ans,row[i]+con[k-i]-(k-i)*i*p);
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include<climits>
#include <algorithm>
#include <string>
#include <cstring>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL a[N][N],row[2*N],con[2*N];
priority_queue<LL> R,C;
int main()
{
    LL n,m,k,p,x;
    cin>>n>>m>>k>>p;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         cin>>a[i][j];
    while(!R.empty()) R.pop();
    while(!C.empty()) C.pop();
    for(int i=1;i<=n;i++)
    {
        x=0;
        for(int j=1;j<=m;j++)
            x+=a[i][j];
        R.push(x);
    }
    for(int j=1;j<=m;j++)
    {
        x=0;
        for(int i=1;i<=n;i++)
            x+=a[i][j];
        C.push(x);
    }
    LL Rmax,Cmax;
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top();
        Cmax=C.top();
        R.pop();
        C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
    LL ans=LLONG_MIN;
     for (int i=0; k>=0; ++i,--k)
        ans=std::max(ans, row[i]+con[k]-((long long)(p*i))*k);
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include<climits>
#include <algorithm>
#include <string>
#include <cstring>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL row[2*N],con[2*N];
int main()
{
    LL n,m,k,p,a,r[1000]={},c[1000]={};
    long long ans=LLONG_MIN,rm,cm;
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&k,&p);
    for (int i=0; i<n; ++i)
    {
        for (int j=0; j<m; ++j)
        {
            scanf("%I64d",&a);
            r[i]+=a;
            c[j]+=a;
        }
    }
    std::priority_queue<long long> R(r,r+n),C(c,c+m);
    LL Rmax,Cmax;
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top();
        Cmax=C.top();
        R.pop();
        C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
     for (int i=0; k>=0; ++i,--k)
        ans=std::max(ans, row[i]+con[k]-((long long)(p*i))*k);
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include<iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include<queue>
#define sf scanf
#define pf printf
using namespace std;
typedef long long LL;
typedef double LB;
const int N=5+1e3;
const LL INF=2e18;
LL a[N][N],row[2*N],con[2*N];
priority_queue<LL> R,C;
int main()
{
    LL n,m,k,p,x;
    cin>>n>>m>>k>>p;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
         cin>>a[i][j];
    while(!R.empty()) R.pop();
    while(!C.empty()) C.pop();
    for(int i=1;i<=n;i++)
    {
        x=0;
        for(int j=1;j<=m;j++)
            x+=a[i][j];
        R.push(x);
    }
    for(int j=1;j<=m;j++)
    {
        x=0;
        for(int i=1;i<=n;i++)
            x+=a[i][j];
        C.push(x);
    }
    LL Rmax,Cmax;
    memset(row,0,sizeof(row));
    memset(con,0,sizeof(con));
    for(int i=1;i<=k;i++)
    {
        Rmax=R.top();
        Cmax=C.top();
        R.pop();
        C.pop();
        row[i]=row[i-1]+Rmax;
        con[i]=con[i-1]+Cmax;
        R.push(Rmax-m*p);
        C.push(Cmax-n*p);
    }
    LL ans=-INF;
    for(int i=0;i<=k;i++)
    {
        ans=max(ans,row[i]+con[k-i]-(LL)(k-i)*i*p);
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

int N, M, K, P;
int a[1005][1005];
int rowVal[1005] = {0};
int colVal[1005] = {0};
priority_queue<int> rowQ;
priority_queue<int> colQ;
priority_queue<int> finale;

vector<int> row;
vector<int> rowSum;
vector<int> col;
vector<int> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%d%d%d%d", &N, &M, &K, &P);
    for (int i = 0; i<N; i++){
        for (int j = 0; j<M; j++){
            scanf("%d", &a[i][j]);
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (int i = 0; i<N; i++) rowQ.push(rowVal[i]);
    int sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (int i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (int i = 0; i<=K; i++) printf("%d ", row[i]); printf("\n");
        for (int i = 0; i<=K; i++) printf("%d ", rowSum[i]); printf("\n");

        printf("\n");

        printf("Best cols\n");
        for (int i = 0; i<=K; i++) printf("%d ", col[i]); printf("\n");
        for (int i = 0; i<=K; i++) printf("%d ", colSum[i]); printf("\n");
        printf("\n\n\n");
    }
    for (int i = 0; i<=K; i++){
        int j = K-i;
        if (DB) printf("%d %d %d\n", i, j, rowSum[i] + colSum[j] - i*j*P);
        finale.push(rowSum[i] + colSum[j] - i*j*P);
    }

    printf("%d\n", finale.top());

}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    cin >> N >> M >> K >> P;
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            cin >> a[i][j];
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<M; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) cout << row[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << rowSum[i] << " "; cout << endl;

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) cout << col[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << colSum[i] << " "; cout << endl;
        printf("\n\n\n");
    }
    long long best;
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (i==0) best = rowSum[i] + colSum[j] - i*j*P;
        else{
            best = max(best, rowSum[i] + colSum[j] - i*j*P);
        }
    }

    cout << best << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;
priority_queue<long long> finale;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%d%d%d%d", &N, &M, &K, &P);
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            scanf("%d", &a[i][j]);
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) printf("%d ", row[i]); printf("\n");
        for (long long i = 0; i<=K; i++) printf("%d ", rowSum[i]); printf("\n");

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) printf("%d ", col[i]); printf("\n");
        for (long long i = 0; i<=K; i++) printf("%d ", colSum[i]); printf("\n");
        printf("\n\n\n");
    }
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (DB) printf("%d %d %d\n", i, j, rowSum[i] + colSum[j] - i*j*P);
        finale.push(rowSum[i] + colSum[j] - i*j*P);
    }

    cout << finale.top() << endl;

}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;
priority_queue<long long> finale;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    cin >> N >> M >> K >> P;
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            cin >> a[i][j];
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K+2){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K+2){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) cout << row[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << rowSum[i] << " "; cout << endl;

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) cout << col[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << colSum[i] << " "; cout << endl;
        printf("\n\n\n");
    }
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (DB) cout << i << " " << j << " " << rowSum[i] + colSum[j] - i*j*P << endl;
        finale.push(rowSum[i] + colSum[j] - i*j*P);
    }

    cout << finale.top() << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;
priority_queue<long long> finale;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%d%d%d%d", &N, &M, &K, &P);
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            scanf("%d", &a[i][j]);
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) printf("%d ", row[i]); printf("\n");
        for (long long i = 0; i<=K; i++) printf("%d ", rowSum[i]); printf("\n");

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) printf("%d ", col[i]); printf("\n");
        for (long long i = 0; i<=K; i++) printf("%d ", colSum[i]); printf("\n");
        printf("\n\n\n");
    }
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (DB) printf("%d %d %d\n", i, j, rowSum[i] + colSum[j] - i*j*P);
        finale.push(rowSum[i] + colSum[j] - i*j*P);
    }

    printf("%d\n", finale.top());

}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;
priority_queue<long long> finale;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    cin >> N >> M >> K >> P;
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            cin >> a[i][j];
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) printf("%d ", row[i]); printf("\n");
        for (long long i = 0; i<=K; i++) printf("%d ", rowSum[i]); printf("\n");

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) printf("%d ", col[i]); printf("\n");
        for (long long i = 0; i<=K; i++) printf("%d ", colSum[i]); printf("\n");
        printf("\n\n\n");
    }
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (DB) printf("%d %d %d\n", i, j, rowSum[i] + colSum[j] - i*j*P);
        finale.push(rowSum[i] + colSum[j] - i*j*P);
    }

    cout << finale.top() << endl;

}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    cin >> N >> M >> K >> P;
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            cin >> a[i][j];
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) cout << row[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << rowSum[i] << " "; cout << endl;

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) cout << col[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << colSum[i] << " "; cout << endl;
        printf("\n\n\n");
    }
    long long best;
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (i==0) best = rowSum[i] + colSum[j] - i*j*P;
        else{
            best = max(best, rowSum[i] + colSum[j] - i*j*P);
        }
    }

    cout << best << endl;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;

#define max_n 100005
#define pb push_back
#define mp make_pair
#define DB 0

long long N, M, K, P;
long long a[1005][1005];
long long rowVal[1005] = {0};
long long colVal[1005] = {0};
priority_queue<long long> rowQ;
priority_queue<long long> colQ;
priority_queue<long long> finale;

vector<long long> row;
vector<long long> rowSum;
vector<long long> col;
vector<long long> colSum;

int main(){
    //freopen("input.txt", "r", stdin);
    cin >> N >> M >> K >> P;
    for (long long i = 0; i<N; i++){
        for (long long j = 0; j<M; j++){
            cin >> a[i][j];
            rowVal[i]+=a[i][j];
            colVal[j]+=a[i][j];
        }
    }

    row.pb(0);
    rowSum.pb(0);
    for (long long i = 0; i<N; i++) rowQ.push(rowVal[i]);
    long long sum = 0;
    while (row.size()<=K){
        sum+=rowQ.top();
        row.pb(rowQ.top()); // sorted values and shit
        rowSum.pb(sum);
        rowQ.push(rowQ.top()-M*P);
        rowQ.pop();
    }

    col.pb(0);
    colSum.pb(0);
    for (long long i = 0; i<N; i++) colQ.push(colVal[i]);
    sum = 0;
    while (col.size()<=K){
        sum+=colQ.top();
        col.pb(colQ.top()); // sorted values and shit
        colSum.pb(sum);
        colQ.push(colQ.top()-N*P);
        colQ.pop();
    }

    if (DB){
        printf("Best rows & sums\n");
        for (long long i = 0; i<=K; i++) cout << row[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << rowSum[i] << " "; cout << endl;

        printf("\n");

        printf("Best cols\n");
        for (long long i = 0; i<=K; i++) cout << col[i] << " "; cout << endl;
        for (long long i = 0; i<=K; i++) cout << colSum[i] << " "; cout << endl;
        printf("\n\n\n");
    }
    for (long long i = 0; i<=K; i++){
        long long j = K-i;
        if (j<=M){
            if (DB) cout << i << " " << j << " " << rowSum[i] + colSum[j] - i*j*P << endl;
            finale.push(rowSum[i] + colSum[j] - i*j*P);
        }
    }

    cout << finale.top() << endl;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

long long hg[1000010], sg[1000010], h[1010], s[1010];

priority_queue<long long> qq;

int main()
{
	int m, n, k, p;
	cin >> n >> m >> k >> p;
	int i, j;
	memset(h, 0, sizeof(h));
	memset(s, 0, sizeof(s));
	for (i = 0; i < n; ++i)
	  for (j = 0; j < m; ++j)
	  {
		  int x;
		  scanf("%d", &x);
		  h[i] += x;
		  s[j] += x;
	  }
	for (i = 0; i < n; ++i)
	  qq.push(h[i]);
	long long sum = 0;
	for (i = 0; i < k; ++i)
	{
		long long x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - m * p);
		hg[i + 1] = sum;
	}
	sum = 0;
	while (!qq.empty()) qq.pop();
	for (i = 0; i < m; ++i)
	  qq.push(s[i]);
	hg[0] = sg[0] = 0;
	for (i = 0; i < k; ++i)
	{
		long long x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - n * p);
		sg[i + 1] = sum;
	}
	sum = -100000000000000000;
	for (i = 0; i <= k; ++i)
	{
		if (sum < hg[i] + sg[k - i] - (long long)i * (k - i) * p)
		  sum = hg[i] + sg[k - i] - (long long)i * (k - i) * p;
	}
	printf("%I64d\n", sum);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

long long hg[1000010], sg[1000010], h[1010], s[1010];

priority_queue<long long> qq;

int main()
{
	int m, n, k, p;
	cin >> n >> m >> k >> p;
	int i, j;
	memset(h, 0, sizeof(h));
	memset(s, 0, sizeof(s));
	for (i = 0; i < n; ++i)
	  for (j = 0; j < m; ++j)
	  {
		  int x;
		  scanf("%d", &x);
		  h[i] += x;
		  s[j] += x;
	  }
	for (i = 0; i < n; ++i)
	  qq.push(h[i]);
	long long sum = 0;
	for (i = 0; i < k; ++i)
	{
		long long x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - m * p);
		hg[i + 1] = sum;
	}
	sum = 0;
	while (!qq.empty()) qq.pop();
	for (i = 0; i < m; ++i)
	  qq.push(s[i]);
	hg[0] = sg[0] = 0;
	for (i = 0; i < k; ++i)
	{
		long long x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - n * p);
		sg[i + 1] = sum;
	}
	sum = -100000000000000000;
	for (i = 0; i <= k; ++i)
	{
		if (sum < hg[i] + sg[k - i] - 1L * i * (k - i) * p)
		  sum = hg[i] + sg[k - i] - 1L * i * (k - i) * p;
	}
	printf("%I64d\n", sum);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

long long hg[1000010], sg[1000010], h[1010], s[1010];

priority_queue<long long> qq;

int main()
{
	int m, n, k, p;
	cin >> n >> m >> k >> p;
	int i, j;
	memset(h, 0, sizeof(h));
	memset(s, 0, sizeof(s));
	for (i = 0; i < n; ++i)
	  for (j = 0; j < m; ++j)
	  {
		  int x;
		  scanf("%d", &x);
		  h[i] += x;
		  s[j] += x;
	  }
	for (i = 0; i < n; ++i)
	  qq.push(h[i]);
	long long sum = 0;
	for (i = 0; i < k; ++i)
	{
		long long x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - m * p);
		hg[i + 1] = sum;
	}
	sum = 0;
	while (!qq.empty()) qq.pop();
	for (i = 0; i < m; ++i)
	  qq.push(s[i]);
	hg[0] = sg[0] = 0;
	for (i = 0; i < k; ++i)
	{
		long long x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - n * p);
		sg[i + 1] = sum;
	}
	sum = 0;
	for (i = 0; i <= k; ++i)
	{
		if (sum < hg[i] + sg[k - i] - 1L * i * (k - i) * p)
		  sum = hg[i] + sg[k - i] - 1L * i * (k - i) * p;
	}
	printf("%I64d\n", sum);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

int h[1010], s[1010], hg[1000010], sg[1000010];

priority_queue<int> qq;

int main()
{
	int m, n, k, p;
	cin >> n >> m >> k >> p;
	int i, j;
	memset(h, 0, sizeof(h));
	memset(s, 0, sizeof(s));
	for (i = 0; i < n; ++i)
	  for (j = 0; j < m; ++j)
	  {
		  int x;
		  scanf("%d", &x);
		  h[i] += x;
		  s[j] += x;
	  }
	for (i = 0; i < n; ++i)
	  qq.push(h[i]);
	int sum = 0;
	for (i = 0; i < k; ++i)
	{
		int x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - m * p);
		hg[i + 1] = sum;
	}
	sum = 0;
	while (!qq.empty()) qq.pop();
	for (i = 0; i < m; ++i)
	  qq.push(s[i]);
	hg[0] = sg[0] = 0;
	for (i = 0; i < k; ++i)
	{
		int x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - n * p);
		sg[i + 1] = sum;
	}
	sum = 0;
	for (i = 0; i <= k; ++i)
	{
		if (sum < hg[i] + sg[k - i] - i * (k - i) * p)
		  sum = hg[i] + sg[k - i] - i * (k - i) * p;
	}
	printf("%d\n", sum);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

long long hg[1000010], sg[1000010], h[1010], s[1010];

priority_queue<long long> qq;

int main()
{
	int m, n, k, p;
	cin >> n >> m >> k >> p;
	int i, j;
	memset(h, 0, sizeof(h));
	memset(s, 0, sizeof(s));
	for (i = 0; i < n; ++i)
	  for (j = 0; j < m; ++j)
	  {
		  int x;
		  scanf("%d", &x);
		  h[i] += x;
		  s[j] += x;
	  }
	for (i = 0; i < n; ++i)
	  qq.push(h[i]);
	long long sum = 0;
	for (i = 0; i < k; ++i)
	{
		int x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - m * p);
		hg[i + 1] = sum;
	}
	sum = 0;
	while (!qq.empty()) qq.pop();
	for (i = 0; i < m; ++i)
	  qq.push(s[i]);
	hg[0] = sg[0] = 0;
	for (i = 0; i < k; ++i)
	{
		int x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - n * p);
		sg[i + 1] = sum;
	}
	sum = 0;
	for (i = 0; i <= k; ++i)
	{
		if (sum < hg[i] + sg[k - i] - 1L * i * (k - i) * p)
		  sum = hg[i] + sg[k - i] - 1L * i * (k - i) * p;
	}
	printf("%I64d\n", sum);
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

int h[1010], s[1010];
long long hg[1000010], sg[1000010];

priority_queue<int> qq;

int main()
{
	int m, n, k, p;
	cin >> n >> m >> k >> p;
	int i, j;
	memset(h, 0, sizeof(h));
	memset(s, 0, sizeof(s));
	for (i = 0; i < n; ++i)
	  for (j = 0; j < m; ++j)
	  {
		  int x;
		  scanf("%d", &x);
		  h[i] += x;
		  s[j] += x;
	  }
	for (i = 0; i < n; ++i)
	  qq.push(h[i]);
	long long sum = 0;
	for (i = 0; i < k; ++i)
	{
		int x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - m * p);
		hg[i + 1] = sum;
	}
	sum = 0;
	while (!qq.empty()) qq.pop();
	for (i = 0; i < m; ++i)
	  qq.push(s[i]);
	hg[0] = sg[0] = 0;
	for (i = 0; i < k; ++i)
	{
		int x = qq.top();
		qq.pop();
		sum += x;
		qq.push(x - n * p);
		sg[i + 1] = sum;
	}
	sum = 0;
	for (i = 0; i <= k; ++i)
	{
		if (sum < hg[i] + sg[k - i] - 1L * i * (k - i) * p)
		  sum = hg[i] + sg[k - i] - 1L * i * (k - i) * p;
	}
	printf("%I64d\n", sum);
	return 0;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;

priority_queue <int> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	int i , j , k ;
	int n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				cin >> map[i][j] ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		int a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		int ans = 0 ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;

priority_queue <int> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	int i , j , k ;
	int n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				cin >> map[i][j] ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		int a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		long long  ans = 0 ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;
#define LL long long

priority_queue <LL> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;
		while(!q1.empty())
			q1.pop() ;
		while(!q2.empty())
			q2.pop() ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				cin >> map[i][j] ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std ;
#define LL __int64

priority_queue <LL> q1,q2 ;
LL map[1005][1005] ;
LL r[1005] , c[1005] ;
LL sum1[1000005] ;
LL sum2[1000005] ;

LL MAX(LL a,LL b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(scanf("%I64d%I64d%I64d%I64d" , &n , &m , &k , &p) != EOF){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;
		while(!q1.empty())
			q1.pop() ;
		while(!q2.empty())
			q2.pop() ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				scanf("%I64d",&map[i][j]) ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			LL s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		printf("%I64d\n",ans) ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;

priority_queue <int> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	int i , j , k ;
	int n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				scanf("%d",&map[i][j]) ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		int a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		int ans = 0 ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std ;
#define LL __int64

priority_queue <LL> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(scanf("%I64d%I64d%I64d%I64d" , &n , &m , &k , &p) != EOF){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;
		while(!q1.empty())
			q1.pop() ;
		while(!q2.empty())
			q2.pop() ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				scanf("%I64d",&map[i][j]) ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			LL s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		printf("%I64d\n",ans) ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std ;
#define LL __int64

priority_queue <LL> q1,q2 ;
LL map[1005][1005] ;
LL r[1005] , c[1005] ;
LL sum1[1000005] ;
LL sum2[1000005] ;

int MAX(LL a,LL b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(scanf("%I64d%I64d%I64d%I64d" , &n , &m , &k , &p) != EOF){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;
		while(!q1.empty())
			q1.pop() ;
		while(!q2.empty())
			q2.pop() ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				scanf("%I64d",&map[i][j]) ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			LL s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		printf("%I64d\n",ans) ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;
#define LL long long

priority_queue <LL> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;
		while(!q1.empty())
			q1.pop() ;
		while(!q2.empty())
			q2.pop() ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				cin >> map[i][j] ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			LL s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;

priority_queue <int> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	int i , j , k ;
	int n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				cin >> map[i][j] ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		int a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*n) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*m) ;
			sum2[i] = a + sum2[i-1] ;
		}

		int ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;
#define LL long long

priority_queue <int> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				cin >> map[i][j] ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		cout << ans << endl ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std ;
#define LL __int64

priority_queue <LL> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	LL i , j , k ;
	LL n , m , p ;
	while(scanf("%I64d%I64d%I64d%I64d" , &n , &m , &k , &p) != EOF){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;
		while(!q1.empty())
			q1.pop() ;
		while(!q2.empty())
			q2.pop() ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				scanf("%I64d\n",&map[i][j]) ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		LL a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		LL ans = sum1[k] ;

		for(i = 0 ; i <= k ; i++){
			LL s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		printf("%lld\n",ans) ;
	}
	return 0 ;
}


#include <iostream>
#include <string.h>
#include <queue>
using namespace std ;

priority_queue <int> q1,q2 ;
int map[1005][1005] ;
int r[1005] , c[1005] ;
int sum1[1000005] ;
int sum2[1000005] ;

int MAX(int a,int b){
	return a > b ? a : b ;
}

int main(){
	int i , j , k ;
	int n , m , p ;
	while(cin >> n >> m >> k >> p){
		memset(r,0,sizeof(r)) ;
		memset(c,0,sizeof(c)) ;
		memset(sum1,0,sizeof(sum1)) ;
		memset(sum2,0,sizeof(sum2)) ;

		for(i = 0 ; i < n ; i++){
			for(j = 0 ; j < m ; j++){
				scanf("%d",&map[i][j]) ;
				c[j] += map[i][j] ;
				r[i] += map[i][j] ;
			}
		}

		for(i = 0 ; i < n ; i++){
			q1.push(r[i]) ;
		}
		for(j = 0 ; j < m ; j++){
			q2.push(c[j]) ;
		}

		int a ;
		for(i = 1 ; i <= k ; i++){
			a = q1.top() ;
			q1.pop() ;
			q1.push(a - p*m) ;
			sum1[i] = a + sum1[i-1] ;
		}

		for(i = 1 ; i <= k ; i++){
			a = q2.top() ;
			q2.pop() ;
			q2.push(a - p*n) ;
			sum2[i] = a + sum2[i-1] ;
		}

		int ans = 0 ;

		for(i = 0 ; i <= k ; i++){
			int s = sum1[i] + sum2[k-i] - p*i*(k-i);
			ans = MAX(ans , s) ;
		}

		printf("%d\n",ans) ;
	}
	return 0 ;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%d", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef int LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL a[N+5][M+5];
    LL Col[M+5];
    LL Row[N+5];
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(a[n][m]);
        }
    }
    multiset<pair<LL, LL> > St_Col;
    multiset<pair<LL, LL> > St_Row;
    for(LL n=0;n<N;++n){
        LL Sum = 0;
        for(LL m=0;m<M;++m){
            Sum += a[n][m];
        }
        Row[n] = Sum;
        St_Row.insert(mp(Sum, n));
        //St.insert(mp(Sum, mp((LL)0, n)));
    }
    for(LL m=0;m<M;++m){
        LL Sum = 0;
        for(LL n=0;n<N;++n){
            Sum += a[n][m];
        }
        Col[m] = Sum;
        St_Col.insert(mp(Sum, m));
    }
    LL Ans = 0;
    LL x, b, c, y;
    pair<LL, LL> temp1, temp2;
    while(K--){
        temp1 = *St_Col.rbegin();
        temp2 = *St_Row.rbegin();
        if(temp1.ff >= temp2.ff){
            b = temp1.ff;
            c = temp1.ss;
            Ans += b;
            St_Col.erase(temp1);
            for(LL n=0;n<N;++n){
                St_Row.erase(mp(Row[n], n));
                Row[n] -= P;
                St_Row.insert(mp(Row[n], n));
            }
            St_Col.erase(mp(Col[c], c));
            Col[c] -= N*P;
            St_Col.insert(mp(Col[c], c));
        }
        else{
            b = temp2.ff;
            c = temp2.ss;
            Ans += b;
            St_Row.erase(temp2);
            for(LL m=0;m<M;++m){
                St_Col.erase(mp(Col[m],  m));
                Col[m] -= P;
                St_Col.insert(mp(Col[m], m));
            }
            St_Row.erase(mp(Row[c], c));
            Row[c] -= M*P;
            St_Row.insert(mp(Row[c], c));
        }
    }
    cout << Ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%lld", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef long long LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL a[N+5][M+5];
    LL Col[M+5];
    LL Row[N+5];
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(a[n][m]);
        }
    }
    LL Ans = 0;
    LL x, b, c, d, y, i, j, n, m, Sum;
    for(n=0;n<N;++n){
        Sum = 0;
        for(m=0;m<M;++m){
            Sum += a[n][m];
        }
        Row[n] = Sum;
    }
    for(m=0;m<M;++m){
        Sum = 0;
        for(n=0;n<N;++n){
            Sum += a[n][m];
        }
        Col[m] = Sum;
    }
    pair<LL, LL> temp1;
    while(K--){
        b = (LL)(-1e15);
        c = 0;
        d = 0;
        for(n=0;n<N;++n){
            if(b < Row[n]){
                b = Row[n];
                c = n;
                d = 0;
            }
        }
        for(m=0;m<M;++m){
            if(b < Col[m]){
                b = Col[m];
                c = m;
                d = 1;
            }
        }
        //cout << "   " << b << " " << c << " " << d << endl;
        if(d == 1){
            Ans += b;
            for(n=0;n<N;++n){
                Row[n] -= P;
            }
            Col[c] -= N*P;
        }
        else{
            Ans += b;
            for(m=0;m<M;++m){
                Col[m] -= P;
            }
            Row[c] -= M*P;
        }
    }
    cout << Ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%lld", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef long long LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

LL dp1[1000005];
LL dp2[1000005];
int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL Col[10005] = {};
    LL Row[10005] = {};
    LL x;
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(x);
            Row[n] += x;
            Col[m] += x;
        }
    }
    LL Pm = (P * M);
    LL PN = (P * N);
    priority_queue<LL> Sr(Row, Row+N);
    priority_queue<LL> Sc(Col, Col+M);
    for(LL k=1;k<=K;++k){
        LL rm = Sr.top();
        LL cm = Sc.top();
        Sr.pop();
        Sc.pop();
        dp1[k] = dp1[k-1] + rm;
        dp2[k] = dp2[k-1] + cm;
        Sr.push(rm - Pm);
        Sc.push(cm - PN);
    }
    long long Ans = (LL)-1e15;
    for(LL n=0;K>=0;--K, ++n){
        Ans = max(Ans, dp1[n] + dp2[K] - (P * n)*K);
    }
    cout << Ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%lld", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef long long LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL a[N+5][M+5];
    LL Col[M+5];
    LL Row[N+5];
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(a[n][m]);
        }
    }
    set<pair<LL, pair<LL, LL> > > St;
    for(LL n=0;n<N;++n){
        LL Sum = 0;
        for(LL m=0;m<M;++m){
            Sum += a[n][m];
        }
        Row[n] = Sum;
        St.insert(mp(Sum, mp((LL)0, n)));
    }
    for(LL m=0;m<M;++m){
        LL Sum = 0;
        for(LL n=0;n<N;++n){
            Sum += a[n][m];
        }
        Col[m] = Sum;
        St.insert(mp(Sum, mp((LL)1, m)));
    }
    LL Ans = 0;
    pair<LL, pair<LL, LL> > temp;
    LL x, b, c, y;
    while(K--){
        temp = *St.rbegin();
        St.erase(temp);
        x = temp.ff;
        b = temp.ss.ff;
        c = temp.ss.ss;
        Ans += x;/*
        cout << x << " " <<  b << " " << c << endl;
        for(int i=0;i<2;++i){
            cout <<"    "<< Row[i];
        }
        cout <<"\n    ---------\n"<< endl;
        for(int i=0;i<2;++i){
            cout <<"    "<< Col[i];
        }
        cout <<"\n    ---------\n"<< endl;/**/
        if(b == 0){
            for(LL m=0;m<M;++m){
                St.erase(mp(Col[m], mp((LL)1, m)));
                Col[m] -= P;
                St.insert(mp(Col[m], mp((LL)1, m)));
            }
            St.erase(mp(Row[c], mp((LL)0, c)));
            Row[c] -= M*P;
            St.insert(mp(Row[c], mp((LL)0, c)));
        }
        else{
            for(LL n=0;n<N;++n){
                St.erase(mp(Row[n], mp((LL)0, n)));
                Row[n] -= P;
                St.insert(mp(Row[n], mp((LL)0, n)));
            }
            St.erase(mp(Col[c], mp((LL)1, c)));
            Col[c] -= N*P;
            St.insert(mp(Col[c], mp((LL)1, c)));
        }
    }
    cout << Ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%lld", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef long long LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL a[N+5][M+5];
    LL Col[M+5];
    LL Row[N+5];
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(a[n][m]);
        }
    }
    set<pair<LL, pair<LL, LL> > > St;
    for(LL n=0;n<N;++n){
        LL Sum = 0;
        for(LL m=0;m<M;++m){
            Sum += a[n][m];
        }
        Row[n] = Sum;
        St.insert(mp(Sum, mp((LL)0, n)));
    }
    for(LL m=0;m<M;++m){
        LL Sum = 0;
        for(LL n=0;n<N;++n){
            Sum += a[n][m];
        }
        Col[m] = Sum;
        St.insert(mp(Sum, mp((LL)1, m)));
    }
    LL Ans = 0;
    pair<LL, pair<LL, LL> > temp;
    LL x, b, c, y;
    while(K--){
        temp = *St.rbegin();
        St.erase(temp);
        x = temp.ff;
        b = temp.ss.ff;
        c = temp.ss.ss;
        Ans += x;/*
        cout << x << " " <<  b << " " << c << endl;
        for(int i=0;i<2;++i){
            cout <<"    "<< Row[i];
        }
        cout <<"\n    ---------\n"<< endl;
        for(int i=0;i<2;++i){
            cout <<"    "<< Col[i];
        }
        cout <<"\n    ---------\n"<< endl;*/
        if(b == 0){
            for(LL m=0;m<M;++m){
                St.erase(mp(Col[m], mp((LL)1, m)));
                Col[m] -= P;
                St.insert(mp(Col[m], mp((LL)1, m)));
            }
            Row[c] -= M*P;
        }
        else{
            for(LL n=0;n<N;++n){
                St.erase(mp(Row[n], mp((LL)0, n)));
                Row[n] -= P;
                St.insert(mp(Row[n], mp((LL)0, n)));
            }
            Col[c] -= N*P;
        }
    }
    cout << Ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%d", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef int LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL a[N+5][M+5];
    LL Col[M+5];
    LL Row[N+5];
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(a[n][m]);
        }
    }
    long long Ans = 0;
    LL x, b, c, d, y, i, j, n, m, Sum;
    for(n=0;n<N;++n){
        Sum = 0;
        for(m=0;m<M;++m){
            Sum += a[n][m];
        }
        Row[n] = Sum;
    }
    for(m=0;m<M;++m){
        Sum = 0;
        for(n=0;n<N;++n){
            Sum += a[n][m];
        }
        Col[m] = Sum;
    }
    while(K--){
        b = Row[n];
        c = 0;
        d = 0;
        for(n=0;n<N;++n){
            if(b < Row[n]){
                b = Row[n];
                c = n;
                d = 0;
            }
        }
        for(m=0;m<M;++m){
            if(b < Col[m]){
                b = Col[m];
                c = m;
                d = 1;
            }
        }
        if(d == 1){
            Ans += b;
            for(n=0;n<N;++n){
                Row[n] -= P;
            }
            Col[c] -= N*P;
        }
        else{
            Ans += b;
            for(m=0;m<M;++m){
                Col[m] -= P;
            }
            Row[c] -= M*P;
        }
    }
    cout << Ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz size()
#define all(a)  a.begin(), a.end()
#define allr(a)  a.rbegin(), a.rend()
#define SL(n) scanf("%d", &n)
#define PL(n) printf("%lld", n)
#define fill(a, x) memset(a, x, sizeof(a));
#define mod 1000000007

using namespace std;
typedef int LL;
typedef vector <LL> VL;
typedef map <LL, LL> ML;
typedef pair<LL, LL> PL;
typedef vector <pair <LL, LL> > VPL;

int main(){
    LL N, M, K, P;
    SL(N);
    SL(M);
    SL(K);
    SL(P);
    LL a[N+5][M+5];
    LL Col[M+5];
    LL Row[N+5];
    for(LL n=0;n<N;++n){
        for(LL m=0;m<M;++m){
            SL(a[n][m]);
        }
    }
    long long Ans = 0, b;
    LL x, c, d, y, i, j, n, m, Sum;
    for(n=0;n<N;++n){
        Sum = 0;
        for(m=0;m<M;++m){
            Sum += a[n][m];
        }
        Row[n] = Sum;
    }
    for(m=0;m<M;++m){
        Sum = 0;
        for(n=0;n<N;++n){
            Sum += a[n][m];
        }
        Col[m] = Sum;
    }
    while(K--){
        b = Row[0];
        c = 0;
        d = 0;
        for(n=1;n<N;++n){
            if(b < Row[n]){
                b = Row[n];
                c = n;
                d = 0;
            }
        }
        for(m=0;m<M;++m){
            if(b < Col[m]){
                b = Col[m];
                c = m;
                d = 1;
            }
        }
        if(d == 1){
            Ans += (long long)b;
            for(n=0;n<N;++n){
                Row[n] -= P;
            }
            Col[c] -= N*P;
        }
        else{
            Ans += (long long)b;
            for(m=0;m<M;++m){
                Col[m] -= P;
            }
            Row[c] -= M*P;
        }
    }
    cout << Ans << endl;
    return 0;
}