#include <stdbool.h>
#include <stdio.h>
int c[100];

int length(int i, int j) {
    return c[10 * i + j];
}

typedef struct Node {
    int number;
    bool done;
    int cost;
} Node;

int main() {
    int h, w;
    scanf("%d %d", &h, &w);
    for (int i = 0; i < 100; i++) scanf("%d", c + i);

    int costs[10] = {0};

    for (int j = 0; j < 10; j++) {
        if (j == 1) continue;
        Node nodes[10];
        for (int i = 0; i < 10; i++) {
            Node node = {.number = i, .done = false, .cost = -1};
            nodes[i] = node;
        }

        nodes[j].cost = 0;

        while (true) {
            Node doneNode = {.number = -1, .done = false, .cost = -1};
            for (int i = 0; i < 10; i++) {
                Node node = nodes[i];
                if (node.done || node.cost < 0) {
                    continue;
                }
                if (doneNode.number == -1 || node.cost < doneNode.cost) {
                    doneNode = node;
                }
            }
            if (doneNode.number == -1) {
                break;
            }
            nodes[doneNode.number].done = true;
            for (int i = 0; i < 10; i++) {
                if (doneNode.number == i) continue;
                int cost = doneNode.cost + length(doneNode.number, i);
                if (nodes[i].cost < 0 || cost < nodes[i].cost) {
                    nodes[i].cost = cost;
                }
            }
        }

        costs[j] = nodes[1].cost;
    }

    int ans = 0;
    for (int i = 0; i < h * w; i++) {
        int a;
        scanf("%d", &a);
        if (a != -1) ans += costs[a];
    }
    printf("%d\n", ans);
}

; ModuleID = '/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_676/source.m'
source_filename = "/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_676/source.m"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

%struct.Node = type { i32, i8, i32 }

@c = dso_local global [100 x i32] zeroinitializer, align 16
@.str = private unnamed_addr constant [6 x i8] c"%d %d\00", align 1
@.str.1 = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@.str.2 = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1

; Function Attrs: minsize mustprogress nofree norecurse nosync nounwind optsize willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local i32 @length(i32 noundef %i, i32 noundef %j) local_unnamed_addr #0 {
entry:
  %mul = mul nsw i32 %i, 10
  %add = add nsw i32 %mul, %j
  %idxprom = sext i32 %add to i64
  %arrayidx = getelementptr inbounds [100 x i32], ptr @c, i64 0, i64 %idxprom
  %0 = load i32, ptr %arrayidx, align 4, !tbaa !5
  ret i32 %0
}

; Function Attrs: minsize nofree nounwind optsize uwtable
define dso_local i32 @main() local_unnamed_addr #1 {
entry:
  %h = alloca i32, align 4
  %w = alloca i32, align 4
  %costs = alloca [10 x i32], align 16
  %nodes = alloca [10 x %struct.Node], align 16
  %a = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %h) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %w) #5
  %call = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %h, ptr noundef nonnull %w) #6
  br label %for.cond

for.cond:                                         ; preds = %for.body, %entry
  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
  %exitcond.not = icmp eq i64 %indvars.iv, 100
  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body

for.cond.cleanup:                                 ; preds = %for.cond
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %costs) #5
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) %costs, i8 0, i64 40, i1 false)
  %cost86 = getelementptr inbounds [10 x %struct.Node], ptr %nodes, i64 0, i64 1, i32 2
  br label %for.cond2

for.body:                                         ; preds = %for.cond
  %add.ptr = getelementptr inbounds i32, ptr @c, i64 %indvars.iv
  %call1 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %add.ptr) #6
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  br label %for.cond

for.cond2:                                        ; preds = %for.inc89, %for.cond.cleanup
  %indvars.iv160 = phi i64 [ %indvars.iv.next161, %for.inc89 ], [ 0, %for.cond.cleanup ]
  switch i64 %indvars.iv160, label %if.end [
    i64 10, label %for.cond94
    i64 1, label %for.inc89
  ]

if.end:                                           ; preds = %for.cond2
  call void @llvm.lifetime.start.p0(i64 120, ptr nonnull %nodes) #5
  br label %for.cond8

for.cond8:                                        ; preds = %for.body11, %if.end
  %indvars.iv147 = phi i64 [ %indvars.iv.next148, %for.body11 ], [ 0, %if.end ]
  %exitcond150.not = icmp eq i64 %indvars.iv147, 10
  br i1 %exitcond150.not, label %for.cond.cleanup10, label %for.body11

for.cond.cleanup10:                               ; preds = %for.cond8
  %cost17 = getelementptr inbounds [10 x %struct.Node], ptr %nodes, i64 0, i64 %indvars.iv160, i32 2
  store i32 0, ptr %cost17, align 4, !tbaa !9
  br label %for.cond19

