#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr<<" "<<(hoge)<<endl;
#define DBG cerr<<"!"<<endl;
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
//0:right,1:left,2:up,3:down
int vx[4]={0,0,-1,1};
int vy[4]={1,-1,0,0};
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin>>n>>m,n){
    ll hoge[m+10][n+10];
    ll c[4];
    for(int i=0;i<m;i++)
      for(int j=0;j<n;j++)
	cin>>hoge[i][j];
    for(int i=0;i<4;i++)
      cin>>c[i];
    ll dist[m+10][n+10][5];
    for(int i=0;i<m+10;i++)
      for(int j=0;j<n+10;j++)
	for(int k=0;k<5;k++)
	  dist[i][j][k]=llINF;
    dist[0][0][0] = 0;
    priority_queue<pair<pair<ll,ll>,pair<ll,ll>>,vector<pair<pair<ll,ll>,pair<ll,ll>>>,greater<>>que;
    que.push(mp(mp(0,0),mp(0,0)));
    while(!que.empty()){
      pair<ll,ll> now = que.top().S;
      ll d = que.top().F.F;
      //0:right,1:left,2:up,3:down
      ll muki = que.top().F.S;
      que.pop();
    
      for(int j=0;j<4;j++){
	if(dist[now.F][now.S][muki] < d)continue;
	ll tox=now.F+vx[j],toy=now.S+vy[j];
	if(0>tox||tox>=m||0>toy||toy>=n)continue;
	ll cost=0;
	if(muki == 0){
	  if(j == 0){
	    if(hoge[now.F][now.S] == 0){
	      cost=0;
	    }else{
	      cost=c[0];
	    }
	  }else if(j == 1){
	    if(hoge[now.F][now.S] == 2){
	      cost=0;
	    }else{
	      cost=c[2];
	    }
	  }else if(j == 2){
	    if(hoge[now.F][now.S] == 3){
	      cost = 0;
	    }else{
	      cost=c[3];
	    }
	  }else if(j == 3){
	    if(hoge[now.F][now.S] == 1){
	      cost=0;
	    }else{
	      cost = c[1];
	    }
	  }
	 
	}else if(muki == 1){
	  if(j == 0){
	    if(hoge[now.F][now.S] == 2){
	      cost=0;
	    }else{
	      cost=c[2];
	    }
	  }else if(j == 1){
	    if(hoge[now.F][now.S] == 0){
	      cost=0;
	    }else{
	      cost=c[0];
	    }
	  }else if(j == 2){
	    if(hoge[now.F][now.S] == 1){
	      cost = 0;
	    }else{
	      cost=c[1];
	    }
	  }else if(j == 3){
	    if(hoge[now.F][now.S] == 3){
	      cost=0;
	    }else{
	      cost = c[3];
	    }
	  }
	 
	}else if(muki == 2){
	  if(j == 0){
	    if(hoge[now.F][now.S] == 1){
	      cost=0;
	    }else{
	      cost=c[1];
	    }
	  }else if(j == 1){
	    if(hoge[now.F][now.S] == 3){
	      cost=0;
	    }else{
	      cost=c[3];
	    }
	  }else if(j == 2){
	    if(hoge[now.F][now.S] == 0){
	      cost = 0;
	    }else{
	      cost=c[0];
	    }
	  }else if(j == 3){
	    if(hoge[now.F][now.S] == 2){
	      cost=0;
	    }else{
	      cost = c[2];
	    }
	  }
	 
	}else if(muki == 3){
	  if(j == 0){
	    if(hoge[now.F][now.S] == 3){
	      cost=0;
	    }else{
	      cost=c[3];
	    }
	  }else if(j == 1){
	    if(hoge[now.F][now.S] == 1){
	      cost=0;
	    }else{
	      cost=c[1];
	    }
	  }else if(j == 2){
	    if(hoge[now.F][now.S] == 2){
	      cost = 0;
	    }else{
	      cost=c[2];
	    }
	  }else if(j == 3){
	    if(hoge[now.F][now.S] == 0){
	      cost=0;
	    }else{
	      cost = c[0];
	    }
	  }
	 
	}
	if(dist[tox][toy][j] > dist[now.F][now.S][muki]+cost){
	  dist[tox][toy][j] = dist[now.F][now.S][muki]+cost;
	  que.push(mp(mp(dist[tox][toy][j],j),mp(tox,toy)));
	}
      }
    }
    /*for(int i=0;i<m;i++){
      for(int j=0;j<n;j++)cout<<dist[i][j]<<" ";
      cout<<endl;
     }*/
    ll ans=llINF;
    for(int i=0;i<4;i++)
      ans=min(ans,dist[m-1][n-1][i]);
    cout<<ans<<endl;
  }
  return 0;
}



Measured memory_bytes: 3312