//100gods

#include <bits/stdc++.h>
#define int long long
#define vec2 vector<vector<int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FRE freopen("elegant.in","r",stdin);freopen("elegant.out","w",stdout)
#define pii pair <int,int>
#define vii vector <int>
#define vpii vector <pii>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define minii LLONG_MAX
#define all(x) x.begin(), x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define double long double
#define eb emplace_back
const double pi = 3.1415926535898;
const int mod=1e9+7;
//const int mod=998244353;

using namespace std;

int n,m,w,x,y,cnt;
int a[2005][2005];
int dp[2005][2005],dp2[2005][2005];
int mini=1e18;
int vis[2005][2005];
int ans;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};


int32_t main()
{
    fio;
    cin>>n>>m>>w;
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            cin>>a[i][j];
            dp[i][j]=1e18;
        }
    }
    dp[0][0]=0;
    queue <pii> q;
    q.push({0,0});
    while (q.size())
    {
        x=q.front().ff;
        y=q.front().ss;
        q.pop();
        if (a[x][y]>0)
        {
            mini=min(mini,a[x][y]+dp[x][y]);
        }
        if (vis[x][y])
        {
            continue;
        }
        vis[x][y]=1;
        for (int i=0;i<4;i++)
        {
            if (x+dx[i]<0||x+dx[i]>=n||y+dy[i]<0||y+dy[i]>=m||a[x+dx[i]][y+dy[i]]==-1)
            {
                continue;
            }
            dp[x+dx[i]][y+dy[i]]=min(dp[x+dx[i]][y+dy[i]],w+dp[x][y]);
            q.push({x+dx[i],y+dy[i]});
        }
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            if (a[i][j]>0)
            {
                dp[i][j]=min(dp[i][j],mini+a[i][j]);
            }
        }
    }
    ans=dp[n-1][m-1];
    mem(vis,0);
    queue <pair<pii,int>> qq;
    qq.push({{n-1,m-1},0});
    while (qq.size())
    {
        x=qq.front().ff.ff;
        y=qq.front().ff.ss;
        cnt=qq.front().ss;
        ans=min(ans,dp[x][y]+cnt*w);
        qq.pop();
        if (vis[x][y])
        {
            continue;
        }
        vis[x][y]=1;
        for (int i=0;i<4;i++)
        {
            if (x+dx[i]<0||x+dx[i]>=n||y+dy[i]<0||y+dy[i]>=m||a[x+dx[i]][y+dy[i]]==-1)
            {
                continue;
            }
            qq.push({{x+dx[i],y+dy[i]},cnt+1});
        }
    }
    if (ans==1e18)
    {
        ans=-1;
    }
    cout<<ans;
    return 0;
}
