let h, w = Scanf.scanf " %d %d" @@ fun a b -> a, b
let css = Array.init 10 @@ fun _ -> Array.init 10 @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let a_ss = Array.init h @@ fun _ -> Array.init w @@ fun _ -> Scanf.scanf " %d" @@ (+) 0
let ans = ref 0
let _ =
  for k = 0 to 9 do
    for i = 0 to 9 do
      for j = 0 to 9 do css.(i).(j) <- min css.(i).(j) @@ css.(i).(k) + css.(k).(j) done done done;
  for i = 0 to h - 1 do
    for j = 0 to w - 1 do if a_ss.(i).(j) <> -1 then ans := !ans + css.(a_ss.(i).(j)).(1) done done;
  Printf.printf "%d\n" !ans