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

using namespace std;

const int INF = 100000000;

class S{
public:
  int y, x, d, cost;
  S(){}
  S(int y, int x, int d, int cost) : y(y), x(x), d(d), cost(cost) {}
};

int h, w;
int map[30][30];
int cost[4];
int vis[30][30][4];
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};

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

int solve(){
  S u, v;
  queue<S> q;
  q.push(S(0, 0, 1, 0));
  fill(vis[0][0], vis[30][0], INF);
  while(!q.empty()){
    u = q.front(); q.pop();
    if(map[u.y][u.x] != 4){
      v.d = (u.d+map[u.y][u.x])%4;
      v.y = u.y + dy[v.d];
      v.x = u.x + dx[v.d];
      v.cost = u.cost;
      if(check(v.y, v.x) && vis[v.y][v.x][v.d] > v.cost){
        vis[v.y][v.x][v.d] = v.cost;
        q.push(v);
      }
    }
    for(int i=0;i<4;i++){
      v.d = (u.d+i) % 4;
      v.y = u.y + dy[v.d];
      v.x = u.x + dx[v.d];
      v.cost = u.cost + cost[i];
      if(check(v.y, v.x) && vis[v.y][v.x][v.d] > v.cost){
        vis[v.y][v.x][v.d] = v.cost;
        q.push(v);
      }
    }
  }
  int res = INF;
  for(int i=0;i<4;i++) res = min(res, vis[h-1][w-1][i]);
  return res;
}

main(){
  while(cin >> w >> h && (h|w)){
    for(int i=0;i<h;i++) for(int j=0;j<w;j++) cin >> map[i][j];
    for(int i=0;i<4;i++) cin >> cost[i];
    cout << solve() << endl;
  }
}