#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 2200
using namespace std;
int a[N][N],f[N][N],l[N],r[N],b[N],id[N];
int n,m,ans;
char s[N];
int check(int x,int y){
	return ((a[x][y]==a[x-1][y])&&(a[x][y-1]==a[x-1][y-1]))||((a[x][y]!=a[x-1][y])&&(a[x][y-1]!=a[x-1][y-1]));
}
bool cmp(int x,int y){
	return b[x]>b[y];
}
int solve(){
	for(int i=1;i<=n;i++)id[i]=i;
	sort(id+1,id+1+n,cmp);
	memset(l,0,sizeof(l));
	memset(r,0,sizeof(r));
	for(int i=1;i<=n;i++){
		int x=id[i];
		l[x]=x;
		r[x]=x;
		if(l[x-1])l[x]=min(l[x],l[x-1]);
		if(r[x+1])r[x]=max(r[x],r[x+1]);
		r[l[x]]=r[x];
		l[r[x]]=l[x];
		ans=max(ans,(r[x]-l[x]+1)*b[x]+((l[x]-1>0)?b[x]:0));
		l[0]=0;
		r[0]=0;
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=m;j++)a[i][j]=s[j]=='.';
	}
	for(int i=1;i<=m;i++)f[1][i]=i;
	for(int i=2;i<=n;i++){
		f[i][1]=1;
		for(int j=2;j<=m;j++){
			if(check(i,j))f[i][j]=f[i][j-1]+1;
			else f[i][j]=1;
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++)b[j]=f[j][i];
		solve();
	}
	printf("%d\n",ans);
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
 
#define mod 1000000007
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define long long long
inline int rei(){int x;cin>>x;return x;}
inline long rel(){long x;cin>>x;return x;}
inline string res(){string x;cin>>x;return x;}
//------------------------------------------------------- 
int DP[301*301*301];
void Calc(){
	string S = res();
	int K = rei();
	int N = S.length();
	for(int i=0;i<N;i++){
		for(int j=N-1;j>i;j--){
			for(int k=0;k<=K;k++){
				DP[(i+1)*301*301+j*301+k] = max(DP[i*301*301+j*301+k],DP[(i+1)*301*301+(j+1)*301+k]);
				if(S[i] == S[j]){
					DP[(i+1)*301*301+j*301+k] = max(DP[(i+1)*301*301+j*301+k],DP[i*301*301+(j+1)*301+k]+1);
				}
				else if(k != 0){
					DP[(i+1)*301*301+j*301+k] = max(DP[(i+1)*301*301+j*301+k],DP[i*301*301+(j+1)*301+k-1]+1);
				}
			}
		}
	}
	int ans = 1;
	for(int k=0;k<=K;k++){
		for(int i=0;i<N-1;i++){
			ans = max(DP[(i+1)*301*301+(i+1)*301+k]*2,ans);
		}
		for(int i=0;i<N-2;i++){
			ans = max(DP[(i+1)*301*301+(i+2)*301+k]*2+1,ans);
		}
	}
	cout << ans << endl;
}
int main(int argc,char** argv){
	ios::sync_with_stdio(false), cin.tie(0);
	cout.tie(0); Calc(); return 0;
}