#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define pb(a) push_back(a)
#define pr(a) cout<<(a)<<endl
#define PR(a,b) cout<<(a)<<" "<<(b)<<endl
#define R cin>>
#define F first
#define S second
#define ll long long
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;
typedef pair<P,P> P2;

int main() {
  int n,m;
  while(cin >> m >> n && n) {
    int a[n][m];
    rep(i,n)rep(j,m) R a[i][j];
    int d[n][m][4],c[4];
    rep(i,n)rep(j,m)rep(k,4) d[i][j][k]=MAX;
    rep(i,4) R c[i];
    d[0][0][1]=0;
    priority_queue<P2,vector<P2>,greater<P2> > que;
    que.push(P2(P(0,1),P(0,0)));
    int ans=-1;
    while(!que.empty()) {
      P2 p=que.top();que.pop();
      int nx=p.S.F,ny=p.S.S,cc=p.F.F,k=p.F.S;
      if(cc<d[nx][ny][k]) continue;
      if(nx==n-1 && ny==m-1) {
	ans=d[nx][ny][k];
	break;
      }
      rep(i,4) {
	int z=c[i],kk=(k+i)%4;
	int x=nx+dx[kk],y=ny+dy[kk];
	if(!check(n,m,x,y)) continue;
	if(a[nx][ny]==i) z=0;
	if(d[x][y][kk]>d[nx][ny][k]+z) {
	  d[x][y][kk]=d[nx][ny][k]+z;
	  que.push(P2(P(d[x][y][kk],kk),P(x,y)));
	}
      }
    }
    pr(ans);
  }
  return 0;
}
