#include<bits/stdc++.h>
#define dro first
#define adg second.first
#define mog second.second
#define ll long long
using namespace std;

const int N = 500001;

int n,r,pos,l,x,U,D,S;
 
long long ans;
int K;

string s;

int fix[100],fix1[100],A[100],B[100];

int dp[305][305][305];

int main(){
    
    cin>>s;
    
    n=s.size();
    
    cin>>K;
    
    for(int j=0;j<n;j++)
    for(int l=0;l+j<n;l++)
    for(int k=0;k<=K;k++){
        
        int r = l+j;
        
        if(l==r){
            dp[l][r][k] = 1;
            continue;
            //dp[l][r][1] = 1
        }

        if(l+1==r){
            if(k==0){
                dp[l][r][k] = 1;
                if(s[l] == s[r]) dp[l][r][k] = 2;
            } else {
                dp[l][r][k] = 2;
            }
            
            continue;
        }
        
        dp[l][r][k] = max( dp[l+1][r][k], dp[l][r-1][k]);
        //cout<<dp[l+1][r][k]<<" "<<l<<" "<<r<<" "<<k<<endl;
        //cout<<dp[l][r-1][k]<<" "<<l<<" "<<r<<" "<<k<<endl;
        if(s[l]==s[r])
            dp[l][r][k] = max( dp[l][r][k], dp[l+1][r-1][k] + 2);
        else {
            if(k-1>=0)
            dp[l][r][k] = max( dp[l][r][k], dp[l+1][r-1][k-1] + 2);
        }

        //cout<<dp[l][r][k]<<" "<<l<<" "<<r<<" "<<k<<endl;
    }
    
    cout<<dp[0][n-1][K]<<endl;
}




#include<bits/stdc++.h>
using namespace std;

inline void upmax(short &x,const short &y){if(y>x) x=y;}

const int N=105;
short f[N][N][N][N];
short sum1[N][N],sum2[N][N];
int n,m,tx,ty;

void gao()
{
    int limu=tx-1,limd=n-tx,liml=ty-1,limr=m-ty;
    for(int u=0;u<=limu;u++)
        for(int d=0;d<=limd;d++)
            for(int l=0;l<=liml;l++)
                for(int r=0;r<=limr;r++)
                {
                    int L=max(ty-l,r+1),R=min(ty+r,m-l);
                    if(L<=R)
                    {
                        upmax(f[u+1][d][l][r],f[u][d][l][r]+(tx-u-1>=d+1?sum1[tx-u-1][R]-sum1[tx-u-1][L-1]:0));
                        upmax(f[u][d+1][l][r],f[u][d][l][r]+(tx+d+1<=n-u?sum1[tx+d+1][R]-sum1[tx+d+1][L-1]:0));
                    }
                    L=max(tx-u,d+1),R=min(tx+d,n-u);
                    if(L<=R)
                    {
                        upmax(f[u][d][l+1][r],f[u][d][l][r]+(ty-l-1>=r+1?sum2[R][ty-l-1]-sum2[L-1][ty-l-1]:0));
                        upmax(f[u][d][l][r+1],f[u][d][l][r]+(ty+r+1<=m-l?sum2[R][ty+r+1]-sum2[L-1][ty+r+1]:0));
                    }
                }
    printf("%d\n",f[limu][limd][liml][limr]);
}

int main()
{
    static char s[105];
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s+1);
        for(int j=1;j<=m;j++)
        {
            if(s[j]=='E') tx=i,ty=j;
            sum1[i][j]=sum1[i][j-1]+(s[j]=='o');
            sum2[i][j]=sum2[i-1][j]+(s[j]=='o');
        }
    }
    gao();
}