#include <bits/stdc++.h>
using namespace std;
int n,m,k,q[1005],q1[1005],a[2005][2005],head=1,tail=1,ans=0x3f3f3f3f,head1=1,tail1=1;
int min_r[1005][1005],min_c[1005][1005],max_r[1005][1005],max_c[1005][1005];
signed main() {
cin>>n>>m>>k;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
scanf("%d",&a[i][j]);
}
}
for(int i=1; i<=n-k+1; i++) {
for(int j=1; j<=m-k+1; j++) {
while(head<=tail&&a[i][q[tail]]>a[i][j]) {
tail--;
}
q[++tail]=j;
while(j-q[head]+1>k) {
head++;
}
if(j>=k) {
min_r[i][j]=a[i][q[head]];
}
}
}
memset(q,0,sizeof(q));
head=tail=1;
for(int i=1; i<=n-k+1; i++) {
for(int j=1; j<=m-k+1; j++) {
while(head<=tail&&a[i][q[tail]]<a[i][j]) {
tail--;
}
q[++tail]=j;
while(j-q[head]+1>k) {
head++;
}
if(j>=k) {
max_r[i][j]=a[i][q[head]];
}
}
}
memset(q,0,sizeof(q));
head=tail=1;
for(int i=1; i<=n-k+1; i++) {
for(int j=1; j<=m-k+1; j++) {
while(head<=tail&&max_r[i][q[tail]]<max_r[i][j]) {
tail--;
}
q[++tail]=j;
while(j-q[head]+1>k) {
head++;
}
if(j>=k) {
max_c[i][j]=max_r[i][q[head]];
}
while(head1<=tail1&&min_r[i][q1[tail]]<min_r[i][j]) {
tail1--;
}
q1[++tail1]=j;
while(j-q1[head1]+1>k) {
head1++;
}
if(j>=k) {
min_c[i][j]=min_r[i][q1[head1]];
}
}
}
for(int i=1;i<=n-k+1;i++){
for(int j=1;j<=m-k+1;j++){
ans=min(ans,max_c[i][j]-min_c[i][j]);
}
}
cout<<ans;
}