#include<bits/stdc++.h> 
using namespace std;
namespace Ruri{
    #define ms(a,b) memset(a,b,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back 
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second                         
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=1e9;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
    #define ss(a) scanf("%s",a)
    inline int ri(){ int x; scanf("%d",&x); return x; }
    inline ll rl(){ ll x;  scanf("%lld",&x); return x; }
    inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
    #define pr(x) cout<<#x<<": "<<(x)<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");   
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
typedef pair<int,int> P;
const int MAX_N=2005;
int mov[4][2]={1,0,-1,0,0,1,0,-1};
int n,m;
ll w;
ll g[MAX_N][MAX_N];
ll dis1[MAX_N][MAX_N],dis2[MAX_N][MAX_N];
int main()
{
	n=ri(),m=ri(),w=rl();
	repi(i,1,n)repi(j,1,m) g[i][j]=rl(),dis1[i][j]=dis2[i][j]=infl;
	dis1[1][1]=0;
	queue<P> q;
	q.push(P(1,1));
	ll mnst=infl;
	while(!q.empty())
	{
		P now=q.front(); q.pop();
		int x=now.fi,y=now.se;
		if(g[x][y]>0) gmn(mnst,g[x][y]+dis1[x][y]);
		repi(i,0,3){
			int tx=x+mov[i][0],ty=y+mov[i][1];
			ll cost=dis1[x][y]+w;
			if(tx<1||tx>n||ty<1||ty>m||g[tx][ty]==-1) continue;
			if(cost>=dis1[tx][ty]) continue;
			dis1[tx][ty]=cost,q.push(P(tx,ty));
		}
	}
	ll mned=infl;
	dis2[n][m]=0,q.push(P(n,m));
	while(!q.empty())
	{
		P now=q.front(); q.pop();
		
		int x=now.fi,y=now.se;
		if(g[x][y]>0) gmn(mned,g[x][y]+dis2[x][y]);
		repi(i,0,3){
			int tx=x+mov[i][0],ty=y+mov[i][1];
			ll cost=dis2[x][y]+w;
			if(tx<1||tx>n||ty<1||ty>m||g[tx][ty]==-1) continue;
			if(cost>=dis2[tx][ty]) continue;
			dis2[tx][ty]=cost,q.push(P(tx,ty));
		}
	}
	if(dis1[n][m]==infl&&(mnst==infl||mned==infl)) return puts("-1"),0;
	printf("%lld\n",min(dis1[n][m],mnst+mned));
    return 0;
}