local mmi, mma = math.min, math.max
local h, w = io.read("*n", "*n")
local t = {}
local asked = {}
local len = {}
local inf = 1000000007
for i = 1, 10 do
  t[i] = {}
  asked[i] = false
  len[i] = inf
  for j = 1, 10 do
    t[i][j] = io.read("*n")
  end
end
len[2] = 0
for i = 1, 10 do
  local dst = false
  for j = 1, 10 do
    if not asked[j] then
      if not dst or len[j] < len[dst] then
        dst = j
      end
    end
  end
  asked[dst] = true
  for src = 1, 10 do
    len[src] = mmi(len[src], len[dst] + t[src][dst])
  end
end

local ret = 0
for i = 1, h * w do
  local a = 1 + io.read("*n")
  if 0 < a then
    ret = ret + len[a]
  end
end
print(ret)
