#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <queue>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 1234
#define MAX 1037471823
using namespace std;
struct node
{
int x, y;
bool operator < (const node &k) const { return y < k.y; }
} a;
struct node2
{
int x, y;
bool operator < (const node2 &k) const { return y > k.y; }
} a2;
int n, m, k, w[MS][MS], dmin[MS][MS], dmax[MS][MS], rmin[MS][MS], rmax[MS][MS], ans;
int main()
{
scanf("%d%d%d", &n, &m, &k);
rep(i, 1, n) rep(j, 1, m) scanf("%d", &w[i][j]);
rep(j, 1, m) rep(i, 1, n-k+1) dmin[i][j] = MAX, dmax[i][j] = -MAX;
rep(j, 1, m)
{
priority_queue <node> q;
rep(i, 1, k) { a.x = i; a.y = w[i][j]; q.push(a); }
dmax[1][j] = q.top().y;
rep(i, 2, n-k+1)
{
while (q.top().x < i) q.pop();
a.x = i+k-1, a.y = w[i+k-1][j]; q.push(a);
dmax[i][j] = q.top().y;
}
}
rep(j, 1, m)
{
priority_queue <node2> q;
rep(i, 1, k) { a2.x = i; a2.y = w[i][j]; q.push(a2); }
dmin[1][j] = q.top().y;
rep(i, 2, n-k+1)
{
while (q.top().x < i) q.pop();
a2.x = i+k-1, a2.y = w[i+k-1][j]; q.push(a2);
dmin[i][j] = q.top().y;
}
}
rep(i, 1, n-k+1)
{
priority_queue <node> q;
rep(j, 1, k) { a.x = j; a.y = dmax[i][j]; q.push(a); }
rmax[i][1] = q.top().y;
rep(j, 2, m-k+1)
{
while (q.top().x < j) q.pop();
a.x = j+k-1, a.y = dmax[i][j+k-1]; q.push(a);
rmax[i][j] = q.top().y;
}
}
rep(i, 1, n-k+1)
{
priority_queue <node2> q;
rep(j, 1, k) { a2.x = j; a2.y = dmin[i][j]; q.push(a2); }
rmin[i][1] = q.top().y;
rep(j, 2, m-k+1)
{
while (q.top().x < j) q.pop();
a2.x = j+k-1, a2.y = dmin[i][j+k-1]; q.push(a2);
rmin[i][j] = q.top().y;
}
}
ans = MAX;
rep(i, 1, n-k+1) rep(j, 1, m-k+1) if (ans > rmax[i][j] - rmin[i][j]) ans = rmax[i][j] - rmin[i][j];
printf("%d\n", ans);
return 0;
}
I2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxxdWV1ZT4KCiNkZWZpbmUgcmVwKGksIGwsIHIpIGZvcihpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKQojZGVmaW5lIGRvd24oaSwgbCwgcikgZm9yKGludCBpID0gbDsgaSA+PSByOyBpLS0pCiNkZWZpbmUgTVMgMTIzNAojZGVmaW5lIE1BWCAxMDM3NDcxODIzCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IG5vZGUKewoJaW50IHgsIHk7Cglib29sIG9wZXJhdG9yIDwgKGNvbnN0IG5vZGUgJmspIGNvbnN0IHsgcmV0dXJuIHkgPCBrLnk7IH0KfSBhOwpzdHJ1Y3Qgbm9kZTIKewoJaW50IHgsIHk7Cglib29sIG9wZXJhdG9yIDwgKGNvbnN0IG5vZGUyICZrKSBjb25zdCB7IHJldHVybiB5ID4gay55OyB9Cn0gYTI7CmludCBuLCBtLCBrLCB3W01TXVtNU10sIGRtaW5bTVNdW01TXSwgZG1heFtNU11bTVNdLCBybWluW01TXVtNU10sIHJtYXhbTVNdW01TXSwgYW5zOwoKaW50IG1haW4oKQp7CglzY2FuZigiJWQlZCVkIiwgJm4sICZtLCAmayk7CglyZXAoaSwgMSwgbikgcmVwKGosIDEsIG0pIHNjYW5mKCIlZCIsICZ3W2ldW2pdKTsKCXJlcChqLCAxLCBtKSByZXAoa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