This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int grid[401][401],ver[401][401],hor[401][401];
LLI dist[802][802],ver2[802][802],hor2[802][802];
int visited[401][401],parent[401][401];
priority_queue<pair<LLI,pii> > H;
int main() {
    int i,j;
    int N,M;
    scanf("%d %d",&N,&M);
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) scanf("%d",&grid[i][j]);
    }
    for (i = 0; i < N; i++) {
        for (j = 0; j <= M; j++) scanf("%d",&ver[i][j]);
    }
    for (i = 0; i <= N; i++) {
        for (j = 0; j < M; j++) scanf("%d",&hor[i][j]);
    }
    for (i = 0; i <= N; i++) {
        for (j = 0; j <= M; j++) dist[i][j] = 1e18;
    }
    dist[0][0] = 0,H.push(mp(0,mp(0,0)));
    while (!H.empty()) {
        LLI d = -H.top().first;
        int x = H.top().second.first;
        int y = H.top().second.second;
        H.pop();
        if ((x > 0) && (d+hor[y][x-1] < dist[y][x-1])) {
            dist[y][x-1] = d+hor[y][x-1],parent[y][x-1] = 2;
            H.push(mp(-dist[y][x-1],mp(x-1,y)));
        }
        if ((x < M) && (d+hor[y][x] < dist[y][x+1])) {
            dist[y][x+1] = d+hor[y][x],parent[y][x+1] = 0;
            H.push(mp(-dist[y][x+1],mp(x+1,y)));
        }
        if ((y > 0) && (d+ver[y-1][x] < dist[y-1][x])) {
            dist[y-1][x] = d+ver[y-1][x],parent[y-1][x] = 3;
            H.push(mp(-dist[y-1][x],mp(x,y-1)));
        }
        if ((y < N) && (d+ver[y][x] < dist[y+1][x])) {
            dist[y+1][x] = d+ver[y][x],parent[y+1][x] = 1;
            H.push(mp(-dist[y+1][x],mp(x,y+1)));
        }
    }
    for (i = 0; i < 2*(N+1); i++) {
        for (j = 0; j < 2*(M+1); j++) {
            if ((i & 1) && (j & 1)) ver2[i][j] = ver[i/2][j/2],hor2[i][j] = hor[i/2][j/2];
            else if (i & 1) ver2[i][j] = ver[i/2][j/2];
            else if (j & 1) hor2[i][j] = hor[i/2][j/2];
        }
    }
    hor2[0][0] = ver2[0][0] = 1e18;
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++) {
            if (grid[i][j]) {
                ver2[2*i][2*j+1] = ver2[2*i][2*j+2] = ver2[2*i+2][2*j+1] = ver2[2*i+2][2*j+2] = 1e18;
                hor2[2*i+1][2*j] = hor2[2*i+2][2*j] = hor2[2*i+1][2*j+2] = hor2[2*i+2][2*j+2] = 1e18;
                int x = j,y = i;
                while (((x > 0) || (y > 0)) && !visited[y][x]) {
                    visited[y][x] = 1;
                    if (parent[y][x] == 0) {
                        ver2[2*y][2*x] = ver2[2*y][2*x-1] = 1e18;
                        x--;
                    }
                    else if (parent[y][x] == 1) {
                        hor2[2*y][2*x] = hor2[2*y-1][2*x] = 1e18;
                        y--;
                    }
                    else if (parent[y][x] == 2) {
                        ver2[2*y][2*x+1] = ver2[2*y][2*x+2] = 1e18;
                        x++;
                    }
                    else {
                        hor2[2*y+1][2*x] = hor2[2*y+2][2*x] = 1e18;
                        y++;
                    }
                }
            }
        }
    }
    for (i = 0; i < 2*(N+1); i++) {
        for (j = 0; j < 2*(M+1); j++) dist[i][j] = 1e18;
    }
    dist[0][1] = 0,H.push(mp(0,mp(1,0)));
    while (!H.empty()) {
        LLI d = -H.top().first;
        int x = H.top().second.first;
        int y = H.top().second.second;
        H.pop();
        if ((x > 0) && (d+hor2[y][x-1] < dist[y][x-1])) {
            dist[y][x-1] = d+hor2[y][x-1];
            H.push(mp(-dist[y][x-1],mp(x-1,y)));
        }
        if ((x < 2*M+1) && (d+hor2[y][x] < dist[y][x+1])) {
            dist[y][x+1] = d+hor2[y][x];
            H.push(mp(-dist[y][x+1],mp(x+1,y)));
        }
        if ((y > 0) && (d+ver2[y-1][x] < dist[y-1][x])) {
            dist[y-1][x] = d+ver2[y-1][x];
            H.push(mp(-dist[y-1][x],mp(x,y-1)));
        }
        if ((y < 2*N+1) && (d+ver2[y][x] < dist[y+1][x])) {
            dist[y+1][x] = d+ver2[y][x];
            H.push(mp(-dist[y+1][x],mp(x,y+1)));
        }
    }
    printf("%lld\n",dist[1][0]);
    return 0;
}
Compilation message (stderr)
wall.cpp: In function 'int main()':
wall.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
wall.cpp:67:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |         for (j = 0; j < M; j++) scanf("%d",&grid[i][j]);
      |                                 ~~~~~^~~~~~~~~~~~~~~~~~
wall.cpp:70:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |         for (j = 0; j <= M; j++) scanf("%d",&ver[i][j]);
      |                                  ~~~~~^~~~~~~~~~~~~~~~~
wall.cpp:73:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |         for (j = 0; j < M; j++) scanf("%d",&hor[i][j]);
      |                                 ~~~~~^~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |