#include <bits/stdc++.h>
using namespace std;
const int INF = -1e9;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> a(n+1, vector<int>(m+1));
vector<vector<bool>> b(n+1, vector<bool>(m+1));
vector<vector<bool>> b1(n+1, vector<bool>(m+1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
int len = 2 * k + 1;
vector<vector<int>> pref(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
pref[i][j] = a[i][j]+ pref[i - 1][j]+ pref[i][j - 1]- pref[i - 1][j - 1];
}
}
vector<vector<int>> rowMax(n+1, vector<int>(m+1));
vector<int> c(260);
for (int i = 1; i <= n; i++) {
deque<int> dq;
for (int j = 1; j <= m; j++) {
c[a[i][j]]++;
while (!dq.empty() && a[i][dq.back()] <= a[i][j]) dq.pop_back();
dq.push_back(j);
if (dq.front() <= j - len) dq.pop_front();
if(j-len >= 1){
c[a[i][ j-len]]--;
}
if(j-len >=0){
int x = a[i][dq.front()];
rowMax[i][j-k] = x;
if(c[x]==1)b[i][j-k] = true;
// cout << x<< " ";
}
}
for(int j = m; j>m-len;j--)c[a[i][j]]=0;
// cout << "\n";
}
vector<vector<int>> maxInSquare(n+1, vector<int>(m+1));
// vector<int> c(260);
for (int j = k+1; j +k<= m; j++) {
deque<int> dq;
for (int i = 1; i <= n; i++) {
c[rowMax[i][j]]+=2-b[i][j];
while (!dq.empty() && rowMax[dq.back()][j] <= rowMax[i][j]) dq.pop_back();
dq.push_back(i);
if (dq.front() <= i - len) dq.pop_front();
if(i-len>=1)c[rowMax[i-len][j]]-=2-b[i-len][j];
if(i-len >= 0){
int x = rowMax[dq.front()][j];
maxInSquare[i-k][j] = x;
if(c[x]==1)b1[i-k][j] = true;
// cout << x << ":" <<c[x] << " ";
}
}
for (int i = n; i > n-len; i--) {
c[rowMax[i][j]]=0;
}
// cout << "\n";
}
// // =====================
// // 3. Duyệt tìm vùng sáng nổi bật nhất
// // =====================
long long ans = 0;
bool found = false;
for (int i = k+1; i + k <= n; i++) {
for (int j = k+1; j + k <= m; j++) {
int maxVal = maxInSquare[i][j];
// cout << maxVal << ":" << a[i][j] << " ";
if (a[i][j] == maxVal && b1[i][j]) {
int x1 = i - k-1, y1 = j - k-1;
int x2 = i + k, y2 = j + k ;
long long sum = pref[x2][y2]-pref[x1][y2]-pref[x2][y1] + pref[x1][y1];
// cout << sum << " ";
ans = max(ans, sum);
found = true;
}
}
// cout << "\n";
}
cout << (found ? ans : 0) << "\n";
return 0;
}