CodeEff/ECCO huggingface dataset:
# Original solution
n, q = list(map(int, input().split()))
cnt = [0 for _ in range(n + 1)]
cnt[0] = -1
mx = [0, -1]


def spam():
    max_v = max(cnt)
    idx = min([i for i, value in enumerate(cnt) if value == max_v])
    return idx, max_v

for _ in range(q):
    a, v = list(map(int, input().split()))
    cnt[a] += v
    if mx[0] == a and v > 0:
        mx[1] += v
    else:
        mx[0], mx[1] = spam()

    if cnt[a] > mx[1]:
        print(("%d %d" % (a, cnt[a])))
        mx[0], mx[1] = a, cnt[a]
    else:
        print(("%d %d" % (mx[0], mx[1])))

# Optimized solution
n, q = list(map(int, input().split()))
cnt = [0 for _ in range(n + 1)]
cnt[0] = -9999999999
mx = [0, cnt[0]]


def spam():
    max_v = max(cnt)
    idx = min([i for i, value in enumerate(cnt) if value == max_v])
    return idx, max_v

for _ in range(q):
    a, v = list(map(int, input().split()))
    cnt[a] += v
    if mx[0] == a and v > 0:
        mx[1] += v

    elif mx[0] == a:
        mx[0], mx[1] = spam()

    elif mx[1] < cnt[a]:
        mx[0], mx[1] = a, cnt[a]

    elif mx[1] == cnt[a] and mx[0] > a:
        mx[0], mx[1] = a, cnt[a]

    print(("%d %d" % (mx[0], mx[1])))
# Original solution
#!/usr/bin/env python

