#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef long long ll;

const int MAX_SIZE = 10000;

struct edge{
	int x,y;
	int angle;
	int cost;
};

vector<edge> G[30][30][4];
int d[30][30][4];
// cost,angle,y,x
typedef pair<pair<int,int>,pair<int,int> > P;
int V;
const int INF = 10000000;
int field[31][31];
int w,h;

void dijkstra(pair<int,int> s){
	priority_queue<P,vector<P>,greater<P> > que;

	for(int i = 0; i < 30; i++)
		for(int j = 0; j < 30; j++)
			fill(d[i][j],d[i][j]+4,INF);

	d[s.first][s.second][1] = 0;
	que.push(P(make_pair(0,1),s));
	while(!que.empty()){
		P p = que.top();
		que.pop();
		pair<int,int> v = p.second;
		if(d[v.first][v.second][p.first.second] < p.first.first)
			continue;
		for(int i = 0; i < G[v.first][v.second][p.first.second].size(); i++){
			edge e = G[v.first][v.second][p.first.second][i];
			if(d[e.y][e.x][e.angle] > d[v.first][v.second][p.first.second] + e.cost){
				d[e.y][e.x][e.angle] = d[v.first][v.second][p.first.second] + e.cost;
				que.push(P(make_pair(d[e.y][e.x][e.angle],e.angle),make_pair(e.y,e.x)));
			}
		}
	}

	// ツゴツーツδ仰づ個マツスツつェツ津「ツ止ツ鳴スツ療淞づ個湘ェツ債つゥツづつ、ツつゥツづ湘ェツ債づュツつッ
	int minCost = INF;
	for(int i = 0; i < 4; i++){
		minCost = min(minCost,d[h-1][w-1][i]);
	}
	cout << minCost << endl;

}

int main(){
	// Gツづ個凝ヲツ静伉づィツづ債つサツづェツつシツづェツ督ッツつカツ暗環置ツづ個暗。ツつ、ツ古シツつォツづーツ表ツつキ
	// G 0-3,4-6,7-9,10-12...
	// 0ツ湘」,1ツ右,2ツ可コ,3ツ債カ
	while(cin >> w >> h && !(w == 0 && h == 0)){
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				cin >> field[i][j];
				for(int k = 0; k < 4; k++){
					G[i][j][k].clear();
				}
			}
		}
		int c[4];
		cin >> c[0] >> c[1] >> c[2] >> c[3];

		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				edge e;
				e.x = j;
				e.y = i;
				for(int k = 0; k < 4; k++){
					if(k == 0 && i >= 1){
						if(field[i][j] == 0)
							e.cost = 0;
						else{
							e.cost = c[0];
						}

						e.y = i-1;
						e.x = j;
						e.angle = 0;
						G[i][j][0].push_back(e);
					}
					else if(k == 1 && j < w - 1){
						if(field[i][j] == 0)
							e.cost = 0;
						else{
							e.cost = c[0];
						}
						e.y = i;
						e.x = j + 1;
						e.angle = 1;
						G[i][j][1].push_back(e);
					}
					else if(k == 2 && i < h - 1){
						if(field[i][j] == 0)
							e.cost = 0;
						else{
							e.cost = c[0];
						}
						e.angle = 2;
						e.y = i + 1;
						e.x = j;
						G[i][j][2].push_back(e);
					}
					else if(k == 3 && j >= 1){
						if(field[i][j] == 0)
							e.cost = 0;
						else{
							e.cost = c[0];
						}
						e.angle = 3;
						e.y = i;
						e.x = j - 1;
						G[i][j][3].push_back(e);
					}


					if(k == 0 && j < w - 1){
						if(field[i][j] == 1)
							e.cost = 0;
						else{
							e.cost = c[1];
						}

						e.angle = 1;
						e.y = i;
						e.x = j+1;
						G[i][j][0].push_back(e);
					}
					else if(k == 1 && i < h - 1){
						if(field[i][j] == 1)
							e.cost = 0;
						else{
							e.cost = c[1];
						}
						e.angle = 2;
						e.y = i+1;
						e.x = j;
						G[i][j][1].push_back(e);
					}
					else if(k == 2 && j >= 1){
						if(field[i][j] == 1)
							e.cost = 0;
						else{
							e.cost = c[1];
						}
						e.angle = 3;
						e.y = i;
						e.x = j-1;
						G[i][j][2].push_back(e);
					}
					else if(k == 3 && i >= 1){
						if(field[i][j] == 1)
							e.cost = 0;
						else{
							e.cost = c[1];
						}
						e.angle = 0;
						e.y = i - 1;
						e.x = j;
						G[i][j][3].push_back(e);
					}

					if(k == 0 && i < h-1){
						if(field[i][j] == 2)
							e.cost = 0;
						else{
							e.cost = c[2];
						}
						e.angle = 2;
						e.y = i+1;
						e.x = j;
						G[i][j][0].push_back(e);
					}
					else if(k == 1 && j >=1){
						if(field[i][j] == 2)
							e.cost = 0;
						else{
							e.cost = c[2];
						}
						e.angle = 3;
						e.y = i;
						e.x = j - 1;
						G[i][j][1].push_back(e);
					}
					else if(k == 2 && i >= 1){
						if(field[i][j] == 2)
							e.cost = 0;
						else{
							e.cost = c[2];
						}
						e.angle = 0;
						e.y = i - 1;
						e.x = j;
						G[i][j][2].push_back(e);
					}
					else if(k == 3 && j < w - 1){
						if(field[i][j] == 2)
							e.cost = 0;
						else{
							e.cost = c[2];
						}
						e.angle = 1;
						e.y = i;
						e.x = j + 1;
						G[i][j][3].push_back(e);
					}

					if(k == 0 && j >= 1){
						if(field[i][j] == 3)
							e.cost = 0;
						else{
							e.cost = c[3];
						}
						e.angle = 3;
						e.y = i;
						e.x = j-1;
						G[i][j][0].push_back(e);
					}
					else if(k == 1 && i >= 1){
						if(field[i][j] == 3)
							e.cost = 0;
						else{
							e.cost = c[3];
						}
						e.angle = 0;
						e.y = i-1;
						e.x = j;
						G[i][j][1].push_back(e);
					}
					else if(k == 2 && j < w - 1){
						if(field[i][j] == 3)
							e.cost = 0;
						else{
							e.cost = c[3];
						}
						e.angle = 1;
						e.y = i;
						e.x = j+1;
						G[i][j][2].push_back(e);
					}
					else if(k == 3 && i < h - 1){
						if(field[i][j] == 3)
							e.cost = 0;
						else{
							e.cost = c[3];
						}
						e.angle = 2;
						e.y = i + 1;
						e.x = j;
						G[i][j][3].push_back(e);
					}
}
}
}
//
dijkstra(make_pair(0,0));
}
return 0;
}

Measured memory_bytes: 1328