for.body11:                                       ; preds = %for.cond8
  %arrayidx = getelementptr inbounds [10 x %struct.Node], ptr %nodes, i64 0, i64 %indvars.iv147
  %0 = trunc i64 %indvars.iv147 to i32
  store i32 %0, ptr %arrayidx, align 4, !tbaa.struct !12
  %node.sroa.4.0.arrayidx.sroa_idx = getelementptr inbounds i8, ptr %arrayidx, i64 4
  store i8 0, ptr %node.sroa.4.0.arrayidx.sroa_idx, align 4, !tbaa.struct !14
  %node.sroa.5131.0.arrayidx.sroa_idx = getelementptr inbounds i8, ptr %arrayidx, i64 8
  store i32 -1, ptr %node.sroa.5131.0.arrayidx.sroa_idx, align 4, !tbaa.struct !15
  %indvars.iv.next148 = add nuw nsw i64 %indvars.iv147, 1
  br label %for.cond8

for.cond19:                                       ; preds = %for.cond19.backedge, %for.cond.cleanup10
  %indvars.iv151 = phi i64 [ 0, %for.cond.cleanup10 ], [ %indvars.iv151.be, %for.cond19.backedge ]
  %doneNode.sroa.11.0 = phi i32 [ -1, %for.cond.cleanup10 ], [ %doneNode.sroa.11.0.be, %for.cond19.backedge ]
  %doneNode.sroa.0.0 = phi i32 [ -1, %for.cond.cleanup10 ], [ %doneNode.sroa.0.0.be, %for.cond19.backedge ]
  %exitcond154.not = icmp eq i64 %indvars.iv151, 10
  br i1 %exitcond154.not, label %for.cond.cleanup21, label %for.body22

for.cond.cleanup21:                               ; preds = %for.cond19
  %cmp44 = icmp eq i32 %doneNode.sroa.0.0, -1
  br i1 %cmp44, label %while.end, label %if.end46

for.body22:                                       ; preds = %for.cond19
  %arrayidx25 = getelementptr inbounds [10 x %struct.Node], ptr %nodes, i64 0, i64 %indvars.iv151
  %node23.sroa.4.0.arrayidx25.sroa_idx = getelementptr inbounds i8, ptr %arrayidx25, i64 4
  %node23.sroa.4.0.copyload = load i8, ptr %node23.sroa.4.0.arrayidx25.sroa_idx, align 4, !tbaa.struct !14
  %node23.sroa.5121.0.arrayidx25.sroa_idx = getelementptr inbounds i8, ptr %arrayidx25, i64 8
  %node23.sroa.5121.0.copyload = load i32, ptr %node23.sroa.5121.0.arrayidx25.sroa_idx, align 4, !tbaa.struct !15
  %1 = and i8 %node23.sroa.4.0.copyload, 1
  %tobool = icmp ne i8 %1, 0
  %cmp28 = icmp slt i32 %node23.sroa.5121.0.copyload, 0
  %or.cond = select i1 %tobool, i1 true, i1 %cmp28
  br i1 %or.cond, label %cleanup, label %if.end30

if.end30:                                         ; preds = %for.body22
  %node23.sroa.0.0.copyload = load i32, ptr %arrayidx25, align 4, !tbaa.struct !12
  %cmp32 = icmp eq i32 %doneNode.sroa.0.0, -1
  %cmp36 = icmp slt i32 %node23.sroa.5121.0.copyload, %doneNode.sroa.11.0
  %or.cond142 = select i1 %cmp32, i1 true, i1 %cmp36
  %doneNode.sroa.11.1 = select i1 %or.cond142, i32 %node23.sroa.5121.0.copyload, i32 %doneNode.sroa.11.0
  %doneNode.sroa.0.1 = select i1 %or.cond142, i32 %node23.sroa.0.0.copyload, i32 %doneNode.sroa.0.0
  br label %cleanup

cleanup:                                          ; preds = %for.body22, %if.end30
  %doneNode.sroa.11.2 = phi i32 [ %doneNode.sroa.11.1, %if.end30 ], [ %doneNode.sroa.11.0, %for.body22 ]
  %doneNode.sroa.0.2 = phi i32 [ %doneNode.sroa.0.1, %if.end30 ], [ %doneNode.sroa.0.0, %for.body22 ]
  %indvars.iv.next152 = add nuw nsw i64 %indvars.iv151, 1
  br label %for.cond19.backedge

for.cond19.backedge:                              ; preds = %for.cond52, %cleanup
  %indvars.iv151.be = phi i64 [ %indvars.iv.next152, %cleanup ], [ 0, %for.cond52 ]
  %doneNode.sroa.11.0.be = phi i32 [ %doneNode.sroa.11.2, %cleanup ], [ -1, %for.cond52 ]
  %doneNode.sroa.0.0.be = phi i32 [ %doneNode.sroa.0.2, %cleanup ], [ -1, %for.cond52 ]
  br label %for.cond19

if.end46:                                         ; preds = %for.cond.cleanup21
  %idxprom48 = sext i32 %doneNode.sroa.0.0 to i64
  %done50 = getelementptr inbounds [10 x %struct.Node], ptr %nodes, i64 0, i64 %idxprom48, i32 1
  store i8 1, ptr %done50, align 4, !tbaa !16
  %mul.i = mul nsw i32 %doneNode.sroa.0.0, 10
  %2 = zext i32 %doneNode.sroa.0.0 to i64
  %3 = sext i32 %mul.i to i64
  br label %for.cond52

