#include<bits/stdc++.h>
using namespace std;
vector<vector<long long>> a;
vector<vector<vector<long long>>> dis;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	/*#ifndef ONLINE_JUDGE
		//for getting input from input.txt
		freopen("input.txt", "r", stdin);
		//for writing output to output.txt
		freopen("output.txt", "w", stdout);
	#endif */
	int n, m, w;
	cin >> n >> m >> w;
	a.clear();
	a.resize(n, vector<long long>(m));
	dis.clear();
	dis.resize(n, vector<vector<long long>>(m, vector<long long>(2)));
	int num;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> num;
			a[i][j] = num;
			dis[i][j][0] = dis[i][j][1] = -1;
		}
	}
	queue<pair<int, int>> q;
	q.push({0, 0});
	dis[0][0][0] = 0;
	while (!q.empty()) {
		pair<int, int> tmp = q.front();
		q.pop();
		int x = tmp.first;
		int y = tmp.second;
		for (int k = 0; k < 4; k++) {
			int tmpx = x + dx[k];
			int tmpy = y + dy[k];
			if (tmpx >= 0 && tmpx < n && tmpy >= 0 && tmpy < m && a[tmpx][tmpy] != -1 && dis[tmpx][tmpy][0] == -1) {
				q.push({tmpx, tmpy});
				dis[tmpx][tmpy][0] = dis[x][y][0] + 1;
			}
		}
	}
	q.push({n - 1, m - 1});
	dis[n - 1][m - 1][1] = 0;
	while (!q.empty()) {
		pair<int, int> tmp = q.front();
		q.pop();
		int x = tmp.first;
		int y = tmp.second;
		for (int k = 0; k < 4; k++) {
			int tmpx = x + dx[k];
			int tmpy = y + dy[k];
			if (tmpx >= 0 && tmpx < n && tmpy >= 0 && tmpy < m && a[tmpx][tmpy] != -1 && dis[tmpx][tmpy][1] == -1) {
				q.push({tmpx, tmpy});
				dis[tmpx][tmpy][1] = dis[x][y][1] + 1;
			}
		}
	}
	long long ans = 1e18;
	long long mn1 = 1e18, mn2 = 1e18;
	if (dis[n - 1][m - 1][0] != -1) {
		ans = dis[n - 1][m - 1][0] * w;
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] > 0 && dis[i][j][0] != -1) {
				mn1 = min(mn1, a[i][j] + dis[i][j][0] * w );
			}
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] > 0 && dis[i][j][1] != -1) {
				mn2 = min(mn2, a[i][j] + dis[i][j][1] * w );
			}
		}
	}
	ans = min(ans, mn1 + mn2);
	if (ans == 1e18)
		ans = -1;
	cout << ans << endl;
}