#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
#define rep(i, n) for(int i=0; i<n; i++)

static const int INF = 1E9;
static const int dx[] = {0, 1, 0, -1};
static const int dy[] = {1, 0, -1, 0};
int w, h;
int s[55][55];
int c[5];
bool used[55][55][5];

inline bool check(int hx, int hy){ return 0 <= hx && hx < h && 0 <= hy && hy < w; }

int main()
{
  while(cin >> w >> h, w){
    rep(i, h) rep(j, w) rep(k, 5) used[i][j][k] = false;
    rep(i, h) rep(j, w) cin >> s[i][j];
    rep(i, 4) cin >> c[i];

    priority_queue<vi, vector<vi>, greater<vi> > que;
    vi tmp(4);
    tmp[0] = 0, tmp[1] = 0, tmp[2] = 0, tmp[3] = 0;
    que.push(tmp);
    while(!que.empty()){
      vi here = que.top();  que.pop();
      int cost = here[0];
      int hd = here[1];
      int hx = here[2];
      int hy = here[3];
      if(hx == h - 1 && hy == w - 1){
        cout << cost << endl;
        break;
      }
      if(used[hx][hy][hd]) continue;
      used[hx][hy][hd] = true;
      rep(k, 4){
        int nd = (hd + k) % 4;
        int nx = hx + dx[nd];
        int ny = hy + dy[nd];
        if(!check(nx, ny)) continue;
        tmp[0] = cost + (s[hx][hy] == k ? 0 : c[k]);
        tmp[1] = nd, tmp[2] = nx, tmp[3] = ny;
        que.push(tmp);
      }
    }
  }

  return 0;
}