// ??????: http://d.hatena.ne.jp/simezi_tan/20140920/1411169324

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);++i)
#define rep2(i,a,b) for (int i=(a);i<(b);++i)
#define rrep(i,n) for (int i=(n)-1;i>=0;--i)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;--i)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(_v) cout<<(#_v)<<":";for(auto(_x):(_v)){cout<<" "<<(_x);}cout<<endl;
#define printVS(vs) cout<<(#vs)<<":"<<endl;for(auto(s):(vs)){cout<<(s)<< endl;}
#define printVV(vv) cout<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cout<<" "<<(x);}cout<<endl;}
#define printP(p) cout<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cout<<(#vp)<<":"<<endl;for(auto(p):(vp)){cout<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cout << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cout << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1e9;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

// 14:30-

// N, E, S, W
const int dx[4] = {-1, 0,  1,  0};
const int dy[4] = { 0, 1,  0, -1};

int w, h;
int a[30][30];
int c[4];
int d[30][30][4]; // d[i][j][k] = ??????(i, j)?????????k??§????????????????????§???????°??????????

bool inside(int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w;
}

int dijkstra() {
    using State = tuple<int, int, int, int>; // cost, i, j, k
    priority_queue<State, vector<State>, greater<State>> pq;
    rep(i, h) rep(j, w) rep(k, 4) d[i][j][k] = inf;
    d[0][0][1] = 0;
    pq.push(State(0, 0, 0, 1));

    while (!pq.empty()) {
        int cost, i, j, k;
        tie(cost, i, j, k) = pq.top(); pq.pop();

        bool debug = false;
        if (i == 0 && j == 7 && k == 1) {
            // debug = true;
        }
        if (debug) cout << "i = " << i << ", j = " << j << ", k = " << k << ", cost = " << cost << endl;
        if (d[i][j][k] < cost) continue;
        rep(nk, 4) {
            int ni = i + dx[nk], nj = j + dy[nk];
            if (debug) cout << "nk = " << nk << ", ni = " << ni << ", nj = " << nj << endl;
            if (!inside(ni, nj)) continue;
            int ncost = c[(nk - k + 4) % 4];
            if (a[i][j] != 4 && nk == (k + a[i][j]) % 4) {
                // cerr << "hey!" << endl;
                ncost = 0;
            }
            if (d[ni][nj][nk] > cost + ncost) {
                d[ni][nj][nk] = cost + ncost;
                pq.push(State(cost + ncost, ni, nj, nk));
            }
        }
    }

    // rep(k, 4) {
    //     cout << "k = " << k << ":" << endl;
    //     rep(i, h) {
    //         rep(j, w) {
    //             cout << d[i][j][k] << " ";
    //         }
    //         cout << endl;
    //     }
    //     cout << endl;
    // }

    int ret = inf;
    rep(k, 4) chmin(ret, d[h-1][w-1][k]);
    return ret;
}

main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int testcase = 0;
    while (cin >> w >> h, w) {
        // cerr << "--- testcase " << ++testcase << " ---" << endl;
        rep(i, h) rep(j, w) cin >> a[i][j];
        rep(i, 4) cin >> c[i];
        // if (testcase != 1) continue;
        cout << dijkstra() << endl;
    }
}

Measured memory_bytes: 3364