#include<iostream>
#include<queue>
#include<string.h>
#define inf 1000000001
using namespace std;

int X[4]={0,1,0,-1};
int Y[4]={-1,0,1,0};
int h,w,c[4];
int map[40][40];
int memo[40][40][4];

class State{
public:
  int y,x,muki,cost;
  State(int y,int x,int muki,int cost):y(y),x(x),muki(muki),cost(cost){}
};

bool in(int y,int x)
{
  if(y<0 || x<0 || y>=h || x>=w)return false;
  return true;
}

int main()
{
  queue<State> Q;
  while(1){
    cin>>w>>h;
    if(h+w==0)break;

    for(int i=0;i<40;i++){
      for(int j=0;j<40;j++){
	for(int k=0;k<4;k++)memo[i][j][k]=inf;
      }
    }
    for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>map[i][j];
    for(int k=0;k<4;k++)cin>>c[k];

    Q.push(State(0,0,1,0));
    int ans=inf;
    memo[0][0][1]=0;
    while(!Q.empty()){
      State u=Q.front();
      Q.pop();
      //      cout<<u.y<<" "<<u.x<<" "<<u.muki<<" "<<u.cost<<endl;
      if(u.y==h-1 && u.x==w-1){
	ans=min(ans,u.cost);
	continue;
      }

      for(int k=0;k<4;k++){
	int mk=(u.muki+k)%4;
	int a=u.y+Y[mk],b=u.x+X[mk];
        if(!in(a,b))continue;
	if(k==map[u.y][u.x]){
	  if(memo[a][b][mk]>u.cost){
	    memo[a][b][mk]=u.cost;
	    Q.push(State(a,b,mk,u.cost));
	  }
	}
	else {
          if(memo[a][b][mk]>u.cost+c[k]){
	    memo[a][b][mk]=u.cost+c[k];
	    Q.push(State(a,b,mk,u.cost+c[k]));
	  }
	}
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}