local h,w=io.read("n","n")
local c={}
for i=0,9 do
    c[i]={}
    for j=0,9 do
        c[i][j]=io.read("n")
    end
end

--Warshall-Floyd法
for i=0,9 do
    for j=0,9 do
        for k=0,9 do
            c[i][j]=math.min(c[i][j],c[i][k]+c[k][j])
        end
    end
end

local counter=0
for i=1,h do
    for j=1,w do
        local input=io.read("n")
        if input>=0 then
            counter=counter+c[input][1]
        end
    end
end
print(counter)