#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX = 31;
const int MAX_D = 4;
const int INF = 1<<27;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int cost[MAX][MAX][MAX_D],g[MAX][MAX],order[MAX_D];
int n,m;

struct State{
  int x,y,d,cost;
  State(){}
  State(int _x,int _y,int _d,int _cost) : x(_x), y(_y) ,d(_d), cost(_cost){}

  bool operator > (const State &s) const {
    return cost > s.cost;
  }
};

int ans(){
  priority_queue<State,vector<State>, greater<State> >Q; 
  fill(cost[0][0],cost[MAX][0],INF);
  Q.push(State(0,0,0,0));
  cost[0][0][0] = 0;

  while(!Q.empty()){
    const State t = Q.top(); Q.pop();
    if(t.x == m-1 && t.y == n-1) return t.cost;
    for(int i = 0 ; i < 4 ; i++){
      State u = t;
      u.cost += (g[t.y][t.x] == i ? 0 : order[i]);
      u.d = (t.d + i) % 4;
      u.x += dx[u.d],u.y += dy[u.d];
      if(0 <= u.y && u.y < n && 0 <= u.x && u.x < m &&
	 cost[u.y][u.x][u.d] > u.cost){
	cost[u.y][u.x][u.d] = u.cost;
	Q.push(u);
      }
    }
  }
  return -1;
}

int main(){
  while(cin >> m >> n ,n+m){
    for(int i = 0 ; i < n ; i++)
      for(int j = 0 ; j < m ; j++)
	cin >> g[i][j];

    for(int i = 0 ; i < 4 ; i++) cin >> order[i];

    cout << ans() << endl;
  }

  return 0;
}