1 #include <map>
2 #include <set>
3 #include <stack>
4 #include <cmath>
5 #include <ctime>
6 #include <queue>
7 #include <cstdio>
8 #include <vector>
9 #include <string>
10 #include <bitset>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16
17 #ifndef unix
18 #define lld "%I64d"
19 #define llu "%I64u"
20 #else
21 #define lld "%lld"
22 #define llu "%llu"
23 #endif
24
25 #define FOR(a,b,c) for(int (a)=b;(a)<=(c);++(a))
26 #define FORD(a,b,c) for(int (a)=b;(a)>=(c);--(a))
27 #define FORV(a,t,b) for(vector<t>::iterator a=b.begin();a!=b.end();++a)
28 #define MAX(a,b) a=max(a,b)
29 #define MIN(a,b) a=min(a,b)
30 #define BLA printf("\n")
31 #define pb push_back
32 #define mp make_pair
33 #define gc getchar
34 #define RT return
35 #define BB second
36 #define AA first
37 #define bk break
38 #define LINF 0x3f3f3f3f3f3f3f3fll
39 #define INF 0x3f3f3f3f
40 #define eps 1e-8
41 #define DINF 1e20
42
43 //#define Generator
44
45 typedef long long ll;
46 typedef unsigned ui;
47 typedef unsigned long long ull;
48 typedef pair<int,int> pii;
49 typedef pair<ll ,ll > pll;
50
51 const int MAXN= 0;
52 const int MOD = 0;
53
54 template <class T> inline void CLR(T &g) {T t;swap(t,g);}
55 template <class T> inline void CLR(T &g,int a){memset(g,a,sizeof g);}
56 template <class T> inline void CPY(T &a,T &b) {memcpy(a,b,sizeof a);}
57 template <class T> inline bool inr(T a,T b,T c) {RT (a>=b && a<=c);}
58 inline int acc(int a,int b) {RT !!(a & (1<<b-1));}
59 inline int fil(int a,int b,int c) {RT a & ~(1<<b-1) | (1<<b-1)*c;}
60
61 int N, M, K, Q, R, C;
62
63 int a[20][20];
64 int f[20][20];//f[i][j]表示 已经选择i列 上一列是j
65 int sum[20], d[20], csum[20][20];
66 /*========================================================*/
67 int main()
68 {
69 #ifndef Generator
70 #ifndef ONLINE_JUDGE
71 #endif
72 #endif //真
73 #ifdef Generator //正
74 freopen("input.txt","w",stdout); //有
75 srand((ui)time(NULL)); //用
76 #endif //的
77 int T, o=0; //在
78 scanf("%d%d%d%d", &N, &M, &R, &C); //这
79 FOR(i, 1, N) //里
80 FOR(j, 1, M)
81 scanf("%d", &a[i][j]);
82 int ans=INF;
83 FOR(i, 1, (1<<N)-1){//枚举哪些行被选入矩阵
84 int cnt=0;
85 FOR(j, 1, N)
86 if (acc(i, j)) d[++cnt]=j;
87 if (cnt != R) continue;
88 FOR(j, 1, M){
89 sum[j]=0;
90 FOR(k, 1, R-1)
91 sum[j] += abs(a[d[k]][j]-a[d[k+1]][j]);
92 }
93 FOR(j, 1, M)
94 FOR(k, j+1, M){
95 csum[j][k]=0;
96 FOR(l, 1, R)
97 csum[j][k] += abs(a[d[l]][j]-a[d[l]][k]);
98 }
99 CLR(f, 0x3f);
100 f[0][0]=0;
101 FOR(j, 1, C){
102 FOR(k, 1, M){
103 int tot=0;
104 FOR(l, 0, k-1)
105 MIN(f[j][k], f[j-1][l]+csum[l][k]+sum[k]);
106 }
107 }
108 FOR(j, 1, M)
109 MIN(ans, f[C][j]);
110 }
111 printf("%d\n", ans);
112 RT 0;
113 }
114 /*===================================================================*/