PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000005. Bức tường chữ số (Wall)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

2 lộ trình

Bức tường chữ số (Wall)

Wall

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

A H×WH\times W wall has digits from 00 to 99 or empty cells indicated by −1-1. Changing ii to jj costs ci,jc_{i,j} and transformations may be chained. Find the minimum total cost to turn all digits into 11.

Input

H,WH,W; a 10×1010\times10 cost matrix; then the H×WH\times W wall.

Output

Print the minimum total cost.

Subtasks

  • Subtask 1 (40 points): H≤5H\le 5, W≤6W\le 6; 1≤H,W≤2001\le H,W\le 200; 0≤cii≤00\le c_ii\le 0; 1≤cij≤10001\le c_ij\le 1000; −1≤Aij≤9-1\le A_ij\le 9.
  • Subtask 2 (60 points): 1≤H,W≤2001\le H,W\le 200; 0≤cii≤00\le c_ii\le 0; 1≤cij≤10001\le c_ij\le 1000; −1≤Aij≤9-1\le A_ij\le 9.

Examples

Input

1 3
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 2 0 9 9 9 9 9 9 9
9 9 1 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 9
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 9 9 9 9 0 9
9 9 9 9 9 9 9 9 9 0
3 1 -1

Output

3

Explanation

Digit 33 can become 22 for 11 and then 11 for 22, totaling 33. Digit 11 costs nothing and the empty cell is ignored.