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

using vi=vector<int>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
void cmax(int &lhs,int rhs){
    lhs=max(lhs,rhs);
}
const int INF=1e5;
int main(){
    int r,c;
    cin>>r>>c;
    
    vector<string> s(r+1);
    for(int i=0;i<r;i++) cin>>s[i+1];
    vvi dp(c+1,vi(1<<c,-INF));
    dp[0][0]=0;
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            for(int bit=0;bit<(1<<c);bit++){
                int upperc=__builtin_popcount((((1<<(c-j))-1)&bit));
                int posu=(int)(s[i].size())-upperc;
                int posn=(int)(__builtin_popcount(bit))-upperc;
                if(posu<0 || posu>s[i].size() || posn<0 || posn>s[i+1].size()) continue;
                int nextbit=bit>>1;
                cmax(dp[j+1][nextbit],dp[j][bit]);
                if(posn<s[i+1].size()){
                    nextbit|=(1<<(c-1));
                    if((bit&1) && s[i][posu]==s[i+1][posn]){
                        cmax(dp[j+1][nextbit],dp[j][bit]+1);
                    }
                    else{
                        cmax(dp[j+1][nextbit],dp[j][bit]);
                    }
                }
            }
        }
        for(int bit=0;bit<(1<<c);bit++){
            if(__builtin_popcount(bit)!=s[i+1].size()) dp[c][bit]=-INF;
            else{
                int idx=0;
                for(int j=0;j<c;j++){
                    if(((bit>>j)&1) && ((bit>>(j+1))&1) && s[i+1][idx]==s[i+1][idx+1]){
                        dp[c][bit]++;
                    }                    
                    if((bit>>j)&1) idx++;
                }
            }
            dp[0][bit]=dp[c][bit];
        }
        for(int j=1;j<=c;j++){
            fill(dp[j].begin(),dp[j].end(),-INF);
        }
    }
    cout<<*max_element(dp[0].begin(),dp[0].end())*2<<endl;
    return 0;
}
