program peanuts;
var p,i,j,m,n,k,t,r,nowx,nowy,good:longint;
w:array[1..20,1..20] of integer;
x,y,e:array[1..400] of integer;
procedure change(var a,b:integer);
begin
a:=a+b;
b:=a-b;
a:=a-b;
end;
procedure qsort(l,r:longint);
var a,b,mid:longint;
begin
mid:=e[(l+r) div 2];
a:=l;b:=r;
repeat
while e[a]>mid do inc(a);
while e[b]<mid do dec(b);
if a<b then
begin
change(e[a],e[b]);
change(x[a],x[b]);
change(y[a],y[b]);
inc(a);dec(b);
end;until a>=b;
if a>l then qsort(a,r);
if b<r then qsort(l,b);
end;
begin
read(m,n,k);
p:=0;
for i:=1 to m do
begin
for j:=1 to n do
begin
read(w[i,j]);
if w[i,j]<>0 then
begin
inc(p);
e[p]:=w[i,j];
x[p]:=j;y[p]:=i;
end;
end;
end;
qsort(1,p);
nowx:=x[1];nowy:=0;
for i:=1 to p do
begin
t:=t+abs(y[i]-nowy)+abs(x[i]-nowx);
r:=y[i];
if t+r+1>k then break;
inc(t);
good:=good+e[i];
nowx:=x[i];
nowy:=y[i];
end;
write(good);
end.