/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

class TwirlingRobot {
public:

    int w, h;
    int p(int i, int j, int k) {
      return i*w*h + j*w + k;
    }

    void solve(istream& cin, ostream& cout) {
      SPEED;
      while(true) {
        cin >> w >> h;
        if (w==0 && h==0) return;

        int dp[4*h*w];
        int mat[h][w];
        vector<P> graph[4*h*w];

        rep(j, h) {
          rep(k, w) {
            cin >> mat[j][k];
          }
        }

        int a, b, c, d;
        cin >> a >> b >> c >> d;

        rep(i, 4*h*w) {
          dp[i] = INT_MAX/2;
        }
        // right: 0
        // down: 1
        // left: 2
        // up: 3
        dp[0] = 0;

        rep(j, h) {
          rep(k, w) {
            // straight;
            int aa = a;
            int bb = b;
            int cc = c;
            int dd = d;
            if (mat[j][k] == 0) aa = 0;
            if (mat[j][k] == 1) bb = 0;
            if (mat[j][k] == 2) cc = 0;
            if (mat[j][k] == 3) dd = 0;
            // move right
            if (k+1<w) {
              graph[p(0, j, k)].emplace_back(p(0, j, k+1), aa);
              graph[p(1, j, k)].emplace_back(p(0, j, k+1), dd);
              graph[p(2, j, k)].emplace_back(p(0, j, k+1), cc);
              graph[p(3, j, k)].emplace_back(p(0, j, k+1), bb);
            }
            // down
            if (j+1<h) {
              graph[p(0, j, k)].emplace_back(p(1, j+1, k), bb);
              graph[p(1, j, k)].emplace_back(p(1, j+1, k), aa);
              graph[p(2, j, k)].emplace_back(p(1, j+1, k), dd);
              graph[p(3, j, k)].emplace_back(p(1, j+1, k), cc);
            }
            // move left
            if (k>0) {
              graph[p(0, j, k)].emplace_back(p(2, j, k-1), cc);
              graph[p(1, j, k)].emplace_back(p(2, j, k-1), bb);
              graph[p(2, j, k)].emplace_back(p(2, j, k-1), aa);
              graph[p(3, j, k)].emplace_back(p(2, j, k-1), dd);
            }
            // up
            if (j>0) {
              graph[p(0, j, k)].emplace_back(p(3, j-1, k), dd);
              graph[p(1, j, k)].emplace_back(p(3, j-1, k), cc);
              graph[p(2, j, k)].emplace_back(p(3, j-1, k), bb);
              graph[p(3, j, k)].emplace_back(p(3, j-1, k), aa);
            }
          }
        }

        priority_queue<P, vector<P>, greater<>> que;
        que.emplace(0, 0);
        dp[0] = 0;
        while(!que.empty()) {
          auto p = que.top(); que.pop();
          if (p.first > dp[p.second]) continue;
          for(const auto& e: graph[p.second]) {
            if (dp[e.first] > p.first + e.second) {
              dp[e.first] = p.first + e.second;
              que.emplace(dp[e.first], e.first);
            }
          }
        }

        int ans = INT_MAX;
        rep(i, 4) {
          chmin(ans, dp[p(i, h-1, w-1)]);
        }
        cout << ans << endl;
      }

    }
};

signed main() {
  TwirlingRobot solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}


Measured memory_bytes: 3596