Problem: problem

There is a light bulb at the grid point $ (x, y) $ that meets $ 1 \ leq x \ leq h, 1 \ leq y \ leq w $.

The power supply is installed at coordinates $ (i + 0.5, j + 0.5) (1 \ leq i <h, 1 \ leq j <w, i + j $ is even) (corrected at 14:21). Coordinates $ When the power supply installed at (i + 0.5, j + 0.5) $ is turned on, the coordinates $ (i, j), (i + 1, j), (i, j + 1), (i + 1) , j + 1) $ 4 $ light bulbs in $ glow.

If you have more than $ 1 $ connected to a power supply with a light bulb on $ (i, j) $, you get $ B_ {i, j} $ cleanliness. You have the total cleanliness. Also, if you turn on each power supply, it costs $ W $ for electricity per $ 1 $.

Turn on some power to maximize "total income-total electricity bill".



output

Output the maximum value of "total income-total electricity bill". Also, output a line break at the end.

Example

Input

4 4 10
100 100 100 100
100 100 100 100
1 100 100 1
1 1 1 1


Output

970

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

template<class T>
struct dinic {
    struct edge{
        int to;
        T cap;
        int rev;
        bool isrev;
    };

    vector<vector<edge>> G;
    vector<int> level, iter;

    void bfs(int s) {
        level.assign(G.size(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(que.size()) {
            int v = que.front(); que.pop();
            for(auto i: G[v]) {
                if(i.cap > 0 && level[i.to] < 0) {
                    level[i.to] = level[v] + 1;
                    que.push(i.to);
                }
            }
        }
    }

    T dfs(int v, const int t, T f) {
        if(v == t) return f;
        for(int &i = iter[v]; i<(ll)G[v].size(); ++i) {
            edge &e = G[v][i];
            if(e.cap > 0 && level[v] < level[e.to]) {
                T 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;
    }

    dinic() {}
    dinic(int n) : G(n), level(n), iter(n) {}

    void add_edge(int from, int to, T cap) {
        G[from].push_back({to, cap, (int)G[to].size(), false});
        G[to].push_back({from, 0, (int)G[from].size()-1, true});
    }

    // sからtへ流量fを流す
    T maxflow(int s, int t, T f = 1LL<<30) {
        T flow = 0;
        while(1) {
            bfs(s);
            if(level[t] < 0) return flow;
            iter.assign(G.size(), 0);
            T tmp;
            while((tmp = dfs(s, t, f)) > 0) flow += tmp;
        }
    }

    // sからtへ1流す
    T flow(int s, int t) {
        bfs(s);
        if(level[t] < 0) return 0;
        iter.assign(G.size(), 0);
        return dfs(s, t, 1);
    }
    // 始点，終点tのフローで辺e=(from,to)の容量を1増やしたときの最大流の変化
    // 並列辺はたぶんバグる
    T add(int from, int to, int s, int t) {
        for(auto &e: G[from]) {
            if(e.to == to && !e.isrev) {
                e.cap++;
                break;
            }
        }
        return flow(s, t);
    }
    // 始点s，終点tのフローで辺e=(from,to)の容量を1減らしたときの最大流の変化
    // 並列辺はたぶんバグる
    T sub(int from, int to, int s, int t) {
        for(auto &e: G[from]) {
            if(e.to == to && !e.isrev) {
                T diff = 0;
                // 辺(from,to)で容量いっぱいに流れている
                if(e.cap == 0) {
                    // 残余グラフでfrom→toのパスがない
                    if(flow(from, to) == 0) {
                        flow(t, to);
                        flow(from, s);
                        diff = -1;
                    }
                    G[e.to][e.rev].cap--;
                } else {
                    e.cap--;
                }
                return diff;
            }
        }
        assert(false); // 存在しない辺を減らそうとした
    }

    friend ostream &operator <<(ostream& out, const dinic& a){
        out << endl;
        for(int i = 0; i < (int)a.G.size(); i++) {
            for(auto &e : a.G[i]) {
                if(e.isrev) continue;
                auto &rev_e = a.G[e.to][e.rev];
                out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
        return out;
    }
};

int main(void) {
    ll h, w, a;
    cin >> h >> w >> a;
    vector<vector<ll>> b(h+1, vector<ll>(w+1));
    REP(i, h) REP(j, w) cin >> b[i+1][j+1];

    // trans[i][j] = マス(i,j)の最小カット求めるグラフでの頂点番号
    ll n = 0;
    vector<vector<ll>> trans(h, vector<ll>(w, -1));
    FOR(i, 1, h) FOR(j, 1, w) if((i+j)%2==0) trans[i][j] = n++;

    dinic<ll> graph(n+2);
    ll s = n, t = n+1;

    ll ret = 0;
    FOR(i, 1, h) FOR(j, 1, w) {
        if((i+j)%2) continue;
        ll bsum = b[i][j] + b[i+1][j] + b[i][j+1] + b[i+1][j+1];
        ll score = bsum - a;
        if(score <= 0) {
            // 辺の重みを反転
            if(i%2) {
                graph.add_edge(trans[i][j], t, score);
                graph.add_edge(s, trans[i][j], 0);
            } else {
                graph.add_edge(trans[i][j], t, 0);
                graph.add_edge(s, trans[i][j], score);
            }
        } else {
            ret += score;
            // 辺の重みを反転
            if(i%2) {
                graph.add_edge(s, trans[i][j], 0);
                graph.add_edge(trans[i][j], t, score);
            } else {
                graph.add_edge(s, trans[i][j], score);
                graph.add_edge(trans[i][j], t, 0);
            }
        }
    }
    // 電球を共有する装置の分の辺
    for(ll i=2; i<h-1; i+=2) FOR(j, 1, w-1) {
        if((i+j)%2) continue;
        if(trans[i+1][j-1]!=-1) {
            graph.add_edge(trans[i][j], trans[i+1][j-1], b[i+1][j]);
        }
        if(trans[i+1][j+1]!=-1) {
            graph.add_edge(trans[i][j], trans[i+1][j+1], b[i+1][j+1]);
        }
        if(trans[i-1][j-1]!=-1) {
            graph.add_edge(trans[i][j], trans[i-1][j-1], b[i][j]);
        }
        if(trans[i-1][j+1]!=-1) {
            graph.add_edge(trans[i][j], trans[i-1][j+1], b[i][j+1]);
        }
    }

    cout << ret - graph.maxflow(s, t) << endl;

    return 0;
}