main=getContents>>=putStrLn.g.map(map read.words).lines
g((w:d:_):s)=let v=foldr1(zipWith o)$map(m w d)s;k=minimum$map fst v in if k<0 then"No"else show$sum.map snd$v
m w d[x,y,z]=[let u=abs(i-x)+abs(j-y)in(u,z-u)|j<-[1..d],i<-[1..w]]
o(0,z)(_,u)=if z<u then(-1,z)else(0,z)
o(a,z)(0,u)=o(0,u)(a,z)
o(_,z)(a,u)=(a,max z u)

