using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    int idx(int y, int x) => y * W + x;
    int H, W;
    void solve()
    {
        H = cin.nextint;
        W = cin.nextint;
        var C = cin.nextlong;
        var B = new long[H][];
        for (int i = 0; i < H; i++)
        {
            B[i] = cin.scanlong;
        }
        var D = new Dinic(H * W + 2);
        int s = H * W;
        int t = H * W + 1;

        
        var value = 0L;
        bool mode = true;
        for (int i = 0; i < H - 1; i++)
        {
            for (int j = 0; j < W - 1; j++)
            {
                if ((i + j) % 2 == 1) continue;

                var sum = B[i][j] + B[i + 1][j] + B[i][j + 1] + B[i + 1][j + 1];
                var on = 0L;
                var off = 0L;
                if (C < sum)
                {
                    value += sum - C;
                    off = sum - C;
                }
                else
                {
                    on = C - sum;
                }

                if (i % 2 == 0)
                {
                    D.add_edge(s, idx(i, j), off);
                    D.add_edge(idx(i, j), t, on);
                    if (i + 2 != H && j + 2 != W)
                    {
                        D.add_edge(idx(i, j), idx(i + 1, j + 1), B[i + 1][j + 1]);
                    }
                    if (i + 2 != H && j != 0)
                    {
                        D.add_edge(idx(i, j), idx(i + 1, j - 1), B[i + 1][j]);
                    }
                }
                else
                {
                    D.add_edge(s, idx(i, j), on);
                    D.add_edge(idx(i, j), t, off);
                    D.add_edge(idx(i + 1, j + 1), idx(i, j), B[i + 1][j + 1]);
                    D.add_edge(idx(i + 1, j - 1), idx(i, j), B[i + 1][j]);
                }
                //mode = !mode;
            }
        }
        //WriteLine(value);

        WriteLine(value - D.max_flow(s, t));

    }

}

class Dinic
{
    // 辺を表すクラス（行き先、容量、逆辺）
    class Edge_
    {
        public int to;
        public long cap;
        public int rev;
        public Edge_(int to, long cap, int rev)
        {
            this.to = to;
            this.cap = cap;
            this.rev = rev;
        }
    }
    public const long INF = long.MaxValue / 3;
    List<Edge_>[] G; // グラフの隣接リスト表現
    int[] level;    // sからの距離
    int[] iter;     // どこまで調べ終わったか
    public Dinic(int V)
    {
        G = new List<Edge_>[V];
        for (int i = 0; i < V; i++) G[i] = new List<Edge_>();
        level = new int[V];
        iter = new int[V];
    }
    // fromからtoへ向かう容量capの辺をグラフに追加する
    public void add_edge(int from, int to, long cap)
    {
        G[from].Add(new Edge_(to, cap, G[to].Count));
        G[to].Add(new Edge_(from, 0, G[from].Count - 1));
    }
    // sからの最短距離をBFSで計算する
    void bfs(int s)
    {
        for (int i = 0; i < level.Length; i++) level[i] = -1;
        var que = new Queue<int>();
        level[s] = 0;
        que.Enqueue(s);
        while (que.Any())
        {
            int v = que.Dequeue();
            foreach (Edge_ e in G[v])
            {
                if (e.cap > 0 && level[e.to] < 0)
                {
                    level[e.to] = level[v] + 1;
                    que.Enqueue(e.to);
                }
            }
        }
    }
    //増加パスをDFSで探す
    long dfs(int v, int t, long f)
    {
        if (v == t) return f;
        for (int i = iter[v]; i < G[v].Count; i++)
        {
            Edge_ e = G[v][i];
            if (e.cap > 0 && level[v] < level[e.to])
            {
                long d = dfs(e.to, t, Math.Min(f, e.cap));
                if (d > 0)
                {
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    // sからtへの最大流を求める
    public long max_flow(int s, int t)
    {
        long flow = 0;
        while (true)
        {
            bfs(s);
            if (level[t] < 0) return flow;
            for (int i = 0; i < iter.Length; i++) iter[i] = 0;
            while (true)
            {
                long f = dfs(s, t, INF);
                if (f <= 0) break;
                flow += f;
            }
        }
    }
}

static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}



Measured memory_bytes: 18716