#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>

#define int long long

struct edge {
    int to;
    int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;


#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define delim(i, n) cout << (i == n-1 ? "\n" : " ");

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn =  30;
int h, w, k;
vii init_board(maxn+1);

int ctoi(const char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default : return -1;
  }
}

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

    cin >> h >> w >> k;
    REP(i, h) {
        string tmp; cin >> tmp;
        REP(j, w) {
            init_board[h-i-1].push_back(ctoi(tmp[j]));
        }
    }
    REP(j, w) init_board[h].push_back(0);

    int pm = 0;
    if (k <= 3) {
        REP(b, w) {
            REP(a, h) {
                int point = 0;
                vii board(h+1);
                REP(i, h+1) {
                    REP(j, w) board[i].push_back(init_board[i][j]);
                }

                FOR(i, a, h) {
                    board[i][b] = board[i+1][b];
                    board[i+1][b] = 0;
                }

                bool flag = true;
                int weight = 1;
                while (flag) {
                    flag = false;
                    REP(i, h) {
                        int idx = 0;
                        vi cnt;
                        cnt.push_back(1);
                        vi s;
                        s.push_back(0);
                        REP(j, w-1) {
                            if (board[i][j] == board[i][j+1]) cnt[idx]++;
                            else {
                                if (cnt[idx] < k) {
                                    cnt[idx] = 1;
                                    s[idx] = j+1;
                                } else {
                                    idx++;
                                    cnt.push_back(1);
                                    s.push_back(j+1);
                                }
                            }
                        }
                        if (cnt[idx] >= k) idx++;
                        REP(j, idx) {
                            if (cnt[j] >= k && board[i][s[j]] != 0) {
                                flag = true;
                                point += board[i][s[j]]*cnt[j]*weight;
                                REP(l, cnt[j]) board[i][s[j]+l] = 0;
                            }
                        }
                    }
                    weight *= 2;
                    bool flag2 = true;
                    while(flag2) {
                        flag2 = false;
                        REP(i, h) {
                            REP(j, w) {
                                if (board[i][j] == 0 && board[i+1][j] != 0) {
                                    flag2 = true;
                                    board[i][j] = board[i+1][j];
                                    board[i+1][j] = 0;
                                }
                            }
                        }
                    }
                    if (point == 0) flag = false;
                    else chmax(pm, point);
                }
                if (point == 0) break;
            }
        }
    }
    cout << std::fixed << std::setprecision(10) << pm << "\n";
}

Measured memory_bytes: 3660