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



#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (ll)((x).size())
#define pii               pair<ll,ll>
#define piii              pair<pair<ll,ll>,ll>
#define Piii              pair<ll,pair<ll,ll>>
#define p_queue(a)        priority_queue<a, vector<a>, greater<a>>
#define mp                make_pair
#define se                second
#define fi                first
#define vi(a)             vector<a>
#define vvi(a)            vector <vector <a> >
#define pie               3.14159265358979323846
#define for0(i,a,b)       for(ll i=a;i<b;i++)
#define for1(i,a,b)       for(ll i=a;i<=b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))

//#define nsetbits(x)        __builtin_popcountll(x)



typedef long long ll;

ll inf = 1000000007;
ll mod = 1000000007;
ll INF = 1e18 + 18;


vector<ll> dx={-1LL, 0LL, 1LL, 0LL}, dy={0LL, 1LL, 0LL, -1LL};
//vector<ll> dx={-1, -1, 0, 1, 1, 1, 0, -1}, dy={0, 1, 1, 1, 0, -1, -1, -1};


//string vowels = "aeiou";
//string consonants = "bcdfghjklmnpqrstvwxyz";
//string numbers = "0123456789";
//string alpha = "abcdefghijklmnopqrstuvwxyz";


bool check(ll x , ll y , ll n , ll m){
	if(x >= 1 && x <= n && y >=1 && y <= m){
		return true;
	}
	return false;
}


void test_case(){

	
	ll n,m,w;
	cin>>n>>m>>w;
	vector<vector<ll>> arr(n+1,vector<ll>(m+1));
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			cin>>arr[i][j];
		}
	}
	vector<vector<ll>> min_start(n+1,vector<ll>(m+1,INF));
	vector<vector<bool>> vis(n+1,vector<bool>(m+1,false));
	min_start[1][1] = 0;
	p_queue(Piii) pq;
	pq.push(mp(0,mp(1,1)));
	vis[1][1] = true;
	while(!pq.empty()){
		Piii node = pq.top();
		ll x = node.se.fi;
		ll y = node.se.se;
		ll d = node.fi;
//		cout<<d<<endl;
		pq.pop();
		for(ll i=0;i<4;i++){
			ll x1 = x + dx[i];
			ll y1 = y + dy[i];
			if(check(x1,y1,n,m) && arr[x1][y1] != -1 && !vis[x1][y1] && d+w < min_start[x1][y1]){
				min_start[x1][y1] = d + w;
				vis[x1][y1] = true;
//				cout<<x1<<" "<<y1<<endl;
				pq.push(mp(min_start[x1][y1],mp(x1,y1)));
			}
		}
		
	}
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			vis[i][j] = false;
		}
	}
	vector<vector<ll>> min_end(n+1,vector<ll>(m+1,INF));
	min_end[n][m] = 0;
	p_queue(Piii) p_q;
	p_q.push(mp(0,mp(n,m)));
	vis[n][m] = true;
	while(!p_q.empty()){
		Piii node = p_q.top();
		ll x = node.se.fi;
		ll y = node.se.se;
		ll d = node.fi;
		p_q.pop();
		for(ll i=0;i<4;i++){
			ll x1 = x + dx[i];
			ll y1 = y + dy[i];
			if(check(x1,y1,n,m) && arr[x1][y1] != -1 && !vis[x1][y1] && d+w < min_end[x1][y1]){
				min_end[x1][y1] = d + w;
				vis[x1][y1] = true;
				p_q.push(mp(min_end[x1][y1],mp(x1,y1)));
			}
		}
		
	}
//	set<ll> seta;
	ll ans = min_start[n][m];
	ll val = INF;
	ll cnt = 0;
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			if(arr[i][j] > 0){
//				seta.insert(min_end[i][j] + arr[i][j]);
				val = min(val,min_end[i][j] + arr[i][j]);
				cnt += 1;
			}
		}
	}
	if(cnt < 2){
		if(ans == INF){
			cout<<-1<<endl;
			return ;
		}
		cout<<ans<<endl;
		return ;
	}
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			if(arr[i][j] > 0){
//				ll val = min_end[i][j] + arr[i][j];
//				seta.erase(val);
//				ll mina = *seta.begin();
				ans = min(ans,val + min_start[i][j] + arr[i][j]);
//				seta.insert(val);
			}
		}
	}
//	cout<<"min_start matrix"<<endl;
//	for(ll i=1;i<=n;i++){
//		for(ll j=1;j<=m;j++){
//			cout<<min_start[i][j]<<" ";
//		}
//		cout<<endl;
//	}
//	cout<<"min_end matrix"<<endl;
//	for(ll i=1;i<=n;i++){
//		for(ll j=1;j<=m;j++){
//			cout<<min_end[i][j]<<" ";
//		}
//		cout<<endl;
//	}
	if(ans == INF){
		cout<<-1<<endl;
		return ;
	}
	cout<<ans<<endl;
	
	
	
	
	

}



void intialize(){

	ios::sync_with_stdio(false);
  	cin.tie(0);
  	cout.tie(0);

}

void preprocess(){



}

void codejam(ll t){
	cout<<"Case #"<<t<<": ";
}

int main(){
//	clock_t start,end;
//	start = clock();
   	intialize();
   	preprocess();
   	
//	string a;
//	getline(cin,a);
   	test_case();
//	ll t;
//	cin>>t;
//	for(ll te=1;te<=t;te++){
////		codejam(te);
//		test_case();
//	}
//	end = clock();
//	double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
//    cout << "Time taken by program is : " << fixed << time_taken << setprecision(10);
//    cout << " sec " << endl;


	return 0;
}

/*

to_string() ---> converts number to string;

stoi()   ------> converts string to number;

substr(i,l)  -----> gives substring starting from index 'i' of len 'l';

__builtin_popcountll(x)   -----> gives no.of setbits in x;



*/



