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

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int dd[]={0,1,2,3};
typedef tuple<int,int,int,int>state;

int H,W;
int S[30][30];
int C[4];

const int INF=1001001001;
int dist[30][30][4];
void solve(){
    fill_n(**dist,30*30*4,INF);
    dist[0][0][0]=0;
    priority_queue<state,vector<state>,greater<state>>que;
    que.push(make_tuple(0,0,0,0));
    while(que.size()){
        int c,y,x,d;
        tie(c,y,x,d)=que.top();
        que.pop();
        if(dist[y][x][d]<c)continue;
        rep(i,4){
            int nd=(d+dd[i])%4;
            int ny=y+dy[nd],nx=x+dx[nd];
            if(ny<0||ny>=H||nx<0||nx>=W)continue;
            int nc=c;
            if(S[y][x]!=i)nc+=C[i];
            if(dist[ny][nx][nd]<=nc)continue;
            dist[ny][nx][nd]=nc;
            que.push(make_tuple(nc,ny,nx,nd));
        }
    }

    int mi=INF;
    rep(i,4)chmin(mi,dist[H-1][W-1][i]);
    cout<<mi<<endl;
}

signed main(){
    while(cin>>W>>H,W||H){
        rep(i,H)rep(j,W)cin>>S[i][j];
        rep(i,4)cin>>C[i];
        solve();
    }
    return 0;
}