#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int K = 4;
const int N = 2020;
const ll INF = 1e18;

int X[K] = {-1, +1, 0, 0};
int Y[K] = {0, 0, -1, +1};

int n, m, w;
int a[N][N];
ll dist[2][N][N];

void minimize(ll& x, ll y) {
    if (x > y) {
        x = y;
    }
}

bool can(int i, int j) {
    return 0 <= i && i < n && 0 <= j && j < m && a[i][j] >= 0;
}

void bfs(int k, int i, int j) {
    queue<pair<int, int>> q;
    q.emplace(i, j);
    dist[k][i][j] = 0;

    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        for (int r = 0; r < K; r++) {
            if (can(x + X[r], y + Y[r])) {
                if (dist[k][x + X[r]][y + Y[r]] == -1) {
                    q.emplace(x + X[r], y + Y[r]);
                    dist[k][x + X[r]][y + Y[r]] = dist[k][x][y] + w;
                }
            }
        }
    }
}

int main() {
    scanf("%d%d%d", &n, &m, &w);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    memset(dist, -1, sizeof(dist));
    bfs(0, 0, 0);
    bfs(1, n - 1, m - 1);

    ll minimum[2] = {INF, INF};
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int k = 0; k < 2; k++) {
                if (dist[k][i][j] != -1 && a[i][j] > 0) {
                    minimize(minimum[k], dist[k][i][j] + a[i][j]);
                }
            }
        }
    }

    ll answer = INF;
    minimize(answer, minimum[0] + minimum[1]);
    if (dist[0][n - 1][m - 1] != -1) {
        minimize(answer, dist[0][n - 1][m - 1]);
    }

    if (answer == INF) {
        answer = -1;
    }
    cout << answer << endl;
}