#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30

using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<P,P> PP;

int f[55][55];
int c[4];
int h,w,ans = -1;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
bool memo[55][55][4];

bool can(int y,int x) {
    if(0 <= y && y < h && 0 <= x && x < w) return true;
    return false;
}

void func() {
    priority_queue<PP,vector<PP>,greater<PP> > que;
    que.push(PP(P(0,0),P(0,0)));
    
    memset(memo,0,sizeof(memo));

    while(que.size()) {
        PP p = que.top();
        que.pop();

        int cost = p.first.first;
        int d = p.first.second;
        int y = p.second.first;
        int x = p.second.second;

        if(y == h-1 && x == w-1) {
            ans = cost;
            break;
        }

        if(memo[y][x][d]) continue;
        memo[y][x][d] = true;

        rep(i,4)
        {
            int nd = (d+i)%4;
            int ny = y + dy[nd];
            int nx = x + dx[nd];

            if(can(ny,nx)) {
                if(f[y][x] == i) {
                    que.push(PP(P(cost,nd),P(ny,nx)));
                }
                else {
                    que.push(PP(P(cost+c[i],nd),P(ny,nx)));
                }
            }
        }
    }
}
int main() {
    
    while(cin >> w >> h) {
        if(w == 0 && h == 0) break;
        
        memset(f,0,sizeof(f));
        rep(i,h) {
            rep(j,w) {
                cin >> f[i][j];
            }
        }

        rep(i,4) cin >> c[i];

        func();
        cout << ans << endl;
    }

    return 0;
}