#include<bits/stdc++.h>usingnamespacestd;constintN=1010;intn,m,k;introw_min[N][N],row_max[N][N];intw[N][N];intq[N];/* * function of get_min & get_max: * a[] is the array of target; * b[] is the array of result; * tot is the length of array a[]. */voidget_min(inta[],intb[],inttot){inthh=0,tt=-1;for(inti=1;i<=tot;i++){if(hh<=tt&&q[hh]<=i-k)hh++;while(hh<=tt&&a[q[tt]]>=a[i])tt--;q[++tt]=i;b[i]=a[q[hh]];}}voidget_max(inta[],intb[],inttot){inthh=0,tt=-1;for(inti=1;i<=tot;i++){if(hh<=tt&&q[hh]<=i-k)hh++;while(hh<=tt&&a[q[tt]]<=a[i])tt--;q[++tt]=i;b[i]=a[q[hh]];}}intmain(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m>>k;for(inti=1;i<=n;i++)for(intj=1;j<=m;j++)cin>>w[i][j];// To get n rows of row_min & row_max.for(inti=1;i<=n;i++){get_min(w[i],row_min[i],m);get_max(w[i],row_max[i],m);}inta[N],b[N],c[N];// Array b is min of square, array c is max of square.intres=1e9;// The begin of right boundary is k.for(inti=k;i<=m;i++){// Get the min of square.for(intj=1;j<=n;j++)a[j]=row_min[j][i];get_min(a,b,n);// Get the max of square.for(intj=1;j<=n;j++)a[j]=row_max[j][i];get_max(a,c,n);// res = min(max - min);// Also, the begin of bottom boundary is k.for(intj=k;j<=n;j++)res=min(res,c[j]-b[j]);}cout<<res<<endl;return0;}