#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
inline int read() {int x=0,f=1,ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1; ch=getchar();}while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-'0'); ch=getchar();}return f==1?x:-x;}
inline void print(int a[],int n,int offset=0) {for(int i=0; i<n; i++)printf("%d ",a[i+offset]);printf("\n");}

const int maxn=2e3+10;
const ll inf=1e17;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
int n, m, w, head, tail;
int a[maxn][maxn],que[maxn*maxn][2];
ll dis[maxn][maxn];
inline void init() {
    n=read(); m=read(); w=read();
    for (int i=0; i<=n+1; i++) for (int j=0; j<=m+1; j++) a[i][j]=-1;
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) a[i][j]=read();
}
inline void doit() {
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) dis[i][j]=inf;
    head=tail=0;
    que[tail][0]=1; que[tail++][1]=1;
    dis[1][1]=0;
    while (head<tail){
        int x=que[head][0],y=que[head++][1];
        for (int i=0; i<4; i++){
            int nx=x+dx[i],ny=y+dy[i];
            if (a[nx][ny]^-1 && dis[nx][ny]>=inf) {
                dis[nx][ny]=dis[x][y]+w;
                que[tail][0]=nx; que[tail++][1]=ny;
            }
        }
    }
    ll st_cost=inf;
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) if (a[i][j]>0&&dis[i][j]<inf) st_cost=min(st_cost,dis[i][j]+a[i][j]);
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) dis[i][j]=inf;
    head=tail=0;
    que[tail][0]=n; que[tail++][1]=m;
    dis[n][m]=0;
    while (head<tail){
        int x=que[head][0],y=que[head++][1];
        for (int i=0; i<4; i++){
            int nx=x+dx[i],ny=y+dy[i];
            if (a[nx][ny]^-1 && dis[nx][ny]>=inf) {
                dis[nx][ny]=dis[x][y]+w;
                que[tail][0]=nx; que[tail++][1]=ny;
            }
        }
    }
    ll ans=min(inf,dis[1][1]);
    for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) if (a[i][j]>0&&dis[i][j]<inf) ans=min(ans,st_cost+dis[i][j]+a[i][j]);
    if (ans>=inf) printf("-1\n");
    else printf("%lld\n", ans);
}
int main() {
    //freopen("test.in", "r", stdin);
    init();
    doit();
    return 0;
}
