// annotshy      #include <bits/stdc++.h>   #define Task  CF    #define pb push_back   #define MOD 1000000007   #define F first   #define S second   #define int long long      using namespace std;      const int maxn = 2005;      int n, m, w;   int a[maxn][maxn];   int dx[5] = {1, 0, -1, 0};   int dy[5] = {0, 1, 0, -1};   long long dp[maxn][maxn], to1 = 0, to2 = 0;      long long BFS(int i, int j) {     queue<pair<int, int> > Q;     Q.push({i, j});     for(int p = 1; p <= n; ++ p) {       for(int q = 1; q <= m; ++ q) dp[p][q] = 1e18;     }     dp[i][j] = 0;     while(Q.size()) {       int u = Q.front().F;       int v = Q.front().S;       Q.pop();       for(int k = 0; k < 4; ++ k) {         int x = u + dx[k];         int y = v + dy[k];         if(x < 1 || y < 1 || x > n || y > m || a[x][y] == -1) continue;         if(dp[x][y] > dp[u][v] + w) {           dp[x][y] = dp[u][v] + w;           Q.push({x, y});         }       }     }     long long res = 1e18;     if(i == 1 && j == 1) to1 = dp[n][m];     if(i == n && j == m) to2 = dp[1][1];     for(int i = 1; i <= n; ++ i) {       for(int j = 1; j <= m; ++ j) if(a[i][j] > 0) {         res = min(res, dp[i][j] + a[i][j]);       }     }     return res;   }      void Solve() {     cin >> n >> m >> w;     for(int i = 1; i <= n; ++ i) {       for(int j = 1; j <= m; ++ j) cin >> a[i][j];     }     long long res1 = BFS(1, 1);     long long res2 = BFS(n, m);     if(res1 + res2 >= 1e18 && to1 == 1e18 && to2 == 1e18) cout << - 1 <<   n ;     else cout << min({to1, to2, res1 + res2}) <<   n ;   }      signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0), cout.tie(0);     if(fopen(Task .inp ,  r )) {       freopen(Task .inp ,  r , stdin);       freopen(Task .out ,  w , stdout);     }     int test = 1;   //  cin >> test;     while(test --) {       Solve();     }   }   