#include <iostream>
#include <vector>

using namespace std;

struct State{
  int x,y;
  int cnt;
  int rotate;
};

int main(void){
  int w,h;
  while(cin>>w>>h,w||h){
    int s[31][31],min[31][31][4];
    for(int i=0;i<h;++i){
      for(int j=0;j<w;++j){
        cin>>s[i][j];
      }
    }
    int c[4];
    for(int i=0;i<4;++i){
      cin>>c[i];
    }
    for(int i=0;i<h;++i){
      for(int j=0;j<w;++j){
        for(int k=0;k<4;++k){
          min[i][j][k]=(w+h)*10;
        }
      }
    }
    State initial;
    initial.x=0;initial.y=0;initial.cnt=0;initial.rotate=0;
    vector<State> qu;
    qu.push_back(initial);
    while(!qu.empty()){
      const int DX[]={1,0,-1,0},DY[]={0,1,0,-1};
      State b=qu.back();qu.pop_back();
      if(b.x<0 || w<=b.x || b.y<0 || h<=b.y) continue;
      if(min[b.y][b.x][b.rotate]>b.cnt)min[b.y][b.x][b.rotate]=b.cnt;
      else continue;
      State a;
      for(int i=0;i<4;++i){
        if(s[b.y][b.x]==i) a.cnt=b.cnt;
        else a.cnt=b.cnt+c[i];
        a.rotate=(b.rotate+i)%4;
        a.x=b.x+DX[a.rotate];a.y=b.y+DY[a.rotate];
        qu.push_back(a);
      }
    }
#if 0
     for(int i=0;i<h;++i){
      for(int j=0;j<w;++j){
        for(int k=0;k<4;++k){
          cerr<<min[i][j][k]<<" ";
        }
        cerr<<",";
      }
      cerr<<endl;
    }
#endif
    int ans=(w+h)*10;
    for(int i=0;i<4;++i){
    	if(ans>min[h-1][w-1][i])ans=min[h-1][w-1][i];
    }
    cout<<ans<<endl;
  }
  return 0;
}