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 <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RREP(i, n) for(int i=n-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=b-1;i>=a;--i)

#define all(x) begin(x),end(x)
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

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;
}


template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
    os << p.first << ":" << p.second;
    return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
    os << "[";
    for (auto it = mp.begin(); it != mp.end(); ++it) {
        if (it != mp.begin()) {
            os << ", ";
        }
        os << *it;
    }
    return os << "]";
}

// edit

class Flow {
    using T = long long;
public:
    struct edge {
        int to;
        T cap, rev;
    };
    vector<vector<edge> > G;
    vector<bool> used;
    vector<T> level;//sからの距離
    vector<int> iter;//どこまで調べ終わったか

    //0 ~ maxSize-1 のフローを構築できるようにする
    explicit Flow(unsigned maxSize) {
        G.resize(maxSize, vector<edge>());
        used.resize(maxSize, false);
        level.resize(maxSize, 0);
        iter.resize(maxSize, 0);
    }

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

    //sからの最短距離をDFSで探す
    void bfs(int s) {
//        memset(level, -1, sizeof(level));
        fill(level.begin(), level.end(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            for (int i = 0; i < G[v].size(); i++) {
                edge &e = G[v][i];
                if (e.cap > 0 && level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }

    //増加パスを探す
    T dfs(int v, int t, T f) {
        if (v == t) return f;
        for (int &i = iter[v]; i < 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;
    }

    //sからtへの最大流を求める
    T max_flow(int s, int t) {
        T flow = 0;
        for (;;) {
            bfs(s);
            if (level[t] < 0) return flow;
//            memset(iter, 0, sizeof(iter));
            fill(iter.begin(), iter.end(), 0);
            T f;
            while ((f = dfs(s, t, INF)) > 0) {
                flow += f;
            }
        }
    }

};

void solve() {
    int h, w;
    ll W;
    cin >> h >> w >> W;

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

    int S = 0, T = 1;
    vector<int> col(2);
    vector<vector<int>> idx(h, vector<int>(w, -1));
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if ((i + j) % 2) continue;
            int sz = col.size();
            idx[i][j] = sz;
            col.push_back(i * j % 2);
        }
    }

    Flow flow(col.size());

    ll ans = 0;
    for (int i = 0; i < h - 1; ++i) {
        for (int j = 0; j < w - 1; ++j) {
            if ((i + j) % 2) continue;
            int node = idx[i][j];
            ll cost = 0;
//            int cost = B[i][j] + B[i][j + 1] + B[i + 1][j] + B[i + 1][j + 1];
            for (int di = 0; i + di < h && di <= 1; ++di) {
                for (int dj = 0; j + dj < w && dj <= 1; ++dj) {
                    cost += B[i + di][j + dj];
                }
            }
            ans += cost;

            if (col[node] == 0) {
                flow.add_edge(node, T, W);
                flow.add_edge(S, node, cost);

                if (i > 0 && j > 0)flow.add_edge(node, idx[i - 1][j - 1], B[i][j]);
                if (i > 0 && j + 1 < w)flow.add_edge(node, idx[i - 1][j + 1], B[i][j + 1]);
                if (i + 1 < h && j > 0)flow.add_edge(node, idx[i + 1][j - 1], B[i + 1][j]);
                if (i + 1 < h && j + 1 < w)flow.add_edge(node, idx[i + 1][j + 1], B[i + 1][j + 1]);
//                for (int di = -1; di <= 1; di += 2) {
//                    for (int dj = -1; dj <= 1; dj += 2) {
//                        flow.add_edge(node, idx[i + di][j + dj], T);
/*
                2 4
                10
                1 2 5 8
                2 4 7 1
                */
//                    }
//                }
            } else {
                flow.add_edge(S, node, W);
                flow.add_edge(node, T, cost);
            }
        }
    }

    ll val = flow.max_flow(S, T);

    ans -= val;
    cout << ans << endl;


}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}