/*
================================
TEMPLATE BY
VAMSI KRISHNA VALLURU
================================
*/

#include<bits/stdc++.h>
#include<sstream>
#include<iomanip>
using namespace std;

typedef int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;

#define forf(i,m,n) for (ll i=(m);i<(n);i++)
#define forb(i,m,n) for (ll i=(m);i>=(n);--i)
#define mk make_pair
#define fst first
#define snd second

// const ll inf = LLONG_MAX;
const ll inf = 1e9;
// const ll inf = 1e18;
// const ll inf = INT_MAX;

const ll mod = 1e9+7;
// const ll mod = 998244353;

/*
================================
DATA STRUCTURES
================================
*/

class minsegtree{
    public:
    ll n, N;
    vl tree;
    minsegtree(ll _n){
        n = _n;
        N = 4*n;
        tree = vl(N, inf);
    }
    void pointUpdate(ll pos, ll l, ll r, ll idx, ll val){
        if(l == r){
        if(idx == l) tree[pos] = val;
        return;
        }
        if(idx < l || r < idx) return;
        pointUpdate(2*pos+1, l, (l+r)/2, idx, val);
        pointUpdate(2*pos+2, (l+r)/2+1, r, idx, val);
        tree[pos] = min(tree[2*pos+1], tree[2*pos+2]);
    }
    void pointUpdate(ll idx, ll val){
        pointUpdate(0, 0, n-1, idx, val);
    }
    ll rangeMin(ll pos, ll l, ll r, ll lo, ll hi){
        if(lo <= l && r <= hi) return tree[pos];
        if(r < lo || hi < l) return inf;
        return min(
        rangeMin(2*pos+1, l, (l+r)/2, lo, hi),
        rangeMin(2*pos+2, (l+r)/2+1, r, lo, hi)
        );
    }
    ll rangeMin(ll lo, ll hi){
        return rangeMin(0, 0, n-1, lo, hi);
    }
};

/*
================================
COMBINATORICS AND NUMBER THEORY:
================================
*/

// Extended Euclid's Algo:
ll ExtendedEuclid(ll a, ll b, ll &x, ll &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    ll g = ExtendedEuclid(b, a%b, x, y);
    ll nx = y, ny = x-a/b*y;
    x = nx; y = ny;
    return g;
}

// Modular Inverse:
ll modInverse(ll x){
    ll a, q;
    ExtendedEuclid(x, mod, a, q);
    if(a < 0){
        a = a+(abs(a)+mod-1)/mod*mod;
    }
    return a;
}

/*
================================
SOLUTION CODE:
================================
*/

// Globals Declaration:

// Testcase Sol:
void solve(){
    ll n, m, w;
    cin >> n >> m >> w;
    ll *a = new ll[n*m];
    vl ports;
    for(ll i = 0; i < n*m; i ++){
        cin >> a[i];
        if(a[i] > 0) ports.push_back(i);
    }
    vl dx{0, 1, 0, -1};
    vl dy{1, 0, -1, 0};
    //bfs1:
    queue<ll> q;
    q.push(0);
    ll *df = new ll[n*m];
    df[0] = 0;
    for(ll i = 1; i < n*m; i ++) df[i] = -1;
    while(!q.empty()){
        ll v = q.front(); q.pop();
        ll i = v/m, j = v%m;
        for(ll k = 0; k < 4; k ++){
            ll x = i+dx[k], y = j+dy[k];
            if(x < 0 || y < 0 || x >= n || y >= m) continue;
            if(a[x*m+y] == -1 || df[x*m+y] >= 0) continue;
            df[x*m+y] = df[i*m+j]+1;
            q.push(x*m+y);
        }
    }
    //bfs2:
    q.push(n*m-1);
    ll *db = new ll[n*m];
    db[n*m-1] = 0;
    for(ll i = 0; i < n*m-1; i ++) db[i] = -1;
    while(!q.empty()){
        ll v = q.front(); q.pop();
        ll i = v/m, j = v%m;
        for(ll k = 0; k < 4; k ++){
            ll x = i+dx[k], y = j+dy[k];
            if(x < 0 || y < 0 || x >= n || y >= m) continue;
            if(a[x*m+y] == -1 || db[x*m+y] >= 0) continue;
            db[x*m+y] = db[i*m+j]+1;
            q.push(x*m+y);
        }
    }
    // cout << "\n";
    // for(ll i = 0; i < n; i ++){
    //     for(ll j = 0; j < m; j ++){
    //         cout << df[i*m+j] << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    // for(ll i = 0; i < n; i ++){
    //     for(ll j = 0; j < m; j ++){
    //         cout << db[i*m+j] << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    
    // account for portals:
    long long ans;
    if(df[n*m-1] == -1) ans = -1;
    else ans = ((long long)w)*((long long)df[n*m-1]);
    long long fans = -1, bans = -1;
    for(ll i = 0; i < ports.size(); i ++){
        if(df[ports[i]] == -1) continue;
        if(fans == -1 || fans > (long long)(((long long)w)*((long long)df[ports[i]]))+((long long)a[ports[i]])){
            fans = (long long)(((long long)w)*((long long)df[ports[i]]))+((long long)a[ports[i]]);
        }
    }
    for(ll i = 0; i < ports.size(); i ++){
        if(db[ports[i]] == -1) continue;
        if(bans == -1 || bans > (long long)(((long long)w)*((long long)db[ports[i]]))+((long long)a[ports[i]])){
            bans = (long long)(((long long)w)*((long long)db[ports[i]]))+((long long)a[ports[i]]);
        }
    }
    // cout << ans << " " << fans << " " << bans << "\n";
    if(fans != -1 && bans != -1){
        if(ans == -1 || ans > fans+bans) ans = fans+bans;
    }
    cout << ans << "\n";
    return;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // cout << fixed << setprecision(15);


    // Precomputation:


    // Testcase Computation:
    // ll t; cin >> t; while(t--)
    solve();
    return 0; 
}
### Prompt

