#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-2;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
#include<cstring>
struct edge { int to; ll cap;int rev; };
vector<edge> G[100000];
bool used[100000];
void add_edge(int from, int to, ll cap) {
	G[from].push_back(edge{ to, cap, (int)G[to].size() });
	G[to].push_back(edge{ from, 0, (int)G[from].size() - 1 });
}
ll dfs(int v, int t, ll f) {
	if (v == t)return f;
	used[v] = true;
	for (int i = 0; i < (int)G[v].size(); i++) {
		edge &e = G[v][i];
		if (!used[e.to] && e.cap > 0) {
			ll d = dfs(e.to, t, min(f, e.cap));
			if (d > 0) {
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}
ll max_flow(int s, int t) {
	ll flow = 0;
	for (;;) {
		memset(used, 0, sizeof(used));
		ll f = dfs(s, t, INF);
		if (f == 0)return flow;
		flow += f;
	}
}

int h, w; ll z;
ll b[50][50];
int main() {
	cin >> h >> w >> z;
	int s = h * w; int t = s + 1;
	rep(i, h) {
		rep(j, w) {
			cin >> b[i][j];
		}
	}
	rep(i, h - 1) {
		rep(j, w - 1) {
			if ((i + j) % 2)continue;
			if (i % 2 == 0) {
				add_edge(s, i*(w-1) + j, z);
			}
			else add_edge(i*(w-1) + j, t, z);
		}
	}
	if (h == 2) {
		ll ans = 0;
		for (int j = 0; j < w - 1; j += 2) {
			ll csum = b[0][j] + b[0][j + 1] + b[1][j] + b[1][j + 1] - z;
			ans += max((ll)0, csum);
		}
		cout << ans << endl;
		return 0;
	}
	if (w == 2) {
		ll ans = 0;
		for (int i = 0; i < h - 1; i += 2) {
			ll csum = b[i][0] + b[i][1] + b[i + 1][0] + b[i + 1][1] - z;
			ans += max((ll)0, csum);
		}
		cout << ans << endl;
		return 0;
	}
	rep(i, h-1) {
		rep(j, w-1) {
			if ((i + j) % 2)continue;
			if (i % 2)continue;
			if (i + 1 < h-1) {
				if (j - 1 >= 0) {
					int to = (i+1) * (w-1) + j - 1;
					add_edge(i*(w-1) + j, to, b[i + 1][j]);
				}
				if (j + 1 < w-1) {
					int to = (i+1) * (w-1) + j + 1;
					add_edge(i*(w-1) + j, to, b[i + 1][j + 1]);
				}
			}
			if(i-1>=0) {
				if (j - 1 >= 0) {
					int to = (i - 1)*(w-1)+ j - 1;
					add_edge(i*(w-1) + j, to, b[i][j]);
				}
				if (j + 1 < w-1) {
					int to = (i - 1)*(w-1) + j + 1;
					add_edge(i*(w-1) + j, to, b[i][j+1]);
				}
			}
		}
	}
	rep(i, h) {
		int ni = 2 * (i / 2); ni *= (w-1);
		add_edge(ni, t, b[i][0]);
		
		ni += w - 2;
		if (w % 2==0) {
			add_edge(ni, t, b[i][w - 1]);
		}
	}
	rep(j, w) {
		if (j == 0 || j == w - 1)continue;
		int ni = 2 * (j / 2);
		add_edge(ni, t, b[0][j]);
		if (h % 2==0) {
			ni += (h - 2)*(w-1);
			add_edge(ni, t, b[h-1][j]);
		}
	}
	ll ans = max_flow(s, t);
	ans = -ans;
	rep(i, h) {
		rep(j, w) {
			ans += b[i][j];
		}
	}
	cout << ans << endl;
	//stop
	return 0;
}



Measured memory_bytes: 5896