#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            line.pop();
            line
        }
    )
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let iter = line.split_whitespace();
            iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    )
}

mod dijkstra {
    use std::cmp::Ordering;
    use std::collections::BinaryHeap;

    #[derive(Copy, Clone, Eq, PartialEq)]
    pub struct State<T>
    {
        pub cost: usize,
        pub pos: T
    }

    pub trait ContextTrait<T>
    {
        fn cost(&self, pos: T) -> usize;
        fn update_cost(&mut self, s: &State<T>);
        fn neighbours(&self, s: &State<T>) -> Vec<State<T>>;

        fn run(&mut self, start: State<T>, end: T) -> Option<usize>
            where T: Copy+Ord {
            let mut heap = BinaryHeap::new();
            heap.push(start);

            while let Some(State {cost, pos}) = heap.pop() {
                if pos == end {
                    return Some(cost);
                }

                if cost > self.cost(pos) { continue; }

                for next in self.neighbours(&State {cost: cost, pos: pos}) {
                    if next.cost < self.cost(next.pos) {
                        self.update_cost(&next);
                        heap.push(next);
                    }
                }
            }
            None
        }
    }

    impl<T> Ord for State<T>
        where T: Copy+Ord {
        fn cmp(&self, other: &State<T>) -> Ordering {
            let c = other.cost.cmp(&self.cost);

            if c == Ordering::Equal {
                self.pos.cmp(&other.pos)
            } else {
                c
            }
        }
    }

    impl<T: Copy+Ord> PartialOrd for State<T> {
        fn partial_cmp(&self, other: &State<T>) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }
}

use dijkstra::*;

struct Context<'a> {
    dtable: &'a Vec<Vec<usize>>,
    ctable: Vec<usize>
}

impl<'a> ContextTrait<usize> for Context<'a> {
    fn cost(&self, pos: usize) -> usize {
        self.ctable[pos]
    }

    fn update_cost(&mut self, s: &State<usize>) {
        self.ctable[s.pos] = s.cost;
    }

    fn neighbours(&self, s: &State<usize>) -> Vec<State<usize>> {
        (0..10).filter(|&n| n != s.pos).map(|n| State{cost: s.cost + self.dtable[s.pos][n], pos:n}).collect()
    }
}

fn solve(cs: Vec<Vec<usize>>, aa: Vec<Vec<i32>>) -> usize {

    let mut costs = vec![0; 10];

    for i in 0..10 {
        let ctable = vec![std::usize::MAX; 10];

        let mut ctx = Context{dtable: &cs, ctable: ctable};
        costs[i] = ctx.run(State{cost:0, pos:i}, 1).unwrap();
    }

//    println!("{:?}", costs);

    aa.iter().fold(0, |sum, line| sum +
                   line.iter().filter(|&a| *a != -1).fold(0, |s, a| s + costs[*a as usize]))
}

fn main() {
    let (h,_w) = parse_line!(usize, usize);

    let css = (0..10).map(|_| parse_vec!(usize)).collect();
    let aa = (0..h).map(|_| parse_vec!(i32)).collect();

    println!("{}", solve(css, aa));
}

#[test]
fn solve_test() {
    let mut dtable = vec![vec![9; 10]; 10];
    for i in 0..10 {
        dtable[i][i] = 0;
    }
    dtable[4][9] = 2;
    dtable[9][1] = 2;
    dtable[8][4] = 2;

    assert_eq!(solve(dtable, vec![vec![-1,-1,-1,-1],
                                  vec![8,1,1,8]]), 12);
}


Measured memory_bytes: 4352