#include <iostream>
#include <vector>
#include <queue>

using namespace std;

#define INF 1000000000

typedef pair<int, int> P;
typedef pair<P, int> Q;

int main() {
	int w, h;
	while (cin >> w >> h) {
		if (w == 0 && h == 0) {
			break;
		}
		vector< vector<int> > s(h, vector<int>(w));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> s[i][j];
			}
		}
		int cc[4];
		for (int i = 0; i < 4; i++) {
			cin >> cc[i];
		}
		vector< vector< vector<long long int> > > dp(h, vector< vector<long long int> >(w, vector<long long int>(4, INF)));
		dp[0][0][0] = 0;
		int dx[] = { 1, 0, -1, 0 };
		int dy[] = { 0, 1, 0, -1 };
		bool hh = true;
		while (hh) {
			hh = false;
			vector< vector< vector<long long int> > > dp2;
			dp2 = dp;
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					for (int k = 0; k < 4; k++) {
						for (int kk = 0; kk < 4; kk++) {
							//cout << i << ' ' << j << ' ' << k << ' ' << kk << endl;
							int mm = (k + kk) % 4;
							int cost = cc[kk];
							if (s[i][j] == kk) {
								cost = 0;
							}
							if (0 <= j+dx[mm] && j+dx[mm] < w && 0 <= i+dy[mm] && i+dy[mm] < h && dp2[i+dy[mm]][j+dx[mm]][mm] > dp[i][j][k]+cost) {
								hh = true;
								dp2[i + dy[mm]][j + dx[mm]][mm] = dp[i][j][k] + cost;
								//cout << i + dy[mm] << ' ' << j + dx[mm] << ' ' << dp2[i + dy[mm]][j + dx[mm]][mm] << endl;

							}
						}
					}
				}
			}
			dp = dp2;
		}
		cout << min(dp[h - 1][w - 1][0], min(dp[h - 1][w - 1][1], min(dp[h - 1][w - 1][2], dp[h - 1][w - 1][3]))) << endl;
	}
	return 0;
}