#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> P;
ll gcd(ll a, ll b){return b? gcd(b, a % b): a;}
ll quickpow(ll a, ll b){ll res = 1; while(b){if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;}
// head

const int N = 2e3 + 5, inf = 1e18;
int n, m, w, mat[N][N];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

void bfs(int x0, int y0, vector<vector<int>> &d) {
	d.resize(n + 1, vector<int> (m + 1, inf));
	queue<pair<int, int>> quu;
	quu.push(make_pair(x0, y0));
	d[x0][y0] = 0;
	while (!quu.empty()) {
		int px = quu.front().first, py = quu.front().second;
		quu.pop();
		for (int i = 0; i < 4; i++) {
			int nx = px + dx[i], ny = py + dy[i];
			if (1 <= nx && nx <= n && 1 <= ny && ny <= m && mat[nx][ny] != -1 && d[nx][ny] == inf) {
				d[nx][ny] = d[px][py] + w;
				quu.push(make_pair(nx, ny));
			}
		}
	}
}

void show(vector<vector<int>> &vt) {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cout << vt[i][j] << " ";
		}
		cout << endl;
	}
}

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m >> w;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mat[i][j];
		}
	}
	vector<vector<int>> d1, d2;
	bfs(1, 1, d1);
	bfs(n, m, d2);
	int ans = d1[n][m];
	int mn1 = inf;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (mat[i][j] <= 0) continue;
			mn1 = min(mn1, d1[i][j] + mat[i][j]);
		}
	}
	int mn2 = inf;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (mat[i][j] <= 0) continue;
			mn2 = min(mn2, d2[i][j] + mat[i][j]);
		}
	}
	ans = min(ans, mn1 + mn2);
	if (ans == inf) {
		cout << -1 << endl;
	}
	else {
		cout << ans << endl;
	}
	return 0;
}
