#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ar array
#define db double
const db pi = acos(-1);
const int mxn = 2e3 + 7, INF = 1e18;
int dr[] = {0, 0, -1, 1};
int dc[] = {1, -1, 0, 0};
int n, m, w;
int a[mxn][mxn];
int vis[mxn][mxn];
int dis[mxn][mxn];
int ans = INF;
bool check(int u, int v)
{
    return u >= 1 && u <= m && v >= 1 && v <= n && !vis[u][v] && a[u][v] != -1;
}
int bfs(int U, int V)
{
    queue<ar<int, 2> > q;
    q.push({U, V});
    for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) 
    {
        dis[i][j] = INF;
        vis[i][j] = 0;
    }
    vis[U][V] = 1;
    dis[U][V] = 0;
    while(!q.empty())
    {
        int u = q.front()[0], v = q.front()[1]; q.pop();
        for (int i = 0; i < 4; i++)
        {
            int nu = u + dr[i];
            int nv = v + dc[i];
            if (check(nu, nv))
            {
                dis[nu][nv] = dis[u][v] + w;
                vis[nu][nv] = 1;
                q.push({nu, nv});
            }
        }
    }
    int res = INF;
    for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) 
    if (a[i][j] > 0) res = min(res, dis[i][j] + a[i][j]);
    return res;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> m >> n >> w;
    for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) cin >> a[i][j];
    int d1 = bfs(1, 1);
    ans = min(ans, dis[m][n]);
    int d2 = bfs(m, n);
    ans = min(ans, d1 + d2);
    if (ans != INF) cout << ans;
    else cout << -1;
    return 0;
}