#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

void solve()
{
	int n, m, w;
	scanf("%d%d%d", &n, &m, &w);
	vector<pair<int,int>> portals;
	vector<vector<int>> a(n, vector<int>(m));
	vector<vector<long long>> D(n+1, vector<long long>(m, 1LL<<62));
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < m; ++j)
		{
			scanf("%d", &a[i][j]);
			if (a[i][j] > 0)
				portals.push_back(pair<int,int>(i, j));
		}
	priority_queue<pair<long long,pair<int,int>>> q;
	q.push(pair<long long,pair<int,int>>(0, pair<int,int>(0,0)));
	D[0][0] = 0;
	while(q.size())
	{
		pair<long long, pair<int,int>> now = q.top();
		q.pop();
		int x = now.second.first;
		int y = now.second.second;
		//printf("%d %d\n", v/m, v%m);
		long long d = -now.first;
		if (D[x][y] != d)
			continue;
		if (x != n)
		{
			const int dx[] = {1,-1,0,0};
			const int dy[] = {0,0,1,-1};
			for (int i = 0; i < 4; ++i)
			{
				int xx = x + dx[i];
				int yy = y + dy[i];
				if (xx >= 0 && xx < n
				&& yy >= 0 && yy < m
				&& a[xx][yy] >= 0)
				{
					long long dd = d + w;
					if (D[xx][yy] > dd)
					{
						D[xx][yy] = dd;
						q.push(pair<long long, pair<int,int>>(-dd, pair<int,int>(xx, yy)));
					}
				}
			}
			if (a[x][y] > 0)
			{
				long long dd = d + a[x][y];
				if (D[n][0] > dd)
				{
					D[n][0] = dd;
					q.push(pair<long long, pair<int,int>>(-dd, pair<int,int>(n,0)));
				}
			}
		}
		else
		{
			for (int j = 0; j < portals.size(); ++j)
			{
				int xx = portals[j].first;
				int yy = portals[j].second;
				long long dd = d + a[xx][yy];
				if (D[xx][yy] > dd)
				{
					D[xx][yy] = dd;
					q.push(pair<long long, pair<int,int>>(-dd, portals[j]));
				}
			}
		}
	}
	if (D[n-1][m-1] == 1LL<<62)
		printf("-1\n");
	else
		printf("%I64d\n", D[n-1][m-1]);
}

int main()
{
	solve();
	return 0;
}
