#include<iostream>
#include<string>
#include<queue>
#include<array>

using namespace std;

#define INF (1 << 29)

struct Entry
{
    int x, y;
    int direction;
};

int main()
{
    int w, h;

    while(cin >> w >> h, (w | h) )
    {
        //cout << "\n\n" << w << " " << h << endl;

        //map[h][w]
        array< array<int, 30>, 30> map{};

        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++)
            {
                cin >> map[i][j];
            }
        }
        
        //command[0]?????´???
        //command[1]?????????
        //command[2]????????¢
        //command[3]?????????
        array<int, 4> command;
        for(int i = 0; i < 4; i++)
        {
            cin >> command[i];
        }

       
        //??????????????\????????????????????????????????¨?????????
        //array< array< array< int, 4>, 30>, 30> cost;
        int cost[4][30][30];

        //cost[0][][]??????
        //cost[1][][]??????
        //cost[2][][]??????
        //cost[3][][]??????


        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++)
            {
                cost[0][i][j] = INF;
                cost[1][i][j] = INF;
                cost[2][i][j] = INF;
                cost[3][i][j] = INF;
            }
        }

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


        //start
        queue<Entry> que;

        que.push(Entry{0, 0, 0});
        cost[0][0][0] = 0;

        bool check = true;

        while(!que.empty())
        {
            Entry now = que.front();
            que.pop();
            //cout << que.size() << " : (" << now.x << ", " << now.y  << ") " << now.direction << " : " << cost[now.direction][now.y][now.x] << endl;

            //????????????????????£?????????????????????
            int followedDirection = now.direction + map[now.y][now.x];
            followedDirection %= 4;

            //halt
            if(map[now.y][now.x] == 4)followedDirection = INF;

            //(now.direction + i) % 4????¬??????????
            //i????????¢???????§????
            for(int i = 0; i < 4; i++)
            {
                int nextDirection = (now.direction + i) % 4;

                int nextX = now.x + dx[nextDirection];
                int nextY = now.y + dy[nextDirection];

                //???????????????????????????
                if( !(nextX < 0 || nextX >= w || nextY < 0 || nextY >= h) ) 
                {
                    int tmp_cost = cost[now.direction][now.y][now.x] + (nextDirection != followedDirection) * command[i];

                    //cout << cost[2][h - 1][w - 1] << "  ";

                    //?????????????°???????????????°??´??°
                    if(tmp_cost < cost[nextDirection][nextY][nextX])
                    {
                        cost[nextDirection][nextY][nextX] = tmp_cost;
                        que.push( Entry{ nextX, nextY, nextDirection } );

                    /*
                    if(cost[2][h - 1][w - 1] != INF && check)
                    {
                        cout << "\n\n";

                        cout <<  "(" << nextX << ", " << nextY << ") " << nextDirection << " : " << cost[nextDirection][nextY][nextX] << " " << cost[2][h - 1][w - 1] << endl;
                        cout << que.size() << " : (" << now.x << ", " << now.y  << ") " << now.direction << " : " << cost[now.direction][now.y][now.x] << endl;
                        
                        cout << "\n\n";
                    }
                    check = cost[2][h - 1][w - 1] == INF;                
                    */
                    }
                }

            }
        }

        int ans = INF;
        for(int i = 0; i < 4; i++)
        {
            if(cost[i][h - 1][w - 1] < ans)ans = cost[i][h - 1][w - 1];
            //cout << cost[i][h - 1][w - 1] << " ";
        }
//        cout << ans << endl;
        cout << min(cost[0][h - 1][w - 1], cost[1][h - 1][w - 1]) << endl;
    }

    return 0;
}

Measured memory_bytes: 2976