unittest
{
	assert( [ "4 4 2", "3413", "4121", "1424", "2312" ].parse.expand.solve == 23 );
	assert( [ "4 4 2", "1212", "2121", "1212", "2121" ].parse.expand.solve == 54 );
	assert( [ "7 7 2", "8989898", "9898989", "8989898", "9898989", "8989898", "9898989", "8989898" ].parse.expand.solve == 2520 );
	assert( [ "17 17 2", "12345678912345678", "23456789123456789", "34567891234567891", "45678912345678912", "56789123456789123", "67891234567891234", "78912345678912345", "89123456789123456", "91234567891234567", "12345678912345678", "23456789123456789", "34567891234567891", "45678912345678912", "56789123456789123", "67891234567891234", "78912345678912345", "89123456789123456" ].parse.expand.solve == 2354638 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto hwk = input.front.split.to!( long[] );
	auto css = new long[][]( hwk[ 1 ], hwk[ 0 ] );		// css[0][0]を左下、css[w][h]を右上にする
	foreach_reverse( y; 0L .. hwk[ 0 ] )
	{
		input.popFront;
		foreach( x, c; input.front.map!( ( a ) => ( a - '0' ).to!( long ) ).enumerate( 0L ) )
		{
			css[ x ][ y ] = c;
		}
	}
	return tuple( hwk[ 0 ], hwk[ 1 ], hwk[ 2 ], css );
}

auto solve( long h, long w, long k, long[][] css )
{
	auto ans = 0L;
	foreach( y; 0L .. h )
	{
		foreach( x; 0L .. w )
		{
			ans = max( ans, solve2( h, w, k, css.dup, x, y ) );
		}
	}
	return ans;
}

auto solve2( long h, long w, long k, long[][] css, long sx, long sy )
{
	css[ sx ] = css[ sx ][ 0 .. sy ] ~ css[ sx ][ sy + 1 .. $ ];
	
	auto st = 0L;
	for( auto i = 0L;; i++ )
	{
		auto s = 0L;
		foreach_reverse( y; 0L .. h )
		{
			for( auto lx = 0L, rx = 1L; lx < w; lx = rx, rx++ )
			{
				if( css[ lx ].length <= y ) continue;
				
				while( rx < w && y < css[ rx ].length && css[ lx ][ y ] == css[ rx ][ y ] ) rx++;
				
				auto n = rx - lx;
				if( k <= n )
				{
					s += css[ lx ][ y ] * n;
					foreach( cx; lx .. rx )
					{
						css[ cx ] = css[ cx ][ 0 .. y ] ~ css[ cx ][ y + 1 .. $ ];
					}
				}
			}
		}
		if( s == 0 ) break;
		st += s * 2 ^^ i;
	}
	return st;
}
