#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

const int mod = 1e9;

ll modpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    ll h, w, k;
    cin >> h >> w >> k;
    string S;
    ll now = 0;
    vector<vector<ll>> board(h, vector<ll>(w, 0));
    for (ll i = 0; i < h; i++)
    {
        cin >> S;
        for (ll j = 0; j < w; j++)
        {
            board[i][j] = S[j] - '0';
        }
    }
    for (int row = 0; row < h; row++)
    {
        for (int column = 0; column < w; column++)
        {
            vector<vector<ll>> board2 = board;
            board2[row][column] = 0;
            for (int k = row; k > 0; k--)
            {
                swap(board2[k][column], board2[k - 1][column]);
            }

            bool can = true;
            ll times = 0;
            ll ans = 0;
            while (can)
            {
                can = false;
                for (ll i = 0; i < h; i++)
                {
                    ll key = board2[i][0];
                    ll cnt = 1;
                    for (ll j = 1; j < w; j++)
                    {
                        if (key == board2[i][j] && key != 0)
                        {
                            cnt++;
                        }
                        else
                        {
                            if (cnt >= k)
                            {
                                can = true;
                                ans += cnt * modpow(2, times) * board2[i][j - 1];
                                for (int m = 1; m < cnt + 1; m++)
                                {
                                    board2[i][j - m] = 0;
                                }
                            }
                            key = board2[i][j];
                            cnt = 1;
                        }
                    }
                    if (cnt >= k)
                    {
                        can = true;
                        ans += cnt * modpow(2, times) * board2[i][w - 1];
                        for (int m = 1; m < cnt + 1; m++)
                        {
                            board2[i][w - m] = 0;
                        }
                    }
                }
                times++;
                if (can)
                {
                    for (int j = 0; j < w; j++)
                    {
                        for (int i = h - 1; i >= 0; i--)
                        {
                            if (board2[i][j] == 0)
                            {
                                int zero = i;
                                while (zero >= 0 && board2[zero][j] == 0)
                                {
                                    zero--;
                                    if (zero == -1)
                                    {
                                        break;
                                    }
                                }
                                if (zero != -1)
                                {
                                    board2[i][j] = board2[zero][j];
                                    board2[zero][j] = 0;
                                }
                            }
                        }
                    }
                }
            }
            now = max(now, ans);
        }
    }
    cout << now << endl;
    return 0;
}

Measured memory_bytes: 3644