from sys import stdin, exit, maxsize


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [(1, -maxsize)] * (n//1000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())
        a -= 1
        base[a] += v

        b = a - a % 1000
        index = value = -maxsize
        for i, v in enumerate(base[b:b+1000], b+1):
            if value < v:
                value = v
                index = i
        midway[b//1000] = (index, value)

        index = value = -maxsize
        for i, v in midway:
            if value < v:
                value = v
                index = i
        print(index, value)
    exit()


if __name__ == '__main__':
    main()

# Optimized solution
#!/usr/bin/env python

from sys import stdin, exit, maxsize


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [(1, -maxsize)] * (n//10000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())
        a -= 1
        base[a] += v

        b = a - a % 10000
        index = value = -maxsize
        for i, v in enumerate(base[b:b+10000], b+1):
            if value < v:
                value = v
                index = i
        midway[b//10000] = (index, value)

        index = value = -maxsize
        for i, v in midway:
            if value < v:
                value = v
                index = i
        print(index, value)
    exit()


if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python

from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())
    #assert 1 <= n <= 1000000
    #assert 1 <= q <= 100000

    base = [0] * n

    for _ in range(q):
        a, v = (int(s) for s in readline().split())
        #assert 1 <= a <= n
        #assert -100 <= v <= 100

        base[a-1] += v

        index = 1
        value = base[0]
        for i, v in enumerate(base, 1):
            if value < v:
                value = v
                index = i

        print(index, value)
    exit()


if __name__ == '__main__':
    main()

# Optimized solution
#!/usr/bin/env python

from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [0] * (n//1000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())

        a -= 1
        base[a] += v

        b = a - a % 1000
        value = base[b]
        index = b
        for i, v in enumerate(base[b:b+1000], b):
            if value < v:
                value = v
                index = i
        midway[b//1000] = index

        index = midway[0]
        value = base[index]
        for i in midway:
            if value < base[i]:
                value = base[i]
                index = i

        print(index+1, value)
    exit()


if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python

from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [0] * (n//1000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())

        a -= 1
        base[a] += v

        b = a - a % 1000
        value = base[b]
        index = b
        for i, v in enumerate(base[b:b+1000], b):
            if value < v:
                value = v
                index = i
        midway[b//1000] = index

        index = midway[0]
        value = base[index]
        for i in midway:
            if value < base[i]:
                value = base[i]
                index = i

        print(index+1, value)
    exit()


if __name__ == '__main__':
    main()

# Optimized solution
#!/usr/bin/env python

from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [0] * (n//1000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())

        a -= 1
        base[a] += v

        b = a - a % 1000
        value = base[b]
        index = b
        end = b + 1000 if b + 1000 < n else n
        for i in range(b, end):
            if value < base[i]:
                value = base[i]
                index = i
        midway[b//1000] = index

        index = midway[0]
        value = base[index]
        for i in midway:
            if value < base[i]:
                value = base[i]
                index = i

        print(index+1, value)
    exit()


if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python

from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [0] * (n//1000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())

        a -= 1
        base[a] += v

        b = a - a % 1000
        index = b
        value = base[b]
        end = b + 1000 if b + 1000 < n else n
        for i in range(b, end):
            if value < base[i]:
                value = base[i]
                index = i
        midway[b//1000] = index

        index = midway[0]
        value = base[index]
        for i in midway:
            if value < base[i]:
                value = base[i]
                index = i

        print(index+1, value)
    exit()


if __name__ == '__main__':
    main()

# Optimized solution
#!/usr/bin/env python

from sys import stdin, exit


def main(readline=stdin.readline):
    n, q = (int(s) for s in readline().split())

    base = [0] * n
    midway = [0] * (n//100 + 1)
    last = [0] * (n//10000 + 1)

    for _ in range(q):
        a, v = (int(s) for s in readline().split())

        a -= 1
        base[a] += v

        b = a - a % 100

        index = b
        value = base[b]
        end = b + 100 if b + 100 < n else n
        for i in range(b, end):
            if value < base[i]:
                value = base[i]
                index = i
        midway[b//100] = index

        c = a - a % 10000
        cc = c // 100

        index = c
        value = base[c]
        for i in midway[cc:cc+100]:
            if value < base[i]:
                value = base[i]
                index = i
        last[c//10000] = index

        index = last[0]
        value = base[index]
        for i in last:
            if value < base[i]:
                value = base[i]
                index = i

        print(index+1, value)
    exit()


if __name__ == '__main__':
    main()
# Original solution
def f(): return list(map(int,input().split()))
n,q=f()
A=[0]*(n+1)
a0,b0=0,0
for _ in range(q):
  a,b=f()
  A[a]+=b
  if A[a]>b0: a0,b0=a,A[a]
  else:
    b0=max(A)
    a0=A.index(b0)
  print(a0,b0)

# Optimized solution
def f(): return list(map(int,input().split()))
n,q=f()
A=[[0,-i] for i in range(n+1)]
for _ in range(q):
  a,b=f()
  A[a][0]+=b
  m0,m1=sorted(A,key=lambda A:[A[0],A[1]])[-1]
  print(-m1,m0)
# Original solution
def f(): return list(map(int,input().split()))
n,q=f()
a0,v0=0,0
A=[0]*(n+1)
for _ in range(q):
  a,v=f()
  A[a]+=v
  c=A[a]
  if a==a0 and c<v0:
    v0=max(A)
    a0=A.index(v0)
  elif c>v0: a0,v0=a,c
  elif c==v0 and a<a0: a0=a
  print(a0,v0)

# Optimized solution
def f(): return list(map(int,input().split()))
n,q=f()
a0,v0=0,0
A=[0]*(n+1)
for _ in range(q):
  a,v=f()
  A[a]+=v
  if a==a0 and A[a]<v0:
    v0=max(A)
    a0=A.index(v0)
  elif A[a]>v0:
    v0=A[a]
    a0=a
  elif A[a]==v0 and a<a0: a0=a
  print(a0,v0)
# Original solution
for _ in range(int(eval(input()))):
    a=sorted(map(int,input().split()))
    print(('YES' if a[0]**2+a[1]**2==a[2]**2 else'NO'))

# Optimized solution
for _ in[0]*int(eval(input())):a,b,c=sorted(map(int,input().split()));print((['NO','YES'][a*a+b*b==c*c]))
# Original solution
for _ in[0]*int(eval(input())):
 a,b,c=sorted(map(int,input().split()))
 print((['NO','YES'][a*a+b*b==c*c]))


# Optimized solution
import sys
eval(input())
for e in sys.stdin:
 a,b,c=sorted(map(int,e.split()))
 print((['NO','YES'][a*a+b*b==c*c]))
# Original solution
for _ in[0]*int(eval(input())):
 a,b,c=sorted(map(int,input().split()))
 print((['NO','YES'][a*a+b*b==c*c]))


# Optimized solution
import sys
eval(input())
for a,b,c in [sorted(map(int,x.split())) for x in sys.stdin]:
 print((['NO','YES'][a*a+b*b==c*c]))
# Original solution
for i in range(0, int(eval(input()))):
    sidelen = [int(j) for j in input().split(" ")]
    sidelen.sort(reverse=True)
    if(sidelen[0]**2 == sidelen[1]**2 + sidelen[2]**2):
        print("YES")
    else:
        print("NO")

# Optimized solution
import sys
for i in sys.stdin:
    try:
        sidelen = [int(j) for j in i.split(" ")]
        sidelen.sort(reverse=True)
        if(sidelen[0]**2 == sidelen[1]**2 + sidelen[2]**2):
            print("YES")
        else:
            print("NO")
    except:
        continue
# Original solution
N = int(eval(input()))
for _ in range(N):
    a, b, c = sorted(list(map(int, input().split())))
    print(("YES" if a*a + b*b == c*c else "NO"))

# Optimized solution
N = int(eval(input()))
for _ in range(N):
    a, b, c = sorted(map(int, input().split()))
    print(("YES" if a*a + b*b == c*c else "NO"))
# Original solution
for n in range(eval(input())):
  a,b,c=sorted(map(int,input().split()))
  s="YES" if a*a+b*b==c*c else "NO"
  print(s)

# Optimized solution
for n in range(eval(input())):
  a,b,c=sorted(map(int,input().split()))
  print("YES" if a*a + b*b == c*c else "NO")
# Original solution
import sys
r = []
n = int(eval(input()))
l = sys.stdin.readlines()
for i in l:
  x, y, z = sorted([x*x for x in list(map(int, i.split()))])
  if x + y == z:
      print("YES")
  else:
      print("NO")

# Optimized solution
import sys

n = int(input())
l = sys.stdin.readlines()
s = ""
for i in l:
  x, y, z = sorted(map(lambda x:x*x,map(int, i.split())))
  if x + y == z:
      s += "YES\n"
  else:
      s += "NO\n"

print(s,end="")
# Original solution
H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]

counter = {i: 0 for i in range(-1, 10)}
for i in range(H):
    tmp = list(map(int, input().split()))
    for j in range(W):
        counter[tmp[j]] += 1
MAX = 10**4
INF = float("inf")
def f(start):
    if start == 1:
        return 0
    dp = [[INF]*MAX for _ in range(10)]
    dp[start][0] = 0
    for i in range(1, MAX):
        for new in range(10):
            dp[new][i] = min(dp[num][i-1] + c[num][new] for num in range(10))
    return min(dp[1])

ans = 0
for i in range(10):
    if counter[i] == 0:
        continue
    else:
        ans += counter[i] * f(i)
print(ans)

# Optimized solution
"""ABC079D Wall 別解
ワーシャルフロイドでやった場合
"""

H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]

counter = {i: 0 for i in range(-1, 10)}
for i in range(H):
    tmp = list(map(int, input().split()))
    for j in range(W):
        counter[tmp[j]] += 1

dp = c.copy()
for k in range(10):
    for i in range(10):
        for j in range(10):
            dp[i][j] = min(
                dp[i][j],
                dp[i][k] + dp[k][j]
            )

ans = sum([counter[i]*dp[i][1] for i in range(10)])
print(ans)
# Original solution
h, w = list(map(int, input().split()))
c = [0]*10
for i in range(10):
  c[i] = [int(x) for x in input().split()]

for _ in range(10):
  for i in range(10):
    for j in range(10):
      for k in range(10):
        c[i][j] = min([c[i][j], c[i][k]+c[k][j]])

ans = 0
for i in range(h):
  a = [int(x) for x in input().split()]
  for j in range(w):
    if a[j] == -1:
      continue
    else:
      ans += c[a[j]][1]

print(ans)

# Optimized solution
h, w = list(map(int, input().split()))
c = [0]*10
for i in range(10):
  c[i] = [int(x) for x in input().split()]


for k in range(10):
  for i in range(10):
    for j in range(10):
      c[i][j] = min([c[i][j], c[i][k]+c[k][j]])

ans = 0
for i in range(h):
  a = [int(x) for x in input().split()]
  for j in range(w):
    if a[j] == -1:
      continue
    else:
      ans += c[a[j]][1]

print(ans)
# Original solution
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]

A = []
for _ in range(H):
    A += list(map(int, input().split()))

for i in range(10):
    for j in range(10):
        for k in range(10):
            C[j][k] = min(C[j][k], C[j][i]+C[i][k])

from collections import Counter
Co = Counter(A)
ans = 0
for a, c in Co.most_common():
    if a==-1 or a==1:
        continue
    ans += C[a][1]*c
print(ans)

# Optimized solution
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return list(map(int, input().split()))
sys.setrecursionlimit(10**9)

from heapq import heappop, heappush
H, W = mapint()
Cs = [list(mapint()) for _ in range(10)]
lis = []
for h in range(H):
    lis.extend(list(mapint()))

def dijkstra(start):
    Q = [(0, start)]
    checked = [0]*10
    while Q:
        d, v = heappop(Q)
        checked[v] = 1
        if v==1:
            return d
        for i in range(10):
            if not checked[i]:
                heappush(Q, (d+Cs[v][i], i))

costs = [0]*10
for i in range(10):
    costs[i] = dijkstra(i)

from collections import Counter
c = Counter(lis)
ans = 0
for i in range(10):
    ans += costs[i]*c[i]
print(ans)
# Original solution
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for i in range(10)]
G = [list(map(int, input().split())) for i in range(H)]

for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])

ans = 0
for row in G:
    for x in row:
        if x >= 0:
            ans += C[x][1]

print(ans)


# Optimized solution
H, W = list(map(int, input().split()))
C = [list(map(int, input().split())) for _ in range(10)]
A = [list(map(int, input().split())) for _ in range(H)]


for k in range(10):
    for i in range(10):
        for j in range(10):
            C[i][j] = min(C[i][j], C[i][k] + C[k][j])


ans = 0
for h in range(H):
    for w in range(W):
        if A[h][w] == -1:
            continue
        ans += C[A[h][w]][1]

print(ans)
# Original solution
#!/usr/bin/env python3


def main():
    H, W = list(map(int, input().split()))
    INF = 10**12
    c = [None for _ in range(10)]
    for i in range(10):
        c[i] = list(map(int, input().split()))

    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k]+c[k][j])
    
    ans = 0
    for i in range(H):
        A = list(map(int, input().split()))
        for j in range(W):
            if A[j] != -1:
                ans += c[A[j]][1]
    print(ans)

if __name__ == "__main__":
    main()


# Optimized solution
from heapq import heappop, heappush

H, W = list(map(int, input().split()))
c =  [list(map(int, input().split())) for _ in range(10)]

# Dijkstra法で1からの距離を求める
INF = 10**12
q = [(0,1)] # 始点のコストとindex
d = [INF] * 10
d[1] = 0

while len(q) > 0:
    cost, u = heappop(q)
    #print(u,c[u])
    for v in range(10):
        if v == u: continue
        #print(i,v)
        tmp = d[u] + c[v][u]
        #print(v,tmp)
        if tmp < d[v]:
            d[v] = tmp
            heappush(q, (d[v], v))
    #print(d)

A =  [0]*10
for i in range(H):
    tmp = list(map(int, input().split()))
    for j in range(W):
        if tmp[j] >= 0:
            A[tmp[j]] += 1

ans = 0
for i in range(10):
    ans += A[i]*d[i]
print(ans)
# Original solution
import heapq
import sys

H, W = list(map(int, input().split()))
a = [[] for i in range(10)]
for i in range(10):
	c = [int(j) for j in input().split()]
	for j in range(10):
		a[i].append([c[j], j])
ans = 0
for i in range(H):
	A = [int(j) for j in input().split()]
	for j in A:
		if j == -1:
			continue
		b = [[0, j]]
		c = [False] * 10
		d = [sys.maxsize] * 10
		d[j] = 0
		while len(b):
			e = heapq.heappop(b)
			c[e[1]] = True
			for k in a[e[1]]:
				if c[k[1]]:
					continue
				if d[e[1]] + k[0] < d[k[1]]:
					d[k[1]] = d[e[1]] + k[0]
					heapq.heappush(b, [d[k[1]], k[1]])
		ans += d[1]
print(ans)

# Optimized solution
H, W = list(map(int, input().split()))
c = [[int(j) for j in input().split()] for i in range(10)]
for i in range(10):
	for j in range(10):
		if i == j:
			continue
		for k in range(10):
			if j == k:
				continue
			if c[i][k] + c[j][i] < c[j][k]:
				c[j][k] = c[i][k] + c[j][i]
ans = 0
for i in range(H):
	A = [int(j) for j in input().split()]
	for j in A:
		if j == -1:
			continue
		ans += c[j][1]
print(ans)
# Original solution
H, W = (int(i) for i in input().split())
#m = [[int(i) for i in input().split] for i in range(1)]
m = []
for i in range(10):
    m.append(list(map(int, input().split())))
wall = []
for i in range(H):
	wall.append(list(map(int, input().split())))
# wall = [[int(i) for i in input().split] for i in range(H)]
cost = 0

def search(i,current_cost,min_cost):
	for j in range(10):
		if i != j:
			if current_cost + m[i][j] < min_cost:
				if j == 1:
					min_cost = current_cost + m[i][j]
					# return current_cost + m[i][j]
				else:
					tmp = search(j, current_cost + m[i][j], min_cost)
					if tmp < min_cost:
						min_cost = tmp
	return min_cost
for i in range(H):
	for j in range(W):
		if wall[i][j] != 1 and wall[i][j] != -1:
			cost += search(wall[i][j], 0, m[wall[i][j]][1])

print(cost)

# Optimized solution
H, W = (int(i) for i in input().split())
#m = [[int(i) for i in input().split] for i in range(1)]
m = []
for i in range(10):
    m.append(list(map(int, input().split())))
wall = []
for i in range(H):
	wall.append(list(map(int, input().split())))
# wall = [[int(i) for i in input().split] for i in range(H)]
cost = 0

for k in range(10):
    for i in range(10):
        for j in range(10):
            if m[i][j] > m[i][k] + m[k][j]:
                m[i][j] = m[i][k] + m[k][j]

for i in range(H):
	for j in range(W):
		if wall[i][j] != 1 and wall[i][j] != -1:
			cost += m[wall[i][j]][1]

print(cost)
# Original solution
h,w=list(map(int,input().split()))
c=[]
for i in range(10):
    l=[int(i) for i in input().split()]
    c.append(l)
a=[]
for i in range(h):
    l=[int(i) for i in input().split()]
    a.append(l)
p=[0]*10
for i in range(10):
    if i==1: continue
    d=[1003]*10
    d[i]=0
    q=set([i])
    while 1:
        if len(q)==0: break
        x=q.pop()
        for j in range(10):
            if i==j: continue
            if (d[j]>d[x]+c[x][j]):
                d[j]=d[x]+c[x][j]
                q.add(j)
    p[i]=d[1]
ans=0
for i in range(h):
    for j in range(w):
        if a[i][j]==-1: pass
        else: ans+=p[a[i][j]]
print(ans)

# Optimized solution
def sol():
    h,w=list(map(int,input().split()))
    c=[]
    for i in range(10):
        l=[int(i) for i in input().split()]
        c.append(l)
    a=[]
    for i in range(h):
        l=[int(i) for i in input().split()]
        a.append(l)
    p=[0]*10
    for i in range(10):
        if i==1: continue
        d=[1003]*10
        d[i]=0
        q=set([i])
        while 1:
            if len(q)==0: break
            x=q.pop()
            for j in range(10):
                if i==j: continue
                if (d[j]>d[x]+c[x][j]):
                    d[j]=d[x]+c[x][j]
                    q.add(j)
        p[i]=d[1]
    ans=0
    for i in range(h):
        for j in range(w):
            if a[i][j]==-1: pass
            else: ans+=p[a[i][j]]
    print(ans)

if __name__=="__main__":
    sol()
# Original solution
def sol():
    h,w=list(map(int,input().split()))
    c=[]
    for i in range(10):
        l=[int(i) for i in input().split()]
        c.append(l)
    a=[]
    for i in range(h):
        l=[int(i) for i in input().split()]
        a.append(l)
    p=[0]*10
    for i in range(10):
        if i==1: continue
        d=[1003]*10
        d[i]=0
        q=set([i])
        while 1:
            if len(q)==0: break
            x=q.pop()
            for j in range(10):
                if i==j: continue
                if (d[j]>d[x]+c[x][j]):
                    d[j]=d[x]+c[x][j]
                    q.add(j)
        p[i]=d[1]
    ans=0
    for i in range(h):
        for j in range(w):
            if a[i][j]==-1: pass
            else: ans+=p[a[i][j]]
    print(ans)

if __name__=="__main__":
    sol()


# Optimized solution
def sol():
    h,w=list(map(int,input().split()))
    c=[list(map(int,input().split())) for i in range(10)]
    a=[list(map(int,input().split())) for i in range(h)]
    for k in range(10):
        for i in range(10):
            for j in range(10):
                #if (c[i][k]!=0 and c[k][j]!=0): c[i][j]=min(c[i][j],c[i][k]+c[k][j])
                c[i][j]=min(c[i][j],c[i][k]+c[k][j])
    ans=0
    for i in range(h):
        for j in range(w):
            if a[i][j]==-1: pass
            else: ans+=c[a[i][j]][1]
    print(ans)

if __name__=="__main__":
    sol()
# Original solution
def main():
    h, w, *ca = list(map(int, open(0).read().split()))
    it = list(zip(*[iter(ca[:100])] * 10))
    *c, = [list(i) for i in it]
    a = ca[100:]

    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])

    ans = 0
    cost = {i: c[i][1] for i in [0, 2, 3, 4, 5, 6, 7, 8, 9]}

    for i in a:
        if i in [-1, 1]:
            continue
        ans += cost[i]

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    h, w, *ca = list(map(int, open(0).read().split()))
    it = list(zip(*[iter(ca[:100])] * 10))
    *c, = [list(i) for i in it]
    a = ca[100:]

    for k in range(10):
        for i in range(10):
            for j in range(10):
                c[i][j] = min(c[i][j], c[i][k] + c[k][j])

    ans = 0
    cost = {i: c[i][1] for i in [0, 2, 3, 4, 5, 6, 7, 8, 9]}
    cost.update({-1: 0, 1: 0})

    for i in a:
        ans += cost[i]

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
import sys
input = sys.stdin.readline
H, W = list(map(int, input().split()))
c = [list(map(int, input().split())) for _ in range(10)]
a = [list(map(int, input().split())) for _ in range(H)]
res = 0
for k in range(10):
  for i in range(10):
    for j in range(10):
      c[i][j] = min(c[i][k] + c[k][j], c[i][j])
for y in range(H):
  for x in range(W):
    if (a[y][x] != -1) or (a[y][x] == 1):
      res += c[a[y][x]][1]
print(res)

# Optimized solution
import sys
input = sys.stdin.readline
H, W = list(map(int, input().split()))
d = [list(map(int, input().split())) for _ in range(10)]
for k in range(10):
  for i in range(10):
    for j in range(10):
      d[i][j] = min(d[i][j], d[i][k] + d[k][j])
res = 0
for _ in range(H):
  a = list(map(int, input().split()))
  for x in a:
    if x != -1: res += d[x][1]
print(res)