#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map> 
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#include <sstream>
#include <complex>
#define BIT(nr) (1ULL << (nr))
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807	
#define LL_HALFMAX 9223372036854775807 / 2	
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

using namespace std;

using Weight = int;
using Flow = int;
struct Edge {
	int src, dst;

	// libalgo のものに追加、メンバを追加するだけなので互換性は崩さないはず、逆辺のG[e.dstの]インデックスを保持
	int rev;
	Weight weight;
	Flow cap;
	Edge() : src(0), dst(0), weight(0) {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
	bool operator<(const Edge& right) const {
		return dst < right.dst;
	}
};

using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph& g, int a, int b, Weight w = 1) {
	g[a].emplace_back(a, b, w);
	g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

int dx[4] = { 0, 1, 0, -1 }; // x軸方向への変位
int dy[4] = { 1, 0, -1, 0 }; // y軸方向への変位


class Vector3D {
public:
	double x, y, z;
	Vector3D(double x, double y, double z) {
		this->x = x;
		this->y = y;
		this->z = z;
	}

	Vector3D operator- (Vector3D right) {
		return Vector3D(right.x - x, right.y - y, right.z - z);
	}

	double Length() {
		return sqrtl(x * x + y * y + z * z);
	}
};


#ifdef DEBUG
template <class T>ostream& operator<<(ostream& o, const vector<T>& v)
{
	o << "{"; for (int i = 0; i < (int)v.size(); i++)o << (i > 0 ? ", " : "") << v[i]; o << "}"; return o;
}
#endif // DEBUG

template <class T>ostream& operator<<(ostream& o, const vector<T>& v)
{
	for (int i = 0; i < (int)v.size(); i++)o << (i > 0 ? " " : "") << v[i]; return o;
}

template< class T >
class CumulativeSum2D {
private:
	// 元データ
	vector<vector<T>> m_Data;
	int m_Height;
	int m_Width;
	// 累積和
	vector<vector<T>> m_Cum;

public:
	CumulativeSum2D(int H, int W) :
		m_Cum(H+1, vector<int>(W+1, 0)), 
		m_Height(H),
		m_Width(W)
	{}

	// TORIAEZU: コピー
	CumulativeSum2D(vector<vector<T>> inputData) :
		m_Data(inputData),
		m_Height(static_cast<int>(inputData.size())),
		m_Width(static_cast<int>(inputData[0].size())),
		m_Cum(m_Height + 1, vector<int>(m_Width + 1, 0))
	{
	}

	// 0-indexed
	void Change(int x, int y, T val) {
		assert(x < m_Width);
		assert(y < m_Height);

		m_Data[y][x] = val;
	}

	void Build() {
		for (int i = 0; i < m_Height; i++) {
			for (int j = 0; j < m_Width; j++) {
				m_Cum[i + 1][j + 1] = m_Cum[i][j + 1] + m_Cum[i + 1][j] - m_Cum[i][j] + m_Data[i][j];
			}
		}
	}

	// [sx, gx), [sy, gy) の範囲の和を取得
	T Query(int sx, int sy, int gx, int gy) {
		return m_Cum[gy][gx] - m_Cum[sy][gx] - m_Cum[gy][sx] + m_Cum[sy][sx];
	}
};


// ランレングス圧縮
template<typename T>
vector<pair<T, int>> RunLength(vector<T> arr) {
	T now = arr[0];
	int len = arr.size();

	vector<pair<T, int>> res;
	int cnt = 0;
	rep(i, len) {
		if (arr[i] != now) {
			res.emplace_back(now, cnt);
			now = arr[i];
			cnt = 0;
		}
		cnt++;
	}

	res.push_back(mp(now, cnt));
	return res;
}

class Circle {
public:
	int x, y, r;
	Circle(int x, int y, int r) {
		this->x = x;
		this->y = y;
		this->r = r;
	}

};

void print(vector<vector<int>> v) {
	for (auto ve : v) {
		cout << ve << "\n";
	}
	cout << "\n";
}

int pow2(int N) {
	int ret = 1;
	rep(i, N) {
		ret *= 2;
	}
	return ret;
}

bool solve() {
	int H, W, K;
	cin >> H >> W >> K;
	if (H == 0) {
		return false;
	}
	W++;
	vector<vector<int>> Board(H, vector<int>(W, -2));

	rep(y, H) {
		rep(x, W-1) {
			char c;
			cin >> c;
			Board[y][x] = (int)c - '0';
		}
	}
	auto calc = [=](int dy, int dx, vector<vector<int>> B) {
		int ret = 0;
		int procCnt = 0;
		B[dy][dx] = -1;

		while (1) {
			int score = 0;
			// おとす
			function<void(int, int)> rec = [&](int y, int x) {
				if (y == H - 1) {
					return;
				}
				if (B[y][x] < 0) {
					return;
				}

				if (B[y + 1][x] == -1) {
					swap(B[y][x], B[y + 1][x]);
					rec(y + 1, x);
				}
			};

			RREP(y, H) {
				rep(x, W) {
					rec(y, x);
				}
			}

			//print(B);
			// みつける
			bool found = false;
			rep(y, H) {
				int s = 0;
				int cnt = 0;
				int now = B[y][0];
				rep(x, W) {
					if (B[y][x] == -1) {
						if (cnt >= K) {
							REP(i, s, x) {
								score += B[y][i];
								B[y][i] = -1;
							}
							found = true;
						}
						s = x;
						cnt = 0;
						now = B[y][x];
					}
					else if (now == B[y][x]) {
						cnt++;
					}
					else if (cnt >= K) {
						REP(i, s, x) {
							score += B[y][i];
							B[y][i] = -1;
						}
						s = x;
						cnt = 1;
						now = B[y][x];
						found = true;
					}
					else {
						s = x;
						cnt = 1;
						now = B[y][x];
					}
				}
				// 番兵 おくので終わった後に追加発見処理はいらない
			}
			if (!found) {
				break;
			}

			score *= pow2(procCnt);
			procCnt++;
			ret += score;
		}
		return ret;
	};

	int ans = -1;
	rep(y, H) {
		rep(x, W-1) {
			int cand = calc(y, x, Board);
			chmax(ans, cand);
		}
	}

	cout << ans << "\n";
	return true;
}

signed main() {
	
	solve();

	return 0;
}

Measured memory_bytes: 3604