#include<iostream>
#include<queue>
using namespace std;
typedef pair<int,int> p;
typedef pair< p , p > P;
#define d first.first
#define x first.second
#define y second.first
#define dir second.second
#define INF 1000000000
P Make(int a,int b,int c,int e){
  return P( p( a,b) , p( c, e) );   
}

int w,h;
int mp[30][30],dmp[4][30][30],f[4][30][30];
int let[4];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int dijkstra(){

  priority_queue <P,vector <P>,greater<P> >Q;
  Q.push(Make(0,0,0,0));
  dmp[0][0][0]=0;
  while(!Q.empty()){
    P t=Q.top(); Q.pop();
    if(t.x==w-1&&t.y==h-1)return t.d;
    f[t.dir][t.y][t.x]=1;
    for(int i=0;i<4;i++){
      int ndir=(t.dir+i)%4;
      int nx=t.x+dx[ndir];
      int ny=t.y+dy[ndir];
      int nd=t.d;
      if(mp[t.y][t.x]!=i)nd+=let[i];
      if(nx<0||w<=nx||ny<0||h<=ny||f[ndir][ny][nx]==1||dmp[ndir][ny][nx]<=nd)continue;
      dmp[ndir][ny][nx]=nd;
      Q.push(Make(nd,nx,ny,ndir));
    }
  }
  return 0;
}

int main(){
  while(1){
    cin>>w>>h;
    if(w==0)break;  
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++){
	cin>>mp[i][j];
	for(int k=0;k<4;k++)dmp[k][i][j]=INF,f[k][i][j]=0;;
      }
    for(int i=0;i<4;i++)cin>>let[i];
    cout<<dijkstra()<<endl; 
  }
  return 0;
}