// Aizu 1156: Twirling Robot
// 2017.9.18 bal4u@uu

#include <stdio.h>
#include <string.h>

#define MAX 10000
typedef struct { int t, a, b, c; } QUE;
QUE que[MAX]; int qsize;

#define PARENT(i) ((i)>>1)
#define LEFT(i)   ((i)<<1)
#define RIGHT(i)  (((i)<<1)+1)

void min_heapify(int i)
{
  int l, r;
  int min;

  l = LEFT(i), r = RIGHT(i);
  if (l < qsize && que[l].t < que[i].t) min = l; else min = i;
  if (r < qsize && que[r].t < que[min].t) min = r;
  if (min != i) {
    QUE t = que[i]; que[i] = que[min]; que[min] = t;
    min_heapify(min);
  }
}

int deq(int *a, int *b, int *c)
{
  if (qsize == 0) return 0;
  *a = que[0].a, *b = que[0].b, *c = que[0].c;
  que[0] = que[--qsize];
  min_heapify(0);
  return 1;
}

void enq(int t, int a, int b, int c)
{
  int i, ii;

  i = qsize++;
  que[i].t = t, que[i].a = a, que[i].b = b, que[i].c = c;
  while (i > 0 && que[ii = PARENT(i)].t > que[i].t) {
    QUE tt = que[i]; que[i] = que[ii]; que[ii] = tt;
    i = ii;
  }
}

char map[32][32]; int h, w;
int node[32][32][4];
int cost[4];
int mv[4][2] = {{-1,0,},{0,1},{1,0},{0,-1}};

int search()
{
	int i, r, c, d, r2, c2, d2;
	int t;

	qsize = 0;
	memset(node, -1, sizeof(node)); node[0][0][1] = 0;
	enq(0, 0, 0, 1);
	while(deq(&r, &c, &d)) {
		if (r == h-1 && c == w-1) break;
		for (i = 0; i < 4; i++) {
			d2 = (d+i) % 4, r2 = r + mv[d2][0], c2 = c + mv[d2][1];
			if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w) continue;
			t = node[r][c][d] + ((map[r][c] == i) ? 0 : cost[i]);
			if (node[r2][c2][d2] < 0 || t < node[r2][c2][d2])
				node[r2][c2][d2] = t, enq(t, r2, c2, d2);
		}
	}
	return node[h-1][w-1][d]; 
}

int main()
{
	int i, r, c, a;

	while (scanf("%d%d", &w, &h) && w) {
		for (r = 0; r < h; r++) for (c = 0; c < w; c++) scanf("%d", &a), map[r][c] = a;
		for (i = 0; i < 4; i++) scanf("%d", cost+i);
		printf("%d\n", search());
	}
	return 0;
}