#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
const vector<pair<int, int>> dir = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int n, m, w;
vector<vector<int>> a;
const ll nax = 1e18;

vector<vector<ll>> bfs(int x, int y){
	vector<vector<ll>> ans(n, vector<ll> (m, nax));
	
	queue<pair<int, int>> q;
	ans[x][y] = 0LL;
	q.push({x, y});
	
	while(not q.empty()){
		auto [i, j] = q.front();
		q.pop();
		for(auto [di, dj] : dir){
			int ni = i + di, nj = j + dj;
			if(ni >= 0 and ni < n and nj >= 0 and nj < m and a[ni][nj] != -1 and ans[ni][nj] == nax){
				q.push({ni, nj});
				ans[ni][nj] = ans[i][j] + w;
			}
		}
	}
	
	return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n >> m >> w;
	a = vector<vector<int>> (n, vector<int> (m));
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			cin >> a[i][j];
		}
	}
	
	vector<vector<ll>> start = bfs(0, 0);
	vector<vector<ll>> end = bfs(n-1, m-1);
	
	ll ans = end[0][0], min_start = nax, min_end = nax;
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(a[i][j] > 0){
				min_start = min(min_start, start[i][j] + a[i][j]);
				min_end = min(min_end, end[i][j] + a[i][j]);
			}
		}
	}
	
	ans = min(ans, min_start + min_end);
	if(ans >= nax) ans = -1;
	
	cout << ans << '\n';
}
