#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>

using namespace std;

struct State {
  int x, y, d, cost;
  State(){}
  State(int x_, int y_, int d_, int cost_) : x(x_), y(y_), d(d_), cost(d_) {}
  bool operator>(const State& s) const {
    return cost>s.cost;
  }
};

const int SIZE = 31;
const int INF = 1<<27;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

int H, W;
int G[SIZE][SIZE], C[4];

bool inField(int x, int y) {
  return 0<=x && x<W && 0<=y & y<H;
}

int dijkstra() {
  int cost[4][SIZE][SIZE];
  
  priority_queue<State, vector<State>, greater<State> > pq;
  
  fill(cost[0][0], cost[4][0], INF);
  pq.push(State(0,0,0,0));
  cost[0][0][0] = 0;

  while(!pq.empty()) {
    const State s = pq.top(); pq.pop();
    if(s.x == W-1 && s.y == H-1) return s.cost;
    
    for(int i=0; i<4; i++) {
      State t = s;
      t.d = (s.d + i) % 4;
      t.x += dx[t.d], t.y += dy[t.d];
      t.cost += i == G[s.y][s.x] ? 0 : C[i];
      if(inField(t.x, t.y) && cost[t.d][t.y][t.x] > t.cost) {
	pq.push(t);
	cost[t.d][t.y][t.x] = t.cost;
      }
    }
  }
  
  return -1;
}

int main() {
  
  while( cin >> W >> H, W+H ) {
    
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cin >> G[i][j];
      }
    }
    for(int i=0; i<4; i++) cin >> C[i];
    
    cout << dijkstra() << endl;
  }
  return 0;
}