Your task is to create a CPP solution to the following problem:

Dima overslept the alarm clock, which was supposed to raise him to school.

Dima wonders if he will have time to come to the first lesson. To do this, he needs to know the minimum time it will take him to get from home to school.

The city where Dima lives is a rectangular field of n × m size. Each cell (i, j) on this field is denoted by one number a_{ij}:

  * The number -1 means that the passage through the cell is prohibited; 
  * The number 0 means that the cell is free and Dima can walk though it. 
  * The number x (1 ≤ x ≤ 10^9) means that the cell contains a portal with a cost of x. A cell with a portal is also considered free. 



From any portal, Dima can go to any other portal, while the time of moving from the portal (i, j) to the portal (x, y) corresponds to the sum of their costs a_{ij} + a_{xy}.

In addition to moving between portals, Dima can also move between unoccupied cells adjacent to one side in time w. In particular, he can enter a cell with a portal and not use it.

Initially, Dima is in the upper-left cell (1, 1), and the school is in the lower right cell (n, m).

Input

The first line contains three integers n, m and w (2 ≤ n, m ≤ 2 ⋅ 10^3, 1 ≤ w ≤ 10^9), where n and m are city size, w is time during which Dima moves between unoccupied cells.

The next n lines each contain m numbers (-1 ≤ a_{ij} ≤ 10^9) — descriptions of cells.

It is guaranteed that the cells (1, 1) and (n, m) are free.

Output

Output the minimum time it will take for Dima to get to school. If he cannot get to school at all, then output "-1".

Example

Input


5 5 1
0 -1 0 1 -1
0 20 0 0 -1
-1 -1 -1 -1 -1
3 0 0 0 0
-1 0 0 0 0


Output


14

Note

Explanation for the first sample: 

<image>

### Response

