#include<bits/stdc++.h>
using namespace std;
 
#define GO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define endl "\n"
const int maxr = 2e3 + 5;
const int INF = 1e18;

int n, m, w;
int arr[maxr][maxr];
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {-1, 1, 0, 0};

void bfs(int x, int y, vector<vector<int>> &dt) {

    vector<vector<bool>> vis(n, vector<bool>(m, false));
    queue<pair<int, int>> q;
    q.push({x, y});
    vis[x][y] = true;
    dt[x][y] = 0;

    while(!q.empty()) {
        auto t = q.front(); q.pop();

        for(int k = 0; k < 4; k++) {
            int ni = t.first + dx[k];
            int nj = t.second + dy[k];

            if(ni < 0 || nj < 0 || ni >= n || nj >= m || vis[ni][nj] || arr[ni][nj] < 0)
                continue;

            q.push({ni, nj});
            vis[ni][nj] = true;
            dt[ni][nj] = min(dt[ni][nj], dt[t.first][t.second] + w);
        }
    }
}

int32_t main()
{
    GO;
    cin >> n >> m >> w;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> arr[i][j];

    vector<vector<int>> dt(n, vector<int>(m, INF));
    bfs(0, 0, dt);
    vector<vector<int>> dd(n, vector<int>(m, INF));
    bfs(n - 1, m - 1, dd);

    int ans = dd[0][0]; 
    int mini1 = INF, mini2 = INF;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(arr[i][j] > 0) {
                mini1 = min(mini1, dt[i][j] + arr[i][j]);
                mini2 = min(mini2, dd[i][j] + arr[i][j]);
            }
        }
    }
    ans = min(ans, mini1 + mini2);
    
    cout << ((ans >= INF) ? (-1) : (ans)) << endl;
    return 0;
}