#include <iostream>
#include <queue>
#define INF 100000;
#define P pair < int , pair < int , pair < int , int > > > 
#define dis first
#define dir second.first
#define x second.second.first
#define y second.second.second
using namespace std;
int w,h;
int mp[31][31];
int c[4];
//> v < ^
//0 1 2 3

int mk(int dr,int i) {
  if(dr==0) return (0+i)%4;
  else if(dr==3) return (1+i)%4;
  else if(dr==2) return (2+i)%4; 
  else if(dr==1) return (3+i)%4;

  return -1;
}

int dijkstra() {
  priority_queue <P> Q;
  int dx[] = {1,0,-1,0};
  int dy[] = {0,1,0,-1};
  bool visited[4][31][31] = {};
  int d[4][31][31];
  for(int i=0;i<4;i++)
    for(int j=0;j<h;j++)
      for(int k=0;k<w;k++) 
	d[i][j][k] = -INF;

  P t;
  t.dis = 0,   t.dir = 0,   t.x = 0,   t.y = 0;
  d[0][0][0] = 0;
  Q.push(t);

  while(!Q.empty()){
    t = Q.top() , Q.pop();
    if(visited[t.dir][t.y][t.x] == 1) continue;
    visited[t.dir][t.y][t.x] = 1;

    // if(mp[t.y][t.x] == 4) continue;

    for(int i=0;i<4;i++) {
      int nx = t.x+dx[i],ny = t.y+dy[i];
      if(nx<0 ||ny<0 ||w<=nx ||h<=ny) continue;
      int ii = mk(t.dir,i);
      int cost = c[ii];
      if(mp[t.y][t.x] == ii) cost = 0;

      if(d[i][ny][nx] < d[t.dir][t.y][t.x]-cost) {
	d[i][ny][nx] = d[t.dir][t.y][t.x]-cost;
	P tt;
	tt.x = nx,  tt.y = ny,  tt.dir = i,  tt.dis = d[i][ny][nx];
	Q.push(tt);
      }
    }
  }

  int ans = -INF;
  for(int i=0;i<4;i++) ans = max(ans,d[i][h-1][w-1]);
  return ans;
}

int main() {

  while(1) {
  cin >> w >> h;
  if(w == 0 && h == 0) break;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin >> mp[i][j];
    }
  }
   
  for(int i=0;i<4;i++) cin >> c[i];

  cout << -dijkstra() <<endl;
  }
  
  return 0;
}