for.cond52:                                       ; preds = %for.inc78, %if.end46
  %indvars.iv155 = phi i64 [ %indvars.iv.next156, %for.inc78 ], [ 0, %if.end46 ]
  %exitcond159.not = icmp eq i64 %indvars.iv155, 10
  br i1 %exitcond159.not, label %for.cond19.backedge, label %for.body55

for.body55:                                       ; preds = %for.cond52
  %cmp57 = icmp eq i64 %indvars.iv155, %2
  br i1 %cmp57, label %for.inc78, label %if.end59

if.end59:                                         ; preds = %for.body55
  %4 = add nsw i64 %indvars.iv155, %3
  %arrayidx.i = getelementptr inbounds [100 x i32], ptr @c, i64 0, i64 %4
  %5 = load i32, ptr %arrayidx.i, align 4, !tbaa !5
  %add = add nsw i32 %5, %doneNode.sroa.11.0
  %cost66 = getelementptr inbounds [10 x %struct.Node], ptr %nodes, i64 0, i64 %indvars.iv155, i32 2
  %6 = load i32, ptr %cost66, align 4, !tbaa !9
  %cmp67 = icmp slt i32 %6, 0
  %cmp72 = icmp slt i32 %add, %6
  %or.cond143 = select i1 %cmp67, i1 true, i1 %cmp72
  %spec.store.select = select i1 %or.cond143, i32 %add, i32 %6
  store i32 %spec.store.select, ptr %cost66, align 4
  br label %for.inc78

for.inc78:                                        ; preds = %if.end59, %for.body55
  %indvars.iv.next156 = add nuw nsw i64 %indvars.iv155, 1
  br label %for.cond52

while.end:                                        ; preds = %for.cond.cleanup21
  %7 = load i32, ptr %cost86, align 4, !tbaa !9
  %arrayidx88 = getelementptr inbounds [10 x i32], ptr %costs, i64 0, i64 %indvars.iv160
  store i32 %7, ptr %arrayidx88, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 120, ptr nonnull %nodes) #5
  br label %for.inc89

for.inc89:                                        ; preds = %for.cond2, %while.end
  %indvars.iv.next161 = add nuw nsw i64 %indvars.iv160, 1
  br label %for.cond2

for.cond94:                                       ; preds = %for.cond2, %if.end104
  %ans.0 = phi i32 [ %ans.1, %if.end104 ], [ 0, %for.cond2 ]
  %i93.0 = phi i32 [ %inc106, %if.end104 ], [ 0, %for.cond2 ]
  %8 = load i32, ptr %h, align 4, !tbaa !5
  %9 = load i32, ptr %w, align 4, !tbaa !5
  %mul = mul nsw i32 %9, %8
  %cmp95 = icmp slt i32 %i93.0, %mul
  br i1 %cmp95, label %for.body97, label %for.cond.cleanup96

for.cond.cleanup96:                               ; preds = %for.cond94
  %call109 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2, i32 noundef %ans.0) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %costs) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %w) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %h) #5
  ret i32 0

for.body97:                                       ; preds = %for.cond94
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %a) #5
  %call98 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.1, ptr noundef nonnull %a) #6
  %10 = load i32, ptr %a, align 4, !tbaa !5
  %cmp99.not = icmp eq i32 %10, -1
  br i1 %cmp99.not, label %if.end104, label %if.then100

if.then100:                                       ; preds = %for.body97
  %idxprom101 = sext i32 %10 to i64
  %arrayidx102 = getelementptr inbounds [10 x i32], ptr %costs, i64 0, i64 %idxprom101
  %11 = load i32, ptr %arrayidx102, align 4, !tbaa !5
  %add103 = add nsw i32 %11, %ans.0
  br label %if.end104

if.end104:                                        ; preds = %if.then100, %for.body97
  %ans.1 = phi i32 [ %add103, %if.then100 ], [ %ans.0, %for.body97 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %a) #5
  %inc106 = add nuw nsw i32 %i93.0, 1
  br label %for.cond94
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: minsize nofree nounwind optsize
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

; Function Attrs: minsize nofree nounwind optsize
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

attributes #0 = { minsize mustprogress nofree norecurse nosync nounwind optsize willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { minsize nofree nounwind optsize uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { minsize nofree nounwind optsize "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { minsize optsize }

!llvm.linker.options = !{}
!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"Ubuntu clang version 18.0.0 (++20231007042255+8abb2ace888b-1~exp1~20231007042414.1230)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"Node", !6, i64 0, !11, i64 4, !6, i64 8}
!11 = !{!"_Bool", !7, i64 0}
!12 = !{i64 0, i64 4, !5, i64 4, i64 1, !13, i64 8, i64 4, !5}
!13 = !{!11, !11, i64 0}
!14 = !{i64 0, i64 1, !13, i64 4, i64 4, !5}
!15 = !{i64 0, i64 4, !5}
!16 = !{!10, !11, i64 4}
