program ex;
type
ss=array[1..2500]of integer;
sz=array[1..50,1..50]of integer;
var
a:sz;
f:ss;
f1,f2:text;
n,p,h:longint;
min,mint,max:longint;
procedure init;
var
i,j:integer;
begin
min:=maxint;
mint:=maxint;
max:=0;
assign(f1,'eldrethalas.in');
assign(f2,'eldrethalas.out');
reset(f1);
rewrite(f2);
readln(f1,n,p,h);
for i:=1 to p do
begin
readln(f1,f[i]);
if f[i]<min then min:=f[i];
if f[i]>max then max:=f[i];
end;
for i:=1 to n do
begin
for j:=1 to n do
read(f1,a[i,j]);
readln(f1);
end;
end;
function pd:boolean;
var
i:integer;
begin
pd:=true;
if min>=h then exit(false);
for i:=1 to n do
if f[a[1,i]]<mint then mint:=f[a[1,i]];
if mint>=h then exit(false);
end;
begin
init;
if pd=false then writeln(f2,'NO')
else writeln(f2,h-max);
close(f2);
end.