#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;

int main()
{
  int w,h;
  for(;cin>>w>>h,w;)
    {
      int maps[h][w];
      for(int i=0;i<h;i++)
	for(int j=0;j<w;j++)
	  {
	    cin>>maps[i][j];
	  }
      int c[4];
      for(int i=0;i<4;i++)
	cin>>c[i];

      //cost,move,now;
      priority_queue<pair<pair<int,int>,pair<int,int> > > qu;
      qu.push(make_pair(make_pair(0,0),make_pair(0,0)));
      
      pair<int,int> goal=make_pair(h-1,w-1);
      int dx[]={0,1,0,-1};
      int dy[]={1,0,-1,0};
      bool closed[4][h][w];
      memset(closed,false,sizeof(closed));
      while(!qu.empty())
	{
	  int cost=-qu.top().first.first;
	  int move=qu.top().first.second;
	  pair<int,int> now=qu.top().second;
	  qu.pop();
	  if(now==goal)
	    {
	      cout<<cost<<endl;
	      break;
	    }
	  int x=now.first;
	  int y=now.second;
	  //  cout<<cost<<" "<<move<<" <"<<x<<","<<y<<">"<<endl;
	  if(closed[move][x][y])
	    continue;
	  closed[move][x][y]=true;
	  
	  for(int i=0;i<4;i++)
	    {
	      int add=0;
	      if(maps[x][y]!=i)
		add=c[i];
	      if(x+dx[move]>=0 && x+dx[move]<h &&
		 y+dy[move]>=0 && y+dy[move]<w &&
		 !closed[move][x+dx[move]][y+dy[move]])
		qu.push(make_pair(make_pair(-(cost+add),move),
				  make_pair(x+dx[move],y+dy[move])));
	      move=(move+1)%4;
	    }
	}
    }

}