import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

bool solve() {
    auto s = readln.split.map!(to!int);
    auto W = s[0];
    auto H = s[1];
    if (W == 0) return false;

    auto A = H.iota.map!(_ => readln.split.map!(to!int).array).array;
    auto C = readln.split.map!(to!int).array;

    const int[] dr = [-1, 0, 1, 0]; // U, R, D, L
    const int[] dc = [0, 1, 0, -1];


    auto D = new int[][][](H, W, 4);
    foreach (i; 0..H) foreach (j; 0..W) D[i][j][] = 1 << 29;

    auto pq = new BinaryHeap!(Array!(Tuple!(int, int, int, int)), "a[3] > b[3]");
    pq.insert(tuple(0, 0, 1, 0));

    while (!pq.empty) {
        auto r = pq.front[0];
        auto c = pq.front[1];
        auto dir = pq.front[2];
        auto dist = pq.front[3];
        pq.removeFront;
        if (D[r][c][dir] <= dist) continue;
        D[r][c][dir] = dist;

        foreach (i; 0..4) {
            auto nr = r + dr[(dir+i)%4];
            auto nc = c + dc[(dir+i)%4];
            auto ndir = (dir+i)%4;
            auto ndist = i == A[r][c] ? dist : dist + C[i];
            if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            if (D[nr][nc][ndir] <= ndist) continue;
            pq.insert(tuple(nr, nc, ndir, ndist));
        }
    }

    D[H-1][W-1].reduce!min.writeln;

    return true;
}

void main() {
    while (solve()) {}
}

