#include <bits/stdc++.h>
#define int long long
using namespace std;
#define file(x) {freopen(x".in", "r", stdin); freopen(x".out", "w", stdout);}
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(std::string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define io Fastio::cin
#define IO Fastio::cout
int T = 1;
const int maxn = 1e3 + 3;
int A[maxn][maxn];
int B[maxn][maxn];
int C[maxn][maxn];
int M[maxn][maxn], N[maxn][maxn];
void solve(){
int n, m, k;
Fastio::cin >> n >> m >> k;
for(int i = 0;i < n;++ i){
for(int j = 0;j < m;++ j){
Fastio::cin >> A[i][j];
M[i][j] = 1e9;
N[i][j] = -1e9;
}
}
for(int i = 0;i <= (n - 1) / k;++ i){
for(int j = 0;j <= (m - 1) / k;++ j){
int u = i * k, v = std::min(n - 1, u + k - 1);
int l = j * k, r = std::min(m - 1, l + k - 1);
std::vector <std::tuple<int, int, int, int, int, int> > T = {
{ u, v, l, r, - k + 1, - k + 1 },
{ u, v, r, l, - k + 1, 0 },
{ v, u, l, r, 0, - k + 1 },
{ v, u, r, l, 0, 0 }
};
for(auto [l1, r1, l2, r2, d1, d2]: T){
int t1 = 0, t2 = 0;
t1 = r1 > l1 ? 1 : -1;
t2 = r2 > l2 ? 1 : -1;
for(int a = l1;a != r1 + t1;a += t1){
for(int b = l2;b != r2 + t2;b += t2){
B[a][b] = C[a][b] = A[a][b];
if(a != l1)
B[a][b] = min(B[a][b], B[a - t1][b]),
C[a][b] = max(C[a][b], C[a - t1][b]);
if(b != l2)
B[a][b] = min(B[a][b], B[a][b - t2]),
C[a][b] = max(C[a][b], C[a][b - t2]);
if(a + d1 >= 0 && b + d2 >= 0)
M[a + d1][b + d2] = min(M[a + d1][b + d2], B[a][b]),
N[a + d1][b + d2] = max(N[a + d1][b + d2], C[a][b]);
}
}
}
}
}
int ans = 1e9;
for(int i = 0;i < n - k + 1;++ i){
for(int j = 0;j < m - k + 1;++ j){
ans = min(ans, N[i][j] - M[i][j]);
}
}
Fastio::cout << ans << '\n';
}
int32_t main(){
// file("A");
// Fastio::cin >> T;
while(T--){
solve();
}
return 0;
}