#include<bits/stdc++.h>

using namespace std;

using int64 = long long;

template< typename T >
struct Dinic {
  struct edge {
    int to;
    T cap;
    int rev;
  };

  const T INF;
  vector< vector< edge > > graph;
  vector< T > min_cost;
  vector< int > iter;

  Dinic(int n) : INF(numeric_limits< T >::max()) {
    graph.resize(n);
  }

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

  bool bfs(int s, int t) {
    min_cost.assign(graph.size(), -1);
    queue< int > que;
    min_cost[s] = 0;
    que.push(s);
    while(!que.empty()) {
      int p = que.front();
      que.pop();
      for(auto &e : graph[p]) {
        if(e.cap > 0 && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return (min_cost[t] != -1);
  }

  T dfs(int idx, const int t, T flow) {
    if(idx == t) return (flow);
    for(int &i = iter[idx]; i < graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
        T d = dfs(e.to, t, min(flow, e.cap));
        if(d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return (d);
        }
      }
    }
    return (0);
  }

  T max_flow(int s, int t) {
    T flow = 0;
    while(bfs(s, t)) {
      iter.assign(graph.size(), 0);
      T f = 0;
      while((f = dfs(s, t, INF)) > 0) {
        flow += f;
      }
    }
    return (flow);
  }
};

int main() {
  int H, W, X;
  int B[150][150];
  vector< pair< int, int > > arr[150][150];
  memset(B, -1, sizeof(B));

  cin >> H >> W >> X;
  int64 all = 0;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      cin >> B[i * 2][j * 2];
      all += B[i * 2][j * 2];
    }
  }
  int C[150][150];
  int usagi[100000];
  memset(C, -1, sizeof(C));
  int ptr = 0, proc = 0;
  for(int i = 0; i + 1 < H; i++) {
    bool add = false;
    for(int j = 0; j + 1 < W; j++) {
      if((i + j) % 2 == 0) {
        add = true;
        C[i * 2 + 1][j * 2 + 1] = ptr++;
      }
    }
    usagi[i * 2 + 1] = proc;
    usagi[i * 2 + 1] %= 2;
    proc += add;
  }
  for(int i = 0; i < H * 2; i++) {
    bool add = false;
    for(int j = 0; j < W * 2; j++) {
      if(C[i][j] == -1) continue; // 電球
      add = true;
      for(int k = -1; k <= 1; k++) {
        for(int l = -1; l <= 1; l++) {
          int ni = k + i, nj = l + j;
          if(ni < 0 || nj < 0) continue;
          if(B[ni][nj] == -1) continue;
          arr[ni][nj].emplace_back(C[i][j], usagi[i]);
        }
      }
    }
    proc += add;
  }


  Dinic< int64 > flow(ptr + 2);
  int S = ptr, T = ptr + 1;


  for(int i = 0; i < H * 2; i++) {
    for(int j = 0; j < W * 2; j++) {
      if(C[i][j] != -1) {
        if(usagi[i] == 1) { // ONのときW失う
          flow.add_edge(C[i][j], T, X);
        } else { // 0のときW失う
          flow.add_edge(S, C[i][j], X);
        }
      }
      if(B[i][j] == -1) continue;
      if(arr[i][j].size() == 0) {
        all -= B[i][j];
      } else if(arr[i][j].size() == 1) {
        if(arr[i][j][0].second == 0) {
          flow.add_edge(arr[i][j][0].first, T, B[i][j]);
        } else {
          flow.add_edge(S, arr[i][j][0].first, B[i][j]);
        }
      } else {
        if(arr[i][j][0].second == 1) {
          swap(arr[i][j][0], arr[i][j][1]);
        }
        flow.add_edge(arr[i][j][0].first, arr[i][j][1].first, B[i][j]);
      }
    }
  }
  cout << all - flow.max_flow(S, T) << endl;
}





Measured memory_bytes: 4128