```cpp
/*
================================
TEMPLATE BY
VAMSI KRISHNA VALLURU
================================
*/

#include<bits/stdc++.h>
#include<sstream>
#include<iomanip>
using namespace std;

typedef int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vb> vvb;

#define forf(i,m,n) for (ll i=(m);i<(n);i++)
#define forb(i,m,n) for (ll i=(m);i>=(n);--i)
#define mk make_pair
#define fst first
#define snd second

// const ll inf = LLONG_MAX;
const ll inf = 1e9;
// const ll inf = 1e18;
// const ll inf = INT_MAX;

const ll mod = 1e9+7;
// const ll mod = 998244353;

/*
================================
DATA STRUCTURES
================================
*/

class minsegtree{
    public:
    ll n, N;
    vl tree;
    minsegtree(ll _n){
        n = _n;
        N = 4*n;
        tree = vl(N, inf);
    }
    void pointUpdate(ll pos, ll l, ll r, ll idx, ll val){
        if(l == r){
        if(idx == l) tree[pos] = val;
        return;
        }
        if(idx < l || r < idx) return;
        pointUpdate(2*pos+1, l, (l+r)/2, idx, val);
        pointUpdate(2*pos+2, (l+r)/2+1, r, idx, val);
        tree[pos] = min(tree[2*pos+1], tree[2*pos+2]);
    }
    void pointUpdate(ll idx, ll val){
        pointUpdate(0, 0, n-1, idx, val);
    }
    ll rangeMin(ll pos, ll l, ll r, ll lo, ll hi){
        if(lo <= l && r <= hi) return tree[pos];
        if(r < lo || hi < l) return inf;
        return min(
        rangeMin(2*pos+1, l, (l+r)/2, lo, hi),
        rangeMin(2*pos+2, (l+r)/2+1, r, lo, hi)
        );
    }
    ll rangeMin(ll lo, ll hi){
        return rangeMin(0, 0, n-1, lo, hi);
    }
};

/*
================================
COMBINATORICS AND NUMBER THEORY:
================================
*/

// Extended Euclid's Algo:
ll ExtendedEuclid(ll a, ll b, ll &x, ll &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    ll g = ExtendedEuclid(b, a%b, x, y);
    ll nx = y, ny = x-a/b*y;
    x = nx; y = ny;
    return g;
}

// Modular Inverse:
ll modInverse(ll x){
    ll a, q;
    ExtendedEuclid(x, mod, a, q);
    if(a < 0){
        a = a+(abs(a)+mod-1)/mod*mod;
    }
    return a;
}

/*
================================
SOLUTION CODE:
================================
*/

// Globals Declaration:

// Testcase Sol:
void solve(){
    ll n, m, w;
    cin >> n >> m >> w;
    ll *a = new ll[n*m];
    vl ports;
    for(ll i = 0; i < n*m; i ++){
        cin >> a[i];
        if(a[i] > 0) ports.push_back(i);
    }
    vl dx{0, 1, 0, -1};
    vl dy{1, 0, -1, 0};
    //bfs1:
    queue<ll> q;
    q.push(0);
    ll *df = new ll[n*m];
    df[0] = 0;
    for(ll i = 1; i < n*m; i ++) df[i] = -1;
    while(!q.empty()){
        ll v = q.front(); q.pop();
        ll i = v/m, j = v%m;
        for(ll k = 0; k < 4; k ++){
            ll x = i+dx[k], y = j+dy[k];
            if(x < 0 || y < 0 || x >= n || y >= m) continue;
            if(a[x*m+y] == -1 || df[x*m+y] >= 0) continue;
            df[x*m+y] = df[i*m+j]+1;
            q.push(x*m+y);
        }
    }
    //bfs2:
    q.push(n*m-1);
    ll *db = new ll[n*m];
    db[n*m-1] = 0;
    for(ll i = 0; i < n*m-1; i ++) db[i] = -1;
    while(!q.empty()){
        ll v = q.front(); q.pop();
        ll i = v/m, j = v%m;
        for(ll k = 0; k < 4; k ++){
            ll x = i+dx[k], y = j+dy[k];
            if(x < 0 || y < 0 || x >= n || y >= m) continue;
            if(a[x*m+y] == -1 || db[x*m+y] >= 0) continue;
            db[x*m+y] = db[i*m+j]+1;
            q.push(x*m+y);
        }
    }
    // cout << "\n";
    // for(ll i = 0; i < n; i ++){
    //     for(ll j = 0; j < m; j ++){
    //         cout << df[i*m+j] << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    // for(ll i = 0; i < n; i ++){
    //     for(ll j = 0; j < m; j ++){
    //         cout << db[i*m+j] << " ";
    //     }
    //     cout << "\n";
    // }
    // cout << "\n";
    
    // account for portals:
    long long ans;
    if(df[n*m-1] == -1) ans = -1;
    else ans = ((long long)w)*((long long)df[n*m-1]);
    long long fans = -1, bans = -1;
    for(ll i = 0; i < ports.size(); i ++){
        if(df[ports[i]] == -1) continue;
        if(fans == -1 || fans > (long long)(((long long)w)*((long long)df[ports[i]]))+((long long)a[ports[i]])){
            fans = (long long)(((long long)w)*((long long)df[ports[i]]))+((long long)a[ports[i]]);
        }
    }
    for(ll i = 0; i < ports.size(); i ++){
        if(db[ports[i]] == -1) continue;
        if(bans == -1 || bans > (long long)(((long long)w)*((long long)db[ports[i]]))+((long long)a[ports[i]])){
            bans = (long long)(((long long)w)*((long long)db[ports[i]]))+((long long)a[ports[i]]);
        }
    }
    // cout << ans << " " << fans << " " << bans << "\n";
    if(fans != -1 && bans != -1){
        if(ans == -1 || ans > fans+bans) ans = fans+bans;
    }
    cout << ans << "\n";
    return;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // cout << fixed << setprecision(15);


    // Precomputation:


    // Testcase Computation:
    // ll t; cin >> t; while(t--)
    solve();
    return 0; 
}
```