#include<bits/stdc++.h>
#define ll long long
using namespace std;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll int n,m,we;
	cin>>n>>m>>we;
	ll int a[n][m];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a[i][j];
		}
	}
	queue<pair<ll int,ll int>>pq;
	vector<vector<ll int>>dp1(n,vector<ll int>(m,1e9));
	vector<vector<ll int>>dp2(n,vector<ll int>(m,1e9));
	dp1[0][0]=0;
	pq.push({0,0});
	while(!pq.empty())
	{
		ll int x,y,w;
		tie(x,y)=pq.front();
		pq.pop();
		w=dp1[x][y];
		for(int k=0;k<4;k++)
		{
			ll int xx=x+dx[k];
			ll int yy=y+dy[k];
			if(xx<n&&yy<m&&xx>=0&&yy>=0&&a[xx][yy]!=-1&&dp1[xx][yy]>w+1)
			{
				dp1[xx][yy]=w+1;
				pq.push({xx,yy});
			}
		}
	}
	dp2[n-1][m-1]=0;
	pq.push({n-1,m-1});
	while(!pq.empty())
	{
		ll int x,y,w;
		tie(x,y)=pq.front();
		pq.pop();
		w=dp2[x][y];
		for(int k=0;k<4;k++)
		{
			ll int xx=x+dx[k];
			ll int yy=y+dy[k];
			if(xx<n&&yy<m&&xx>=0&&yy>=0&&a[xx][yy]!=-1&&dp2[xx][yy]>w+1)
			{
				dp2[xx][yy]=w+1;
				pq.push({xx,yy});
			}
		}
	}
	long long ans=1e18,ans1=1e18,ans2=1e18;
	if(dp1[n-1][m-1]!=1e9)ans=dp1[n-1][m-1]*we;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(a[i][j]>0&&dp1[i][j]!=1e9)
			{
				ans1=min(ans1,a[i][j]+dp1[i][j]*we);
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(a[i][j]>0&&dp2[i][j]!=1e9)
			{
				ans2=min(ans2,a[i][j]+dp2[i][j]*we);
			}
		}
	}
	ans=min(ans,ans1+ans2);
	if(ans>1e16)ans=-1;
	cout<<ans<<endl;
}