#include <bits/stdc++.h>
using namespace std;
int n, m, k;
struct node
{
int x, y, w;
};
node a[405];
int cnt;
int ans;
bool cmp(node a, node b)
{
return a.w > b.w;
}
int main()
{
cin >> n >> m >> k;
int w;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
cin >> w;
if(w > 0)
{
a[++cnt].w = w;
a[cnt].x = i;
a[cnt].y = j;
}
}
}
sort(a + 1, a + cnt + 1, cmp);
int lastx = 0, lasty = a[1].y; //上一个花生的位置,初始放在和第一个花生同列的路上
for(int i = 1; i <= cnt; i++)
{
int t1 = abs(a[i].y - lasty) + abs(a[i].x - lastx) + 1; //走到下一个花生的位置并采摘所需时间
int t2 = a[i].x; //从下一个花生位置返回路边所需时间
if(k < t1 + t2) break; //剩下的时间不够采摘下一个花生且返回
ans += a[i].w; //够采则采摘
k -= t1; //剩余时间减去t1
lastx = a[i].x, lasty = a[i].y; //更新上一个花生位置
}
cout << ans;
return 0;
}