#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int w, h;
int s[35][35];
int c[4];
int vx[4][4] = {{1, 0, -1, 0}, {0, 1, 0, -1}, {-1, 0, 1, 0}, {0, -1, 0, 1}};
int vy[4][4] = {{0, 1, 0, -1}, {-1, 0, 1, 0}, {0, -1, 0, 1}, {1, 0, -1, 0}};
int vv[4][4] = {{0, 3, 2, 1}, {1, 0, 3, 2}, {2, 1, 0, 3}, {3, 2, 1, 0}};

class robot {
	public :
		int x;
		int y;
		int v; // 向き
		int c;

		robot() {
			x = 0; y = 0; v = 0; c = 0;
		}

		bool operator<(const robot& other) const{
			return c > other.c;
		}

		bool operator>(const robot& other) const{
			return c < other.c;
		}

		bool next(int o) {
			if(o != s[y][x])
				c += ::c[o];
			x += vx[v][o];
			y += vy[v][o];
			v = vv[v][o];

			if(x < 0 || y < 0 || x >= w || y >= h)
				return false;

			return true;
		}
}; 

bool input() {
	cin >> w >> h;
	if(w == 0 && h == 0)
		return 0;
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) {
			cin >> s[i][j];
		}
	}

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

	return 1;
}

int solve(void) {
	priority_queue<robot> q;
	int use[35][35][4];
	memset(use, -1, 35 * 35 * 4 * sizeof(int));

	q.push(robot());
	while(!q.empty()) {
		robot now = q.top();
		q.pop();

		if(use[now.x][now.y][now.v] == -1) {
			use[now.x][now.y][now.v] = now.c;
		}

		else
			continue;

		if(now.x == w - 1 && now.y == h - 1){
			return now.c;
		}

		for(int i = 0; i < 4; i++) {
			robot temp = now;
			if(temp.next(i)){
					q.push(temp);
			}
		}
	}
	return -1;
}

int main(void) {
	while(input()){
		cout << solve() << endl;
	}
	return 0;
}