#include <bits/stdc++.h>

using namespace std;
using VI = vector<int>;
using PII = pair<int,int>;
#define MP make_pair
#define FF first
#define SS second

int dist[30][30][4];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

int main(){
  int W, H;
  while(cin>>W>>H,W){
    vector<VI> xs(H, VI(W));
    for(int y=0;y<H;++y)
      for(int x=0;x<W;++x)
	cin >> xs[y][x];
    VI cs(4);
    for(int i=0;i<4;++i)
      cin >> cs[i];

    fill((int*)dist, (int*)dist+30*30*4, 1e9);
    // dist, dir, y, x
    using P = pair<int,pair<int,PII>>;
    priority_queue<P,vector<P>,greater<P>> pq;
    // y, x, dir
    dist[0][0][0] = 0;
    pq.push(MP(0,MP(0,MP(0,0))));
    while(!pq.empty()){
      P p = pq.top();
      pq.pop();
      int cost = p.FF;
      int dir = p.SS.FF;
      int y = p.SS.SS.FF;
      int x = p.SS.SS.SS;
      if(dist[y][x][dir] < cost) continue;
      for(int i=0;i<4;++i){
	int tdir = (dir + i) % 4;
	int tx = x + dx[tdir], ty = y + dy[tdir];
	int nc = cost + cs[i];
	if(0 <= ty && ty < H && 0 <= tx && tx < W
	   && dist[ty][tx][tdir] > nc){
	  pq.push(MP(nc, MP(tdir, MP(ty,tx))));
	  dist[ty][tx][tdir] = nc;
	}
      }

      if(xs[y][x] == 4) continue;
      if(xs[y][x] == 1) (++dir) %= 4;
      else if(xs[y][x] == 2) (dir+=2)%=4;
      else if(xs[y][x] == 3) (dir+=3)%=4;
      int tx = x + dx[dir], ty = y + dy[dir];
      if(0 <= ty && ty < H && 0 <= tx && tx < W
	 && dist[ty][tx][dir] > cost){
	pq.push(MP(cost, MP(dir, MP(ty,tx))));
	dist[ty][tx][dir] = cost;
      }
    }
    
    int ans = 1e9;
    for(int i=0;i<4;++i)
      ans = min(ans, dist[H-1][W-1][i]);
    cout << ans << endl;
  }

  return 0;
}