#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
struct Dinic {
	using Flow = int;
	const Flow kInfFlow = INF;
	struct Edge {
		int to, rev;
		Flow cap;
		Edge() {}
		Edge(int to, int rev, Flow cap) :to(to), rev(rev), cap(cap) {}
	};
	int n;
	vector<vector<Edge>> g;
	vector<bool> used;
	vector<int> level;
	vector<int> iter;
	Dinic(int n) :n(n), g(n), used(n), level(n), iter(n) {};
	void addArc(int from, int to, Flow cap) {
		dump(from, to, cap);
		g[from].emplace_back(to, (int)g[to].size(), cap);
		g[to].emplace_back(from, (int)g[from].size() - 1, Flow());
	}
	void addEdge(int a, int b, Flow cap) {
		g[a].emplace_back(b, (int)g[b].size(), cap);
		g[b].emplace_back(a, (int)g[a].size() - 1, cap);
	}
	Flow maximumFlow(int s, int t) {
		Flow total = Flow();
		while (true) {
			levelize(s);
			if (level[t] < 0)return total;
			fill(iter.begin(), iter.end(), 0);
			Flow f;
			while (true) {
				f = augment(s, t, kInfFlow);
				if (f == Flow())break;
				total += f;
			}
		}
	}
	Flow augment(int v, int t, Flow f) {
		if (v == t)return f;
		for (int &i = iter[v]; i < g[v].size(); i++) {
			Edge &e = g[v][i];
			if (e.cap > Flow() && level[v] < level[e.to]) {
				Flow d = augment(e.to, t, min(f, e.cap));
				if (d > Flow()) {
					e.cap -= d;
					g[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return Flow();
	}
	void levelize(int s) {
		fill(level.begin(), level.end(), -1);
		queue<int> q;
		level[s] = 0;
		q.push(s);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (int i = 0; i < g[v].size(); i++) {
				Edge &e = g[v][i];
				if (e.cap > Flow() && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int h, w, W; cin >> h >> w >> W;

	vector<vector<int>> B(h, vector<int>(w));
	rep(i, 0, h) rep(j, 0, w) { cin >> B[i][j]; }

	int size = h * w + 2;
	Dinic dinic(size);
	int s = size - 2;
	int t = size - 1;

	int sum = 0;

	rep(i, 0, h - 1)rep(j, 0, w - 1) {
		if ((i + j) % 2 != 0)continue;
		int cap = B[i][j] + B[i + 1][j] + B[i][j + 1] + B[i + 1][j + 1] - W;
		sum += max(0LL, cap);
		dump(i, j, cap);
		if (i % 2 == 0) {
			if (cap > 0) {
				dinic.addArc(s, i*w + j, cap);
			}
			else {
				dinic.addArc(i*w + j, t, -cap);
			}
		}
		else {
			if (cap > 0) {
				dinic.addArc(i*w + j, t, cap);
			}
			else {
				dinic.addArc(s, i*w + j, -cap);
			}
		}

		if (i % 2 == 0) {
			rep(di, -1, 2)rep(dj, -1, 2) {
				if (di == 0 || dj == 0)continue;
				int ni = i + di, nj = j + dj;
				if (ni >= 0 && ni < h - 1 && nj >= 0 && nj < w - 1) {
					int bi = i + (di == -1 ? 0 : 1), bj = j + (dj == -1 ? 0 : 1);
					dump(B[bi][bj]);
					int cap = B[bi][bj];
					dinic.addArc(i * w + j, ni*w + nj, cap);
				}
			}
		}
	}

	cout << sum - dinic.maximumFlow(s, t) << endl;
	return 0;
}


Measured memory_bytes: 3420