Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]

def dfs(nv,vl,cost):
    if nv==1:
        return cost
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    cost.append(dfs(i,[],0))
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += cost[a[i][j]]
print(ans)

Fixed submission:
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]
memo = {}
def dfs(nv,vl,cost):
    if nv==1:
        return cost
    if nv in memo.keys():
        return cost+memo[nv]
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    memo[i] = dfs(i,[],0)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += memo[a[i][j]]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]

def dfs(nv,vl,cost):
    if nv==1:
        return cost
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    cost.append(dfs(i,[],0))
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += cost[a[i][j]]
print(ans)

Fixed submission:
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]
memo = {}
def dfs(nv,vl,cost):
    if nv==1:
        return cost
    if nv in memo.keys():
        return cost+memo[nv]
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    memo[i] = dfs(i,[],0)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += memo[a[i][j]]
print(ans)

Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]

def dfs(nv,vl,cost):
    if nv==1:
        return cost
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    cost.append(dfs(i,[],0))
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += cost[a[i][j]]
print(ans)

Fixed submission:
h,w = map(int,input().split())
c = [list(map(int,input().split())) for _ in range(10)]
a = [list(map(int,input().split())) for _ in range(h)]
memo = {}
def dfs(nv,vl,cost):
    if nv==1:
        return cost
    if nv in memo.keys():
        return cost+memo[nv]
    vl_ = vl.copy()
    vl_.append(nv)
    ret = []
    for i in range(10):
        if not i in vl_:
            ret.append(dfs(i,vl_,cost+c[nv][i]))
    return min(ret)
cost = []
for i in range(10):
    memo[i] = dfs(i,[],0)
ans = 0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans += memo[a[i][j]]
print(ans)