#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

const int kMaxN = 2e3 + 1;
const int kD[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const long long kInf = 1LL << 60;

struct P {
  int x, y;
};
queue<P> q;
int a[kMaxN][kMaxN], d[kMaxN][kMaxN][2];
long long v[2] = {kInf, kInf}, w, ans;
int n, m;

void R(int x, int y, int o, int l) {
  if (min(x, y) <= 0 || x > n || y > m || d[x][y][o] || a[x][y] == -1) {
    return;
  }
  d[x][y][o] = l;
  q.push({x, y});
  if (a[x][y] > 0) {
    v[o] = min(v[o], a[x][y] + (l - 1) * w);
  }
}

void S(int sx, int sy, int o) {
  for (R(sx, sy, o, 1); !q.empty(); q.pop()) {
    P p = q.front();
    for (int i = 0; i <= 3; i++) {
      R(p.x + kD[i][0], p.y + kD[i][1], o, d[p.x][p.y][o] + 1);
    }
  }
}

int main() {
  scanf("%d %d %lld", &n, &m, &w);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  S(1, 1, 0);
  S(n, m, 1);
  ans = min(kInf, v[0] + v[1]);
  if (d[n][m][0]) {
    ans = min(ans, (d[n][m][0] - 1) * w);
  }
  printf("%lld", ans == kInf ? -1 : ans);
  return 0;
}