#include <queue>
#include <cstdio>
#include <vector>
using namespace std;
int H, W, s[30][30], c[4], d[30][30][4];
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
int main() {
	while (~scanf("%d%d", &W, &H)) {
		if (H == 0 && W == 0) break;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) scanf("%d", &s[i][j]);
		}
		for (int i = 0; i < 4; i++) scanf("%d", &c[i]);
		for (int i = 0; i < 30; i++) {
			for (int j = 0; j < 30; j++) {
				for (int k = 0; k < 4; k++) d[i][j][k] = 999999999;
			}
		}
		d[0][0][0] = 0;
		priority_queue<vector<int> > que; que.push({ 0, 0, 0, 0 }); // (cost, x, y, dir)
		while (!que.empty()) {
			vector<int> v1 = que.top(); que.pop();
			if (v1[1] + 1 == W && v1[2] + 1 == H) continue;
			for (int i = 0; i < 4; i++) {
				int sx = v1[1] + dx[(v1[3] + i) % 4];
				int sy = v1[2] + dy[(v1[3] + i) % 4];
				if (0 <= sx && sx < W && 0 <= sy && sy < H) {
					int cost = -v1[0] + (s[v1[2]][v1[1]] != i ? c[i] : 0);
					if (cost < d[sy][sx][(v1[3] + i) % 4]) {
						d[sy][sx][(v1[3] + i) % 4] = cost;
						que.push({ -cost, sx, sy, (v1[3] + i) % 4 });
					}
				}
			}
		}
		int ret = 999999999;
		for (int i = 0; i < 4; i++) ret = min(ret, d[H - 1][W - 1][i]);
		printf("%d\n", ret);
	}
	return 0;
}