#include<bits/stdc++.h>
#define ll long long int 
#define mod 1000000007
#define se second
#define fi first
#define endl "\n"
#define pi 3.1415926535897932384626433832795028841971
#define MAX 9223372036854775807
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
//	cout<<fixed<<setprecision(10);
	ll n,m,w;cin>>n>>m>>w;
	ll ans1=MAX,ans=MAX;
	ll arr[n+1][m+1];
	for(int a=1;a<=n;a++)
	{
		for(int b=1;b<=m;b++)
		{
			cin>>arr[a][b];
		}
	}
	ll dist1[n+1][m+1];
	bool vis1[n+1][m+1];
	for(int a=1;a<=n;a++)
	{
		for(int b=1;b<=m;b++)
		vis1[a][b]=0;
	}
	dist1[1][1]=0;
	vis1[1][1]=1;
	if(arr[1][1]>0)
	ans1=min(ans1,arr[1][1]);
	queue<pair<ll,ll> > q;
	q.push({1,1});
	while(!q.empty())
	{
		ll x,y;
		x=q.front().fi;
		y=q.front().se;
		q.pop();
		ll xx[4]={1,0,-1,0};
		ll yy[4]={0,1,0,-1};
		for(int a=0;a<4;a++)
		{
			ll x1,y1;
			x1=x+xx[a];
			y1=y+yy[a];
			if(x1>=1&&x1<=n&&y1>=1&&y1<=m&&arr[x1][y1]!=-1&&vis1[x1][y1]==0)
			{
				dist1[x1][y1]=dist1[x][y]+w;
				vis1[x1][y1]=1;
				q.push({x1,y1});
				if(arr[x1][y1]>0)
				ans1=min(ans1,dist1[x1][y1]+arr[x1][y1]);
			}
		}
	}
	if(vis1[n][m]==1)
	ans=dist1[n][m];
	else
	ans=-1;
	queue<pair<ll,ll> > q1;
	bool vis2[n+1][m+1];
	for(int a=1;a<=n;a++)
	{
		for(int b=1;b<=m;b++)
		vis2[a][b]=0;
	}
	ll dist2[n+1][m+1];
	dist2[n][m]=0;
	ll ans2=MAX;
	if(arr[n][m]>0)
	ans2=arr[n][m];
	q1.push({n,m});
	while(!q1.empty())
	{
		ll x,y;
		x=q1.front().fi;
		y=q1.front().se;
		q1.pop();
		ll xx[4]={1,0,-1,0};
		ll yy[4]={0,1,0,-1};
		for(int a=0;a<4;a++)
		{
			ll x1,y1;
			x1=x+xx[a];
			y1=y+yy[a];
			if(x1>=1&&x1<=n&&y1>=1&&y1<=m&&arr[x1][y1]!=-1&&vis2[x1][y1]==0)
			{
				dist2[x1][y1]=dist2[x][y]+w;
				vis2[x1][y1]=1;
				q1.push({x1,y1});
				if(arr[x1][y1]>0)
				ans2=min(ans2,dist2[x1][y1]+arr[x1][y1]);
			}
		}
	}
	if(ans1!=MAX&&ans2!=MAX)
	{
		if(ans==-1)
		ans=ans1+ans2;
		else
		ans=min(ans,ans1+ans2);
	}
	cout<<ans<<endl;
	return 0;
}
