//int max = 2 147 483 647 (2^31-1)
//ll max = 9 223 372 036 854 775 807 (2^63-1)
#include<bits/stdc++.h>
using namespace std;

#define forn(i,n) for(int i=0;i<n;i++)
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define MOD 1000000007

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pi;

//Fast input and output
void fast_io(){
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);}

//Printing pairs and vectors
template<typename A, typename B> ostream& operator<< (ostream &cout, pair<A,B> const &p) {return cout << "(" << p.f << ", " << p.s << ")";}
template<typename A> ostream& operator<< (ostream &cout, vector<A> const&v){
cout << "["; forn(i,(int)v.size()){ if (i) cout << ", "; cout << v[i];} return cout << "]";}

int n,m;
vector<vl>a;

vector<pi>adj(pi x){
  vector<pi>ans;
  if(x.f>0 and a[x.f-1][x.s]!=-1) ans.pb(mp(x.f-1,x.s));
  if(x.s>0 and a[x.f][x.s-1]!=-1) ans.pb(mp(x.f,x.s-1));
  if(x.f<n-1 and a[x.f+1][x.s]!=-1) ans.pb(mp(x.f+1,x.s));
  if(x.s<m-1 and a[x.f][x.s+1]!=-1) ans.pb(mp(x.f,x.s+1));
  return ans;
}

int main(){
  ll inf=(ll)MOD*MOD;
  fast_io();
  ll w; cin >> n >> m >> w;
  a=vector<vl>(n,vl(m));
  forn(i,n) forn(j,m) cin >> a[i][j];
  // bfs from home
  vector<vl>disth(n,vl(m,inf));
  vector<pi> visit(1,mp(0,0)), next;
  disth[0][0]=0; ll curr=w;
  while(visit.size()>0){
    for(auto i:visit){
      for(auto j:adj(i)){
        if (disth[j.f][j.s]==inf){
          disth[j.f][j.s]=curr;
          next.pb(j);
        }
      }
    }
    visit=next; next.clear(); curr+=w;
  }
  ll lowh=inf;
  forn(i,n){
    forn(j,m){
      if(a[i][j]>0) lowh=min(lowh,disth[i][j]+a[i][j]);
    }
  }
  //bfs from school
  vector<vl>dists(n,vl(m,inf));
  visit=vector<pi>(1,mp(n-1,m-1)); next.clear();
  dists[n-1][m-1]=0; curr=w;
  while(visit.size()>0){
    for(auto i:visit){
      for(auto j:adj(i)){
        if (dists[j.f][j.s]==inf){
          dists[j.f][j.s]=curr;
          next.pb(j);
        }
      }
    }
    visit=next; next.clear(); curr+=w;
  }
  ll lows=inf;
  forn(i,n){
    forn(j,m){
      if(a[i][j]>0) lows=min(lows,dists[i][j]+a[i][j]);
    }
  };
  if(lows==inf or lowh==inf){
    if(disth[n-1][m-1]==inf) cout << -1;
    else cout << disth[n-1][m-1];
  }
  else cout << min(disth[n-1][m-1],lowh+lows);
}
//code by DanTheMan
