#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

enum DIR{
    North,
    East,
    South,
    West,
};

int dp[30][30][4],H,W,map[30][30],cost[4];

struct Info{
    Info(int arg_row,int arg_col,DIR arg_dir){
        row = arg_row;
        col = arg_col;
        dir = arg_dir;
    }

    int row,col;
    DIR dir;
};

DIR turn_R(DIR dir){
    switch(dir){
    case North:
        return East;
    case East:
        return South;
    case South:
        return West;
    case West:
        return North;
    }
}

DIR turn_L(DIR dir){
    switch(dir){
    case North:
        return West;
    case East:
        return North;
    case South:
        return East;
    case West:
        return South;
    }
}

DIR turn_180(DIR dir){
    switch(dir){
    case North:
        return South;
    case East:
        return West;
    case South:
        return North;
    case West:
        return East;
    }
}

bool rangeCheck(int row,int col){
    if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
    else{
        return false;
    }
}

int main(){

    int next_row,next_col;
    DIR next_dir;

    while(true){
        scanf("%d %d",&W,&H);
        if(W == 0 && H == 0)break;

        for(int i = 0; i < H; i++){
            for(int k = 0; k < W; k++)scanf("%d",&map[i][k]);
        }

        for(int i = 0; i < 4; i++)scanf("%d",&cost[i]);

        for(int i = 0; i < H; i++){
            for(int k = 0; k < W; k++){
                for(int p = 0; p < 4; p++)dp[i][k][p] = BIG_NUM;
            }
        }

        dp[0][0][East] = 0;

        queue<Info> Q;

        Q.push(Info(0,0,East));

        while(!Q.empty()){

            if(Q.front().row == H-1 && Q.front().col == W-1){
                Q.pop();
            }else{

                for(int i = 0; i <= 5; i++){
                    switch(i){
                    case 0:
                        next_dir = Q.front().dir;

                        switch(Q.front().dir){
                        case North:
                            next_row = Q.front().row-1;
                            next_col = Q.front().col;
                            break;
                        case East:
                            next_row = Q.front().row;
                            next_col = Q.front().col+1;
                            break;
                        case South:
                            next_row = Q.front().row+1;
                            next_col = Q.front().col;
                            break;
                        case West:
                            next_row = Q.front().row;
                            next_col = Q.front().col-1;
                            break;
                        }

                        if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]+cost[0]){
                            dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir]+cost[0];
                            Q.push(Info(next_row,next_col,next_dir));
                        }
                        break;
                    case 1:
                        next_dir = turn_R(Q.front().dir);
                        switch(Q.front().dir){
                        case North:
                            next_row = Q.front().row;
                            next_col = Q.front().col+1;
                            break;
                        case East:
                            next_row = Q.front().row+1;
                            next_col = Q.front().col;
                            break;
                        case South:
                            next_row = Q.front().row;
                            next_col = Q.front().col-1;
                            break;
                        case West:
                            next_row = Q.front().row-1;
                            next_col = Q.front().col;
                            break;
                        }

                        if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]+cost[1]){
                            dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir]+cost[1];
                            Q.push(Info(next_row,next_col,next_dir));
                        }

                        break;
                    case 2:
                        next_dir = turn_180(Q.front().dir);
                        switch(Q.front().dir){
                        case North:
                            next_row = Q.front().row+1;
                            next_col = Q.front().col;
                            break;
                        case East:
                            next_row = Q.front().row;
                            next_col = Q.front().col-1;
                            break;
                        case South:
                            next_row = Q.front().row-1;
                            next_col = Q.front().col;
                            break;
                        case West:
                            next_row = Q.front().row;
                            next_col = Q.front().col+1;
                            break;
                        }
                        if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]+cost[2]){
                            dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir]+cost[2];
                            Q.push(Info(next_row,next_col,next_dir));
                        }

                        break;
                    case 3:
                        next_dir = turn_L(Q.front().dir);
                        switch(Q.front().dir){
                        case North:
                            next_row = Q.front().row;
                            next_col = Q.front().col-1;
                            break;
                        case East:
                            next_row = Q.front().row-1;
                            next_col = Q.front().col;
                            break;
                        case South:
                            next_row = Q.front().row;
                            next_col = Q.front().col+1;
                            break;
                        case West:
                            next_row = Q.front().row+1;
                            next_col = Q.front().col;
                            break;
                        }
                        if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]+cost[3]){
                            dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir]+cost[3];
                            Q.push(Info(next_row,next_col,next_dir));
                        }
                        break;
                    case 4:
                        switch(map[Q.front().row][Q.front().col]){
                        case 0:
                            next_dir = Q.front().dir;

                            switch(Q.front().dir){
                            case North:
                                next_row = Q.front().row-1;
                                next_col = Q.front().col;
                                break;
                            case East:
                                next_row = Q.front().row;
                                next_col = Q.front().col+1;
                                break;
                            case South:
                                next_row = Q.front().row+1;
                                next_col = Q.front().col;
                                break;
                            case West:
                                next_row = Q.front().row;
                                next_col = Q.front().col-1;
                                break;
                            }

                            if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]){
                                dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir];
                                Q.push(Info(next_row,next_col,next_dir));
                            }
                            break;
                        case 1:
                            next_dir = turn_R(Q.front().dir);
                            switch(Q.front().dir){
                            case North:
                                next_row = Q.front().row;
                                next_col = Q.front().col+1;
                                break;
                            case East:
                                next_row = Q.front().row+1;
                                next_col = Q.front().col;
                                break;
                            case South:
                                next_row = Q.front().row;
                                next_col = Q.front().col-1;
                                break;
                            case West:
                                next_row = Q.front().row-1;
                                next_col = Q.front().col;
                                break;
                            }

                            if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]){
                                dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir];
                                Q.push(Info(next_row,next_col,next_dir));
                            }

                            break;
                        case 2:
                            next_dir = turn_180(Q.front().dir);
                            switch(Q.front().dir){
                            case North:
                                next_row = Q.front().row+1;
                                next_col = Q.front().col;
                                break;
                            case East:
                                next_row = Q.front().row;
                                next_col = Q.front().col-1;
                                break;
                            case South:
                                next_row = Q.front().row-1;
                                next_col = Q.front().col;
                                break;
                            case West:
                                next_row = Q.front().row;
                                next_col = Q.front().col+1;
                                break;
                            }
                            if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]){
                                dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir];
                                Q.push(Info(next_row,next_col,next_dir));
                            }

                            break;
                        case 3:
                            next_dir = turn_L(Q.front().dir);
                            switch(Q.front().dir){
                            case North:
                                next_row = Q.front().row;
                                next_col = Q.front().col-1;
                                break;
                            case East:
                                next_row = Q.front().row-1;
                                next_col = Q.front().col;
                                break;
                            case South:
                                next_row = Q.front().row;
                                next_col = Q.front().col+1;
                                break;
                            case West:
                                next_row = Q.front().row+1;
                                next_col = Q.front().col;
                                break;
                            }
                            if(rangeCheck(next_row,next_col) == true && dp[next_row][next_col][next_dir] > dp[Q.front().row][Q.front().col][Q.front().dir]){
                                dp[next_row][next_col][next_dir] = dp[Q.front().row][Q.front().col][Q.front().dir];
                                Q.push(Info(next_row,next_col,next_dir));
                            }
                            break;
                        case 4:
                            //Do nothing
                            break;
                        }
                        break;
                    }
                }

                Q.pop();
            }
        }


        printf("%d\n",min(dp[H-1][W-1][East],dp[H-1][W-1][South]));

    }

    return 0;
}

Measured memory_bytes: 2624