Buggy python submission (verdict: Time Limit Exceeded):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

def func(num, goto, ignore):
    if num == goto:
        return 0
    res = []
    for i in range(10):
        if i in ignore:
            continue
        nignore  = deepcopy(ignore)
        nignore.append(i)
        #print(num, i, goto, nignore)
        plus = func(i, goto, nignore) if d[i][goto] == -1 else d[i][goto]
        #d[i][goto] = plus
        tmp = c[num][i] + plus
        res.append(tmp)
    return min(res)

h, w = MAP()
c = [LIST() for _ in range(10)]
a = [LIST() for _ in range(h)]
d = [[-1 for _ in range(10)] for _ in range(10)]
for i in range(10): #iから1に変更する最小コスト
    if i == 1:
        continue
    res = func(i, 1, [i])
    d[i][1] = min(c[i][1], res)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1 or a[i][j] == 1:
            continue
        ans += d[a[i][j]][1]
print(ans)

Fixed submission:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]

def func(num, goto, ignore):
    if num == goto:
        return 0
    res = []
    for i in range(10):
        if i in ignore:
            continue
        nignore  = deepcopy(ignore)
        nignore.append(i)
        #print(num, i, goto, nignore)
        plus = func(i, goto, nignore) if d[i][goto] == -1 else d[i][goto]
        #d[i][goto] = plus
        tmp = c[num][i] + plus
        res.append(tmp)
    return min(res)

h, w = MAP()
c = [LIST() for _ in range(10)]
a = [LIST() for _ in range(h)]
d = [[-1 for _ in range(10)] for _ in range(10)]
for i in range(10): #iから1に変更する最小コスト
    if i == 1:
        continue
    res = func(i, 1, [i])
    d[i][1] = min(c[i][1], res)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j] == -1 or a[i][j] == 1:
            continue
        ans += d[a[i][j]][1]
print(ans)