#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp       make_pair
#define pb       push_back
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int w,h;
int board[40][40];
int cost[5];
int dir;
int cboard[40][40][4];
int main(){
	while(cin >> w >> h){
		dir = 0;
		int ans = 1 << 30;
		for (int i = 0; i < 40; i++){
			for (int j = 0; j < 40; j++){
				board[i][j] = -1;
				for (int k = 0; k < 4; k++){
					cboard[i][j][k] = INF;
				}
			}
		}
		cboard[1][1][0] = 0;
		if (w == 0 || h == 0)break;
		for (int i = 1; i <= h; i++){
			for (int j = 1; j <= w; j++){
				cin >> board[i][j];
			}
		}
		for (int i = 0; i < 4; i++){
			cin >> cost[i];
		}
		int cnt = 0;
		while(cnt <= 50000){
		for (int i = 1; i <= h; i++){//y
			for (int j = 1; j <= w; j++){//x
				for (int k = 0; k < 4; k++){//dir
					if (i == h && j == w){
						ans = min(ans, cboard[i][j][k]);
					}else{
						//no
						if (board[i][j] == 0){
							if (k == 0){
								cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][0]);
							}
							if (k == 1){
								cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][1]);
							}
							if (k == 2){
								cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][2]);
							}
							if (k == 3){
								cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][3]);
							}
						}
						if (board[i][j] == 1){//right
							if (k == 0){
								cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][0]);
							}
							if (k == 1){
								cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][1]);
							}
							if (k == 2){
								cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][2]);
							}
							if (k == 3){
								cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][3]);
							}
						}
						if (board[i][j] == 2){//back
							if (k == 0){
								cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][0]);
							}
							if (k == 1){
								cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][1]);
							}
							if (k == 2){
								cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][2]);
							}
							if (k == 3){
								cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][3]);
							}
						}
						if (board[i][j] == 3){//left
							if (k == 0){
								cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][0]);
							}
							if (k == 1){
								cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][1]);
							}
							if (k == 2){
								cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][2]);
							}
							if (k == 3){
								cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][3]);
							}

						}
						//straight
						if (k == 0){
							cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][0] + cost[0]);
						}
						if (k == 1){
							cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][1] + cost[0]);
						}
						if (k == 2){
							cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][2] + cost[0]);
						}
						if (k == 3){
							cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][3] + cost[0]);
						}
						//right
						if (k == 0){
							cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][0] + cost[1]);
						}
						if (k == 1){
							cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][1] + cost[1]);
						}
						if (k == 2){
							cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][2] + cost[1]);
						}
						if (k == 3){
							cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][3] + cost[1]);
						}
						//back
						if (k == 0){
							cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][0] + cost[2]);
						}
						if (k == 1){
							cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][1] + cost[2]);
						}
						if (k == 2){
							cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][2] + cost[2]);
						}
						if (k == 3){
							cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][3] + cost[2]);
						}
						//left
						if (k == 0){
							cboard[i - 1][j][1] = min(cboard[i - 1][j][1], cboard[i][j][0] + cost[3]);
						}
						if (k == 1){
							cboard[i][j - 1][2] = min(cboard[i][j - 1][2], cboard[i][j][1] + cost[3]);
						}
						if (k == 2){
							cboard[i + 1][j][3] = min(cboard[i + 1][j][3], cboard[i][j][2] + cost[3]);
						}
						if (k == 3){
							cboard[i][j + 1][0] = min(cboard[i][j + 1][0], cboard[i][j][3] + cost[3]);
						}
					}
				}
			}
			cnt++;
			// for (int k = 0; k < 4; k++){
			// 	ans = min(ans, cboard[h][w][k]);
			// }
		}
	}
	cout << ans << endl;
}
return 0;
}

Measured memory_bytes: 1196