from scipy.sparse import*

h,w,*a=list(map(int,open(0).read().split()))

c=csgraph.dijkstra(csr_matrix([a[i*10:-~i*10]for i in range(10)]))

print((int(sum([c[i][1]for i in a[100:]if i>=0]))))
from scipy.sparse import*

h,w,*a=list(map(int,open(0).read().split()))

c=csgraph.dijkstra(csr_matrix([a[i*10:-~i*10]for i in range(10)]))

print((int(sum(c[i][1]for i in a[100:]if i>=0))))
from scipy.sparse import *

h, w, *a = list(map(int, open(0).read().split()))
c = csgraph.dijkstra(csr_matrix([a[i * 10 : -~i * 10] for i in range(10)]))
print((int(sum([c[i][1] for i in a[100:] if i >= 0]))))

from scipy.sparse import *

h, w, *a = list(map(int, open(0).read().split()))
c = csgraph.dijkstra(csr_matrix([a[i * 10 : -~i * 10] for i in range(10)]))
print((int(sum(c[i][1] for i in a[100:] if i >= 0))))

-print((int(sum([c[i][1] for i in a[100:] if i >= 0])))) +print((int(sum(c[i][1] for i in a[100:] if i >= 0))))