#![allow(clippy::needless_range_loop)]
#![allow(unused_macros)]
#![allow(dead_code)]

use proconio::input;
use proconio::marker::*;

use std::cmp::*;
use std::collections::binary_heap::BinaryHeap;
#[derive(Copy, Clone, Eq, PartialEq)]
struct GraphEdge {
    node_to: usize,
    cost: isize,
}
impl Ord for GraphEdge {
    fn cmp(&self, other: &GraphEdge) -> Ordering {
        other.cost.cmp(&self.cost)
    }
}
impl PartialOrd for GraphEdge {
    fn partial_cmp(&self, other: &GraphEdge) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl GraphEdge {
    fn new(node_to: usize, cost: isize) -> Self {
        Self { node_to, cost }
    }
}
struct Graph {
    pub edges: Vec<Vec<GraphEdge>>,
    pub nodes: usize,
}
impl Graph {
    fn new(nodes: usize) -> Self {
        Self {
            edges: vec![vec![]; nodes],
            nodes,
        }
    }
    fn edge_undirected(&mut self, node_a: usize, node_b: usize) {
        self.edges[node_a].push(GraphEdge::new(node_b, 1));
        self.edges[node_b].push(GraphEdge::new(node_a, 1));
    }
    fn edge_directed(&mut self, node_from: usize, node_to: usize) {
        self.edges[node_from].push(GraphEdge::new(node_to, 1));
    }
    fn edge_undirected_costed(&mut self, node_a: usize, node_b: usize, cost: isize) {
        self.edges[node_a].push(GraphEdge::new(node_b, cost));
        self.edges[node_b].push(GraphEdge::new(node_a, cost));
    }
    fn edge_directed_costed(&mut self, node_from: usize, node_to: usize, cost: isize) {
        self.edges[node_from].push(GraphEdge::new(node_to, cost));
    }
    fn dijkstra(&self, node_from: usize) -> Vec<isize> {
        let mut costs = vec![std::isize::MAX; self.nodes];
        let mut heap: BinaryHeap<GraphEdge> = BinaryHeap::new();
        heap.push(GraphEdge::new(node_from, 0));
        while let Some(GraphEdge { cost, node_to }) = heap.pop() {
            if cost >= costs[node_to] {
                continue;
            }
            costs[node_to] = cost;
            for edge in &self.edges[node_to] {
                let next = GraphEdge::new(edge.node_to, cost + edge.cost);
                heap.push(next);
            }
        }
        costs
    }
    fn warshall_floyd(&self) -> Vec<Vec<isize>> {
        let mut costs = vec![vec![std::isize::MAX; self.nodes]; self.nodes];
        for i in 0..self.nodes {
            costs[i][i] = 0;
        }
        for (node_from, edges) in self.edges.iter().enumerate() {
            for GraphEdge { node_to, cost } in edges.iter() {
                costs[node_from][*node_to] = *cost;
            }
        }
        for k in 0..self.nodes {
            for i in 0..self.nodes {
                for j in 0..self.nodes {
                    costs[i][j] =
                        std::cmp::min(costs[i][j], costs[i][k].saturating_add(costs[k][j]));
                }
            }
        }
        costs
    }
}

fn main() {
    input! {
        h: usize,
        w: usize,
        c: [[isize; 10]; 10],
        a: [isize; h * w],
    }
    let mut graph = Graph::new(10);
    for (node_from, costs) in c.iter().enumerate() {
        for (node_to, cost) in costs.iter().enumerate() {
            graph.edge_directed_costed(node_from, node_to, *cost);
        }
    }
    let costs = graph.warshall_floyd();
    let ans: isize = a
        .iter()
        .filter(|x| **x != -1)
        .map(|x| costs[*x as usize][1])
        .sum();
    println!("{}", ans);
}


Measured memory_bytes: 2500