//#include <bits/stdc++.h>
//#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>
 
 
#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000
 
using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;

 
int dy[5] = { 0,0,1,-1,0 };
int dx[5] = { 1,-1,0,0,0 };

struct edge{
			long long int to,cost;
		};

 
int main(void){

	while(1){
		long long int w,h;
		

		cin>>w>>h;
		if(w==0&h==0){
			break;
		}
		
		long long int s[31][31];
		long long int c[4];

		FOR(i,1,h){
			FOR(j,1,w){
				cin>>s[i][j];
			}
		}
		FOR(i,0,3){
			cin>>c[i];
		}

		

		vector<edge> G[18000];
		long long int d[18000];

		edge ED;

		LFOR(y,1,h){
			LFOR(x,1,w){
				LFOR(l,0,3){
					ED.cost=LINF;
					switch(l){
						case 0:
							LFOR(k,0,4){
								if(s[y][x]==k){
									if(x<w){
										ED.to=4*w*(y-1)+4*x+l;
										if(k==0){
											ED.cost=0;
										}else{
											ED.cost=c[0];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y<h){
										ED.to=4*w*y+4*(x-1)+l+3;
										if(k==1){
											ED.cost=0;
										}else{
											ED.cost=c[1];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(x>1){
										ED.to=4*w*(y-1)+4*(x-2)+l+1;
										if(k==2){
											ED.cost=0;
										}else{
											ED.cost=c[2];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y>1){
										ED.to=4*w*(y-2)+4*(x-1)+l+2;
										if(k==3){
											ED.cost=0;
										}else{
											ED.cost=c[3];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
								}
							}
							break;
						case 1:
							LFOR(k,0,4){
								if(s[y][x]==k){
									if(x<w){
										ED.to=4*w*(y-1)+4*x+l-1;
										if(k==2){
											ED.cost=0;
										}else{
											ED.cost=c[2];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y<h){
										ED.to=4*w*y+4*(x-1)+l+2;
										if(k==3){
											ED.cost=0;
										}else{
											ED.cost=c[3];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(x>1){
										ED.to=4*w*(y-1)+4*(x-2)+l;
										if(k==0){
											ED.cost=0;
										}else{
											ED.cost=c[0];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y>1){
										ED.to=4*w*(y-2)+4*(x-1)+l+1;
										if(k==1){
											ED.cost=0;
										}else{
											ED.cost=c[1];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
								}
							}
							break;
						case 2:
							LFOR(k,0,4){
								if(s[y][x]==k){
									if(x<w){
										ED.to=4*w*(y-1)+4*x+l-2;
										if(k==1){
											ED.cost=0;
										}else{
											ED.cost=c[1];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y<h){
										ED.to=4*w*y+4*(x-1)+l+1;
										if(k==2){
											ED.cost=0;
										}else{
											ED.cost=c[2];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(x>1){
										ED.to=4*w*(y-1)+4*(x-2)+l-1;
										if(k==3){
											ED.cost=0;
										}else{
											ED.cost=c[3];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y>1){
										ED.to=4*w*(y-2)+4*(x-1)+l;
										if(k==0){
											ED.cost=0;
										}else{
											ED.cost=c[0];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
								}
							}
							break;
						case 3:
							LFOR(k,0,4){
								if(s[y][x]==k){
									if(x<w){
										ED.to=4*w*(y-1)+4*x+l-3;
										if(k==3){
											ED.cost=0;
										}else{
											ED.cost=c[3];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y<h){
										ED.to=4*w*y+4*(x-1)+l;
										if(k==0){
											ED.cost=0;
										}else{
											ED.cost=c[0];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(x>1){
										ED.to=4*w*(y-1)+4*(x-2)+l-2;
										if(k==1){
											ED.cost=0;
										}else{
											ED.cost=c[1];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
									if(y>1){
										ED.to=4*w*(y-2)+4*(x-1)+l-1;
										if(k==2){
											ED.cost=0;
										}else{
											ED.cost=c[2];
										}
										G[4*w*(y-1)+4*(x-1)+l].push_back(ED);
									}
								}
							}
							break;
					}

				}
			}
		}

		priority_queue<P,vector<P>,greater<P> > que;
		LFOR(i,0,4*w*(h-1)+4*(w-1)+4){
			d[i]=LINF;
		}
		d[0]=0;
		que.push(P(0,0));

		while(!que.empty()){
			P p=que.top();
			que.pop();
			long long int v=p.second;

			if(d[v]<p.first){
				continue;
			}

			LFOR(i,0,G[v].size()-1){
				edge e=G[v][i];
				if(d[e.to]>d[v]+e.cost){
					d[e.to]=d[v]+e.cost;
					que.push(P(d[e.to],e.to));
				}
			}
		}		

		long long int mini=LINF;
		FOR(i,0,4){
			if(d[4*w*(h-1)+4*(w-1)+i]<mini){
				mini=d[4*w*(h-1)+4*(w-1)+i];
			}
		}

		cout<<mini<<endl;

	}

}



Measured memory_bytes: 3976