import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int M = scn.nextInt();
int N = scn.nextInt();
int K = scn.nextInt();
ArrayList<Integer> li = new ArrayList<Integer>();
HashMap<Integer,Node> map = new HashMap<Integer,Node>();
int temp = 0;
for(int i=1; i<M+1; i++) {
for(int j=1; j<N+1; j++) {
temp = scn.nextInt();
if(temp > 0) {
Node node = new Node();
node.setM(i);
node.setN(j);
map.put(temp, node);
li.add(temp);
}
}
}
Collections.sort(li);
Collections.reverse(li);
int maxNum = 0;
int tempGo = 0;
int tempBack = 0;
Node tempNode = map.get(li.get(0));
tempGo = tempNode.getM() + 1;
tempBack = tempNode.getN();
if(tempGo + tempBack <= K) {
maxNum = li.get(0);
}
for(int i=1; i<li.size(); i++) {
tempBack = map.get(li.get(i)).getM();
tempGo += Math.abs(map.get(li.get(i)).getN() - map.get(li.get(i-1)).getN()) + Math.abs(map.get(li.get(i)).getM() - map.get(li.get(i-1)).getM())+ 1;
if(tempBack + tempGo <= K) {
maxNum += li.get(i);
} else {
break;
}
}
System.out.print(maxNum);
}
}
class Node {
int m;
int n;
public void setM(int m) {
this.m = m;
}
public int getM() {
return m;
}
public void setN(int n) {
this.n = n;
}
public int getN() {
return n;
}
}