Problem:
Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

3 5
0 4 3 6 2 7 2 5 3 3
4 0 5 3 7 5 3 7 2 7
5 7 0 7 2 9 3 2 9 1
3 6 2 0 2 4 6 4 2 3
3 5 7 4 0 6 9 7 6 7
9 8 5 2 2 0 4 7 6 5
5 4 6 3 2 3 0 5 4 3
3 6 2 3 4 2 4 0 8 9
4 6 5 4 3 5 3 2 0 8
2 1 3 4 5 7 8 6 4 0
3 5 2 6 1
2 5 3 2 1
6 9 2 5 6

Sample Output 3

47

Solution in Rust (1.15.1) (judge verdict: Accepted)

fn main() {
  let (h, _) = get::tuple::<usize,usize>();
  let mat = get::lists::<isize>(10);
  let ass = get::lists::<isize>(h);

  let mut es = vec![];
  for i in 0 .. 10 {
    for j in 0 .. 10 {
      es.push((i+1, j+1, mat[i][j]))
    }
  }
  let g = Graph::new_labeled(10, &es); 
  let wf = g.warshall_floyd(); //println!("{:?}", wf); println!("{:?}", g.adj);

  let ans = ass.iter().flat_map(|v|v.iter().map(|&a|if a==-1 {0} else {wf[(a+1) as usize][2]})).sum::<isize>();
  println!("{}", ans)
}

struct Graph {
  size: usize,
  adj: Vec<Vec<(usize, isize)>>
}

impl Clone for Graph {
  fn clone(&self) -> Graph {
    Graph {
      size: self.size,
      adj: self.adj.clone()
    }
  }
}

#[allow(dead_code)]
impl Graph {
  fn new_nonlabeled(n: usize, edges: &[(usize, usize)]) -> Graph {
    let edges = edges.iter().map(|&(a,b)|(a,b,1)).collect::<Vec<_>>();
    Graph::new_labeled(n, &edges)
  }

  fn new_labeled(n: usize, edges: &[(usize, usize, isize)]) -> Graph {
    let mut g: Vec<Vec<(usize, isize)>> = vec![vec![]; n+1];
    for &(a, b, c) in edges {
      g[a].push((b,c));
      //g[b].push((a,c));  // delete for digraph
    }
    Graph {size: n, adj: g}
  }

  fn is_adjacent(&self, u: usize, v: usize) -> bool {
    self.adj[u].iter().any(|&(w,_)|w==v) || self.adj[v].iter().any(|&(w,_)|w==u)
  }

  fn is_connected(&self) -> bool {
    self.dfs(1).len() == self.size
  }

  fn is_hitofude(&self) -> bool {
    let deg_odd = self.adj.iter().filter(|vs|vs.len()%2==1).count();
    deg_odd == 0 || deg_odd == 2
  }

  fn bellman_ford(&self, s: usize) -> Vec<isize> {
    const INF: isize = std::isize::MAX >> 1;
    let edges = self.edges();
    let mut bf: Vec<isize> = vec![INF; self.size];
    bf[s] = 0;

    for _ in 1 .. self.size {
      for &(v, w, c) in &edges {
        bf[w] = std::cmp::min(bf[w], bf[v]+c)
      }
    }
    bf
  }

  fn dijkstra(&self, s: usize) -> Vec<isize> {
    use std::collections::BinaryHeap;

    const INF: isize = std::isize::MAX;
    let mut dk: Vec<isize> = vec![INF; self.size+1];
    dk[s] = 0;

    let mut pq = BinaryHeap::new();
    pq.push((0,s));

    while let Some((acc,v)) = pq.pop() {
      let acc = -acc;
      for &(w,c) in &self.adj[v] {
        let cand = acc + c;
        if cand < dk[w] {
          dk[w] = cand;
          pq.push((-cand,w));
        }
      }
    }
    dk
  }

  fn warshall_floyd(&self) -> Vec<Vec<isize>> {
    const INF: isize = std::isize::MAX >> 1;
    let mut wf: Vec<Vec<isize>> = vec![vec![INF; self.size+1]; self.size+1];
    for i in 1 .. self.size+1 {wf[i][i] = 0}

    for (next, i) in self.adj.iter().zip(0..) {
      for &(j, x) in next {
        wf[i][j] = x
      }
    }
    for k in 1 .. self.size+1 {
      for i in 1 .. self.size+1 {
        for j in 1 .. self.size+1 {
          wf[i][j] = std::cmp::min(wf[i][j], wf[i][k] + wf[k][j]);
        }
      }
    }
    wf
  }

