#include <bits/stdc++.h>
using namespace std;

// type {{{
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
// }}}


// macro {{{
#define REP(i,n) for (int i=0; i<(n); ++i)
#define RREP(i,n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i,a,n) for (int i=(a); i<(n); ++i)
#define RFOR(i,a,n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define all(x) begin(x),end(x)
// }}}


// debug {{{
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    REP (i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "(" << p.first << " " << p.second << ")";
}
// }}}


// chmax, chmin {{{
template<class T>
bool chmax(T& a, const T& b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
bool chmin(T& a, const T& b) {
    if (b < a) { a = b; return true; }
    return false;
}
// }}}


// constants {{{
#define inf(T) (numeric_limits<T>::max() / 2)
const ll MOD = 1e9+7;
const ld EPS = 1e-9;
// }}}



template<typename T>
struct edge {
    int src, to;
    T cost;

    edge() {}
    edge(int to, T cost) : src(-1), to(to), cost(cost) {}
    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

    friend ostream& operator<<(ostream& os, const edge& e) {
        return os << "(" << e.src << "->" << e.to << ":" << e.cost << ")";
    }
};


template<typename T>
vector<T> dijkstra(const vector<vector<edge<T>>> &g, int s) {
    const T INF = numeric_limits<T>::max();
    vector<T> d(g.size(), INF);

    using Pi = pair<T, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    d[s] = 0;
    que.emplace(d[s], s);
    while (!que.empty()) {
        T cost;
        int v;
        tie(cost, v) = que.top();
        que.pop();
        if (d[v] < cost) continue;
        for (auto &e : g[v]) {
            T nxt = cost + e.cost;
            if (d[e.to] > nxt) {
                d[e.to] = nxt;
                que.emplace(nxt, e.to);
            }
        }
    }
    return d;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    const int STRAIGHT = 0,
              RIGHT = 1,
              BACK = 2,
              LEFT = 3,
              HALT = 4;

    const int dy[] = {0, 1, 0, -1},
              dx[] = {1, 0, -1, 0};

    for (;;) {
        int W, H; cin >> W >> H;
        if (W == 0) break;

        vvi S(H, vi(W));
        REP(i, H) REP(j, W) cin >> S[i][j];
        vi C(4);
        REP(i, 4) cin >> C[i];

        vector<vector<edge<int>>> G(H*W*4);

        REP(d, 4) REP(i, H) REP(j, W) {
            for (int op : {STRAIGHT, RIGHT, BACK, LEFT}) {
                int nd = (d + op) % 4;
                int ni = i + dy[nd],
                    nj = j + dx[nd];
                if (ni < 0 or nj < 0 or ni >= H or nj >= W) continue;
                G[i*(W*4) + j*4 + d].emplace_back(
                        ni*(W*4) + nj*4 + nd, C[op]);
            }
            if (S[i][j] != HALT) {
                int nd = (d + S[i][j]) % 4;
                int ni = i + dy[nd],
                    nj = j + dx[nd];
                if (ni < 0 or nj < 0 or ni >= H or nj >= W) continue;
                G[i*(W*4) + j*4 + d].emplace_back(
                        ni*(W*4) + nj*4 + nd, 0);
            }
        }

        auto dist = dijkstra(G, 0);

        int ans = inf(int);

        REP(d, 4) chmin(ans, dist[(H-1)*(W*4) + (W-1)*4 + d]);

        cout << ans << endl;
    }

    return 0;
}



Measured memory_bytes: 3480