#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
int a,b,n;
int c[maxn][maxn];
deque <int> dq1,dq2;
int max1[maxn][maxn],min1[maxn][maxn];
int max2[maxn][maxn],min2[maxn][maxn];
int ans = 2e9;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> a >> b >> n;
for(int i = 1; i <= a; i++){
for(int j = 1; j <= b; j++){
cin >> c[i][j];
}
}
for(int i = 1; i <= a; i++){
dq1.clear();dq2.clear();
for(int j = 1; j <= b; j++){
while(!dq1.empty() && j - dq1.front() + 1 > n) dq1.pop_front();
while(!dq1.empty() &&c[i][dq1.back()] <= c[i][j]) dq1.pop_back();
dq1.push_back(j);
if(j >= n) max1[i][j - n + 1] = c[i][dq1.front()];
while(!dq2.empty() && j - dq2.front() + 1 > n) dq2.pop_front();
while(!dq2.empty() &&c[i][dq2.back()] >= c[i][j]) dq2.pop_back();
dq2.push_back(j);
if(j >= n) min1[i][j - n + 1] = c[i][dq2.front()];
}
}
for(int j = 1; j <= b - n + 1; j++){
dq1.clear();dq2.clear();
for(int i = 1; i <= a; i++){
while(!dq1.empty() && i - dq1.front() + 1 > n) dq1.pop_front();
while(!dq1.empty() && max1[i][j] >= max1[dq1.back()][j]) dq1.pop_back();
dq1.push_back(i);
if(i >= n) max2[i - n + 1][j] = max1[dq1.front()][j];
while(!dq2.empty() && i - dq2.front() + 1 > n) dq2.pop_front();
while(!dq2.empty() && min1[i][j] <= min1[dq2.back()][j]) dq2.pop_back();
dq2.push_back(i);
if(i >= n) min2[i - n +1][j] = min1[dq2.front()][j];
}
}
for(int i = 1; i <= a - n +1; i++){
for(int j = 1; j <= b - n +1; j++){
ans = min(ans,max2[i][j] - min2[i][j]);
}
}
cout << ans;
return 0;
}