#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define mod 998244353
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define ff first
#define ss second
#define prec(y,x) fixed<<setprecision(y)<<x
#define inf 1e18
#define pi 3.1415926535
vector<vector<ll>> v,vis,dis;
set<pair<ll,pair<ll,ll>>> s,temp;
ll dx[]={-1,0,1,0};
ll dy[]={0,1,0,-1};
int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll n,m,w;
	cin>>n>>m>>w;
	v.resize(n+1);
	dis.resize(n+1);
	vis.resize(n+1);
	ll fl=0;
	ll arr[2][3];
	for(ll i=0;i<2;i++)
	{
		for(ll j=0;j<3;j++)
			arr[i][j]=0;
	}

	for(ll i=0;i<=n;i++)
	{
		v[i].resize(m+1);
		dis[i].resize(m+1,-1);
		vis[i].resize(m+1,0);
	}
	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			cin>>v[i][j];
		}
	}
	dis[n][m]=0;
	queue<pair<ll,ll>> q;
	q.push(mk(n,m));
	while(q.size())
	{
		pair<ll,ll> cod=q.front();
		q.pop();
		
		ll x=cod.ff;
		ll y=cod.ss;
		ll val=dis[x][y];
		if(vis[x][y]==1)
			continue;
		vis[x][y]=1;
		if(v[x][y]>0)
		{
			if(fl==0)
			{
				arr[0][0]=val+v[x][y];
				arr[0][1]=x;
				arr[0][2]=y;
				fl++;
			}
			else if(fl==1)
			{
				fl++;
				arr[1][0]=val+v[x][y];
				arr[1][1]=x;
				arr[1][2]=y;
				if(arr[1][0]<arr[0][0])
				{
					swap(arr[1][0],arr[0][0]);
					swap(arr[1][1],arr[0][1]);
					swap(arr[1][2],arr[0][2]);
				}
			}
			else
			{
				ll valu=val+v[x][y];
				if(valu>=arr[1][0])
				{

				}
				else if(valu>=arr[0][0]&&valu<=arr[1][0])
				{
					arr[1][0]=val+v[x][y];
					arr[1][1]=x;
					arr[1][2]=y;
				}
				else
				{
					arr[1][0]=val+v[x][y];
					arr[1][1]=x;
					arr[1][2]=y;
					swap(arr[1][0],arr[0][0]);
					swap(arr[1][1],arr[0][1]);
					swap(arr[1][2],arr[0][2]);
				}
			}
			// s.insert(mk(val+v[x][y],mk(x,y)));
		}
		for(ll i=0;i<4;i++)
		{	
			ll nx=x+dx[i];
			ll ny=y+dy[i];
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
			{
				if(v[nx][ny]==-1)
					continue;
				if(dis[nx][ny]==-1)
				{
					dis[nx][ny]=w+val;
					q.push(mk(nx,ny));
					
				}
				else if(dis[nx][ny]>w+val)
				{
					dis[nx][ny]=w+val;
					q.push(mk(nx,ny));
				}
			}	
		}
	}



	for(ll i=1;i<=n;i++)
	{
		for(ll j=1;j<=m;j++)
		{
			dis[i][j]=-1;
			vis[i][j]=0;
		}
	}
	ll ans=1e17;
	dis[1][1]=0;
	q.push(mk(1,1));
	while(q.size())
	{
		pair<ll,ll> cod=q.front();
		q.pop();
		ll x=cod.ff;
		ll y=cod.ss;
		ll val=dis[x][y];
		if(vis[x][y]==1)
			continue;
		vis[x][y]=1;
		if(v[x][y]>0)
		{
		    	// cout<<x<<" "<<y<<" "<<val<<endl;
		    	
			if(fl==0)
				continue;
			else if(fl==1)
			{
				
				ll qval=arr[0][0];
				ll qx=arr[0][1];
				ll qy=arr[0][2];
				//	cout<<x<<" "<<y<<" "<<val<<endl;
				if(qx!=x||qy!=y)
				{
					ll ans1=v[x][y]+dis[x][y]+qval;
					ans=min(ans,ans1);
				}

			}
			else
			{
				ll qval=arr[0][0];
				ll qx=arr[0][1];
				ll qy=arr[0][2];
				if(qx!=x||qy!=y)
				{
					ll ans1=v[x][y]+dis[x][y]+qval;
					ans=min(ans,ans1);
				}
				else
				{
					ll qval=arr[1][0];
					ll qx=arr[1][1];
					ll qy=arr[1][2];
					ll ans1=v[x][y]+dis[x][y]+qval;
					ans=min(ans,ans1);
				}
			}
		}
		
		for(ll i=0;i<4;i++)
		{	
			ll nx=x+dx[i];
			ll ny=y+dy[i];
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
			{
				if(v[nx][ny]==-1)
					continue;
				if(dis[nx][ny]==-1)
				{
					dis[nx][ny]=w+val;
					q.push(mk(nx,ny));
				}
				else if(dis[nx][ny]>w+val)
				{
					dis[nx][ny]=w+val;
					q.push(mk(nx,ny));
				}
			}	
		}
	}
	if(dis[n][m]!=-1)
	{
		ans=min(ans,dis[n][m]);
	}
	if(ans==1e17)
		cout<<-1<<endl;
	else
		cout<<ans<<endl;
	return 0;
}