#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
#define MAXH 31
#define MAXW MAXH
#define MAXC 4
#define INF (1<<20)
typedef enum {STRA=0,RIGHT=1,BACK=2,LEFT=3,STOP=4} TYPE;
int W,H;
int F[MAXH][MAXW];
int cost[MAXC+1]; // cost[STOP]=0;
int D[MAXH][MAXW][4];
bool visited[MAXH][MAXW][4];

class State {
public:
  int i,j,d,c; // i,j,direction,cost
  State (int i, int j, int d, int c) : i(i),j(j),d(d),c(c) {}
  bool operator < (const State &s) const {
    return c > s.c;
  }
};

bool ok(int i, int j) {
  return 1 <= i && i <= H && 1 <= j && j <= W;
}

int solve() {
  priority_queue<State> PQ;
  for(int i=1;i<=H;i++) {
    for(int j=1;j<=W;j++) {
      for(int k=0;k<MAXC;k++) {
	D[i][j][k]=INF;
      }
    }
  }
  memset(visited,0,sizeof(visited));
  PQ.push(State(1,1,RIGHT,0));
  D[1][1][RIGHT]=0;
  // x=>l, y=>k
  int di[][4]={ {-1, 0, 1, 0},
		{ 0, 1, 0,-1},
		{ 1, 0,-1, 0},
		{ 0,-1, 0, 1}};
  int dj[][4]={ { 0, 1, 0,-1},
		{ 1, 0,-1, 0},
		{ 0,-1, 0, 1},
		{-1, 0, 1, 0}};
  while(!PQ.empty()) {
    State u = PQ.top(); PQ.pop();
    int i = u.i, j=u.j, d=u.d, c=u.c;
    if(i==H&&j==W) {
      return D[i][j][d];
    }
    visited[i][j][d]=true;
    
    for(int k=0;k<4;k++) {
      int ui = i + di[d][k];
      int uj = j + dj[d][k];
      if(!ok(ui,uj)) continue;
      if(k == F[i][j]) { // no cost
	int _cost = D[i][j][d] + 0;
	if(_cost < D[ui][uj][(d+k)%4]) {
	  D[ui][uj][(d+k)%4] = _cost;
	  PQ.push(State(ui,uj,(d+k)%4,_cost));
	}
      } else { // have to pay cost
	int _cost = D[i][j][d] + cost[k];
	if(_cost < D[ui][uj][(d+k)%4]) {
	  D[ui][uj][(d+k)%4] = _cost;
	  PQ.push(State(ui,uj,(d+k)%4,_cost));
	}
      }
    } // for4
    
  }
}

int main() {
  while(cin>>W>>H,W) {
    for(int i=1;i<=H;i++) {
      for(int j=1;j<=W;j++) {
	cin>>F[i][j];
      }
    }
    for(int i=0;i<MAXC;i++) 
      cin>>cost[i];
    cout << solve() << endl;
  }
}