  fn coloring2(&self) -> Option<(Vec<usize>, Vec<usize>)> {
    fn paint(v: usize, p: bool, g: &Graph, cv: &mut Vec<Option<bool>>) -> bool {
      match cv[v] {
        None => {
          let next = &g.adj[v];
          cv[v] = Some(p);
          next.iter().all(|&(w,_)|paint(w,!p,g,cv))},
        Some(q) => {q == p}
      }
    }

    let mut canvas: Vec<Option<bool>> = vec![None; self.size+1];
    let ans = paint(1, false, self, &mut canvas);
    let bs = canvas.iter().enumerate().filter(|&(_,&v)|v==Some(false)).map(|(i,_)|i).collect::<Vec<_>>();
    let ws = canvas.iter().enumerate().filter(|&(_,&v)|v==Some(true)).map(|(i,_)|i).collect::<Vec<_>>();
    if ans {Some((bs,ws))} else {None}
  }

  fn dfs(&self, s: usize) -> Vec<usize> {
    fn go(g: &Graph, current: usize, mut path: &mut Vec<usize>, mut visited: &mut Vec<bool>) {
      for &(next, _) in &g.adj[current] {
        if visited[next] {
          continue
        } else {
          visited[next] = true;
          path.push(next);
          go(&g, next, &mut path, &mut visited)
        }
      }
    }

    let mut path = vec![s];
    let mut visited = vec![false; self.size+1];
    visited[s] = true;
    go(&self, s, &mut path, &mut visited);
    path
  }

  fn bfs(&self, v: usize) -> Vec<usize> {
    use std::collections::VecDeque;
    let mut q = VecDeque::new();
    let mut buf = vec![];
    let mut vd = vec![false; self.size+1];
    vd[v] = true;
    q.push_back(v);
    while let Some(w) = q.pop_front() {
      buf.push(w);
      for &(next,_) in self.adj[w].iter() {
        if !vd[next] {
          q.push_back(next);
          vd[next] = true
        }
      }
    }
    buf
  }

  fn ultimate(&self, v: usize) -> usize {
    use std::collections::VecDeque;
    let mut q = VecDeque::new();
    let mut dist = 0;
    let mut vd = vec![false; self.size+1];
    vd[v] = true;
    q.push_back((v,dist));
    while let Some((w, d)) = q.pop_front() {
      dist = std::cmp::max(dist, d);
      for &(next,_) in self.adj[w].iter() {
        if !vd[next] {
          q.push_back((next, d+1));
          vd[next] = true
        }
      }
    }
    dist
  }

  fn edges(&self) -> Vec<(usize, usize, isize)> {
    let mut buf = vec![];
    for (i, next) in self.adj.iter().enumerate() {
      for &(j, x) in next {
        buf.push((i, j, x))
      }
    }
    buf
  }

  fn cut(&mut self, v: usize, w: usize) {
    self.adj[v].retain(|&(t,_)| t != w);
    self.adj[w].retain(|&(t,_)| t != v);
  }
}

#[allow(dead_code)]
mod get {
  use std::io::*;
  use std::str::*;

  pub fn val<T: FromStr>() -> T {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    buf.trim_right().parse::<T>().ok().unwrap()
  }

  pub fn vals<T: FromStr>(n: usize) -> Vec<T> {
    let mut vec: Vec<T> = vec![];
    for _ in 0 .. n {
      vec.push(val());
    }
    vec
  }

  pub fn tuple<T1: FromStr, T2: FromStr>() -> (T1, T2) {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    let mut it = buf.trim_right().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    (x, y)
  }

  pub fn tuples<T1: FromStr, T2: FromStr>(n: usize) -> Vec<(T1, T2)> {
    let mut vec: Vec<(T1, T2)> = vec![];
    for _ in 0 .. n {
      vec.push(tuple());
    }
    vec
  }

  pub fn tuple3<T1: FromStr, T2: FromStr, T3: FromStr>() -> (T1, T2, T3) {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    let mut it = buf.trim_right().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    let z = it.next().unwrap().parse::<T3>().ok().unwrap();
    (x, y, z)
  }

  pub fn tuple3s<T1: FromStr, T2: FromStr, T3: FromStr>(n: usize) -> Vec<(T1, T2, T3)> {
    let mut vec: Vec<(T1, T2, T3)> = vec![];
    for _ in 0 .. n {
      vec.push(tuple3());
    }
    vec
  }

  pub fn list<T: FromStr>() -> Vec<T> {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    buf.trim_right().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
  }

  pub fn lists<T: FromStr>(h: usize) -> Vec<Vec<T>> {
    let mut mat: Vec<Vec<T>> = vec![];
    for _ in 0 .. h {
      mat.push(list());
    }
    mat
  }

  pub fn chars() -> Vec<char> {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    buf.trim_right().chars().collect()
  }
}