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

#define ll long long

const ll INF = 4e18;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    ll w;
    cin >> n >> m >> w;

    vector<vector<int>> a(n, vector<int>(m));

    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> a[i][j];

    int dx[] = {1, -1, 0, 0};
    int dy[] = {0, 0, 1, -1};

    ll ans = INF, bst1 = INF, bst2 = INF;

    // BFS from start
    {
        vector<vector<ll>> d(n, vector<ll>(m, INF));

        if (a[0][0] != -1) {
            queue<pair<int, int>> q;
            q.push({0, 0});
            d[0][0] = 0;

            while (!q.empty()) {
                auto [x, y] = q.front();
                q.pop();

                if (a[x][y] > 0)
                    bst1 = min(bst1, d[x][y] + (ll)a[x][y]);

                for (int k = 0; k < 4; k++) {
                    int nx = x + dx[k];
                    int ny = y + dy[k];

                    if (nx < 0 || ny < 0 || nx >= n || ny >= m)
                        continue;
                    if (a[nx][ny] == -1)
                        continue;
                    if (d[nx][ny] != INF)
                        continue;

                    d[nx][ny] = d[x][y] + w;
                    q.push({nx, ny});
                }
            }

            ans = d[n - 1][m - 1];
        }
    }

    // BFS from end
    {
        vector<vector<ll>> d(n, vector<ll>(m, INF));

        if (a[n - 1][m - 1] != -1) {
            queue<pair<int, int>> q;
            q.push({n - 1, m - 1});
            d[n - 1][m - 1] = 0;

            while (!q.empty()) {
                auto [x, y] = q.front();
                q.pop();

                if (a[x][y] > 0)
                    bst2 = min(bst2, d[x][y] + (ll)a[x][y]);

                for (int k = 0; k < 4; k++) {
                    int nx = x + dx[k];
                    int ny = y + dy[k];

                    if (nx < 0 || ny < 0 || nx >= n || ny >= m)
                        continue;
                    if (a[nx][ny] == -1)
                        continue;
                    if (d[nx][ny] != INF)
                        continue;

                    d[nx][ny] = d[x][y] + w;
                    q.push({nx, ny});
                }
            }
        }
    }

    if (bst1 != INF && bst2 != INF)
        ans = min(ans, bst1 + bst2);

    cout << (ans == INF ? -1 : ans);

    return 0;
}