#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t = 1,n,m,w,k,q,l,r,wtf,a[2009][2009],v[2009][2009],a1 = 1e18,a2 = 1e18,b,c,u,x,y,z,mod = 1e9 + 7;
string s;
ll dx[4] = {1,0,0,-1},dy[4] = {0,-1,1,0};
struct st{ll x,y,z; /*bool operator < (const st &a) const{return x < a.x;};*/};
bool cmp(const st &a,const st &b){return a.x < b.x;}
inline ll read(){
	ll s = 0,w = 1;
	char ch = getchar();
	while (ch > '9' || ch < '0'){ if (ch == '-') w = -1; ch = getchar();}
	while (ch <= '9' && ch >= '0') s = (s << 1) + (s << 3) + (ch ^ 48),ch = getchar();
	return s * w;
}
ll qp(ll x,ll y){
	ll a = 1,b = x;
	while (y){
		if (y & 1) a = a * b % mod;
		b = b * b % mod,y >>= 1;
	}
	return a;
}
int main(){//t = read();
while (t --){
	n = read(),m = read(),w = read();
	for (ll i = 1;i <= n;i += 1) for (ll j = 1;j <= m;j += 1) a[i][j] = read();
	queue <st> q;
	q.push({1,1,0}),v[1][1] = 1; ll fl = 0;
	while (!q.empty()){
		ll x = q.front().x,y = q.front().y,z = q.front().z; q.pop();
		if (a[x][y] > 0) a1 = min(a1,a[x][y] + w * z);
		if (x == n && y == m) fl = 1,wtf = w * z;
		for (ll i = 0;i < 4;i += 1){
			ll nx = x + dx[i],ny = y + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == -1 || v[nx][ny]) continue;
			v[nx][ny] = 1,q.push({nx,ny,z + 1});
		}
	}
	while (!q.empty()) q.pop();
	fill(v[0],v[0] + 2009 * 2009,0);
	q.push({n,m,0}),v[n][m] = 1;
	while (!q.empty()){
		ll x = q.front().x,y = q.front().y,z = q.front().z; q.pop();
		if (a[x][y] > 0) a2 = min(a2,a[x][y] + w * z);
		for (ll i = 0;i < 4;i += 1){
			ll nx = x + dx[i],ny = y + dy[i];
			if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == -1 || v[nx][ny]) continue;
			v[nx][ny] = 1,q.push({nx,ny,z + 1});
		}
	}
	if (fl == 1) cout<<min(wtf,a1 + a2);
	else if (a1 < 1e18 && a2 < 1e18) cout<<a1 + a2;
	else cout<<-1;
}
	return 0;
}