Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

const int MAX_V = 4000;
const int INF = 9999999;

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

int cost[MAX_V][MAX_V];
int d[MAX_V];
bool used[MAX_V];
int V;

int h, w;

bool in(int i, int j){
	return 0 <= i && i < h && 0 <= j && j < w;
}

int toV(int i, int j, int dir){
	return 4 * (i * w + j) + dir;
}

void calc(){
	fill(d, d + V, INF);
	fill(used, used + V, false);
	d[0] = 0;

	while (true) {
		int v = -1;
		for (int u = 0; u < V; u++) {
			if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
		}

		if (v == -1)break;
		used[v] = true;
		for (int u = 0; u < V; u++) {
			d[u] = min(d[u], d[v] + cost[v][u]);
		}
	}
}

int main(){
	while (cin >> w >> h , w != 0 || h != 0) {
		vvi s = vvi(h, vi(w, 0));
		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> s[i][j];
		int c[4];
		for (int i = 0; i < 4; i++)
			cin >> c[i];

		V = 4 * w * h;
		fill_n(cost, MAX_V * MAX_V, INF);
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				for (int dir = 0; dir < 4; dir++) {
					cost[toV(i, j, dir)][toV(i, j, dir)] = 0;
					for (int ndir = 0; ndir < 4; ndir++) {
						int ni = i + dy[ndir], nj = j + dx[ndir];
						if (!in(ni, nj)) continue;
						if (s[i][j] != 4 && (dir + s[i][j]) % 4 == ndir)
							cost[toV(i, j, dir)][toV(ni, nj, ndir)] = 0;
						else
							cost[toV(i, j, dir)][toV(ni, nj, ndir)] = c[(ndir - dir + 4) % 4];
					}
				}
			}
		}

		calc();

		int ans = INF;
		for (int dir = 0; dir < 4; dir++) {
			ans = min(ans, d[toV(h - 1, w - 1, dir)]);
		}

		cout << ans << endl;
	}
}

Compiler output:
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of 'typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int (*)[4000]; _Tp = int; typename __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type = void]':
/usr/include/c++/14/bits/stl_algobase.h:998:21:   required from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = int (*)[4000]; _Tp = int]'
  998 |     { std::__fill_a1(__first, __last, __value); }
      |       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1154:20:   required from '_OutputIterator std::__fill_n_a(_OutputIterator, _Size, const _Tp&, random_access_iterator_tag) [with _OutputIterator = int (*)[4000]; _Size = int; _Tp = int]'
 1154 |       std::__fill_a(__first, __first + __n, __value);
      |       ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1183:29:   required from '_OI std::fill_n(_OI, _Size, const _Tp&) [with _OI = int (*)[4000]; _Size = int; _Tp = int]'
 1183 |       return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
      |              ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1184 |                                std::__iterator_category(__first));
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:59:9:   required from here
   59 |                 fill_n(cost, MAX_V * MAX_V, INF);
      |                 ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:952:18: error: incompatible types in assignment of 'const int' to 'int [4000]'
  952 |         *__first = __tmp;
      |         ~~~~~~~~~^~~~~~~