func_code_string: #include <bits/stdc++.h>      using namespace std;      #define ll long long   typedef pair<ll, ll> pll;   typedef pair<ll, int> pli;   typedef pair<int, ll> pil;   typedef pair<int, int> pii;   typedef vector<int> vi;   typedef vector<vi> vvi;   typedef vector<bool> vb;   typedef vector<vb> vvb;   typedef vector<char> vc;   typedef vector<vc> vvc;   typedef vector<string> vs;   typedef vector<vs> vvs;   typedef vector<ll> vll;   typedef vector<vll> vvll;   typedef vector<pii> vpii;   typedef vector<vpii> vvpii;   typedef vector<pll> vpll;   typedef vector<vpll> vvpll;   typedef set<int> si;   typedef set<vi> svi;   typedef set<ll> sll;   typedef set<vll> svll;   typedef set<pii> spii;   typedef set<vpii> svpii;   typedef set<pll> spll;   typedef set<vpll> svpll;   typedef vector<si> vsi;   typedef vector<sll> vsll;   typedef vector<spii> vspii;   typedef vector<spll> vspll;   #define ft first   #define sc second   #define pb push_back   #define ins insert   #define io                                ios_base::sync_with_stdio(false);     cin.tie(NULL);                        cout.tie(NULL)   #define all(v) v.begin(), v.end()   #define Sort(v) sort(all(v))   #define sortarr(a, n) sort(a, a + n)   #define tt         ll tc;         cin >> tc;     while (tc--)   #define rep(var, start, end, interval) for (ll var = start; (interval > 0 and var < end) or (interval < 0 and var > end); var += interval)   #define each(x, v) for (auto x : v)   #define takearr0(a, n) rep(z, 0, n, 1) cin >> a[z];   #define takearr1(a, n) rep(z, 1, n + 1, 1) cin >> a[z];      const ll max_n = 2e3 + 5;   const ll inf = 1e16;      ll n, m, w;   vvll a;      vpll neighbours(pll p)   {    vpll vec;    if (p.ft > 0 and a[p.ft - 1][p.sc] >= 0)     vec.pb({p.ft - 1, p.sc});    if (p.ft < n - 1 and a[p.ft + 1][p.sc] >= 0)     vec.pb({p.ft + 1, p.sc});    if (p.sc > 0 and a[p.ft][p.sc - 1] >= 0)     vec.pb({p.ft, p.sc - 1});    if (p.sc < m - 1 and a[p.ft][p.sc + 1] >= 0)     vec.pb({p.ft, p.sc + 1});    return vec;   }      int main()   {    io;    cin >> n >> m >> w;    a.resize(n);    a.assign(n, vll(m));    rep(i, 0, n, 1) takearr0(a[i], m);    // each(vec, a)    // {    //  each(x, vec) cout << setw(18) << x <<    ;    //  cout << endl;    // }    // cout << endl;    vvb v(n, vb(m, false));    vvll dists(n, vll(m, inf));    queue<pll> q;    q.push({0, 0});    v[0][0] = true;    dists[0][0] = 0;    while (!q.empty())    {     pll p = q.front();     q.pop();     vpll nb = neighbours(p);     each(pr, nb) if (!v[pr.ft][pr.sc])     {      dists[pr.ft][pr.sc] = dists[p.ft][p.sc] + w;      v[pr.ft][pr.sc] = true;      q.push(pr);     }    }    rep(i, 0, n, 1) rep(j, 0, m, 1) v[i][j] = false;    vvll distt(n, vll(m, inf));    q.push({n - 1, m - 1});    v[n - 1][m - 1] = true;    distt[n - 1][m - 1] = 0;    while (!q.empty())    {     pll p = q.front();     q.pop();     vpll nb = neighbours(p);     each(pr, nb) if (!v[pr.ft][pr.sc])     {      distt[pr.ft][pr.sc] = distt[p.ft][p.sc] + w;      v[pr.ft][pr.sc] = true;      q.push(pr);     }    }    ll pors = inf, port = inf;    rep(i, 0, n, 1) rep(j, 0, m, 1) if (a[i][j] > 0)    {     pors = min(pors, dists[i][j] + a[i][j]);     port = min(port, distt[i][j] + a[i][j]);    }    ll ans = min(dists[n - 1][m - 1], pors + port);    if (ans >= inf)     cout << -1 << endl;    else     cout << ans << endl;    return 0;   }