Problem:
Connect

You are playing a solitaire puzzle called "Connect", which uses several letter tiles.

There are R × C empty cells.
For each i (1 ≤ i ≤ R), you must put a string si (1 ≤ |si| ≤ C) in the i-th row of the table, without changing the letter order.
In other words, you choose an integer sequence {aj} such that
1 ≤ a1 < a2 < ... < a|si| ≤ C
, and put the j-th character of the string si in the aj-th column (1 ≤ j ≤ |si|).

For example, when C = 8 and si = "ICPC", you can put si like followings.

I_C_P_C_
ICPC____
_IC___PC

'_' represents an empty cell.

For each non-empty cell x, you get a point equal to the number of adjacent cells which have the same character as x.
Two cells are adjacent if they share an edge.

Calculate the maximum total point you can get.

Input

The first line contains two integers R and C (1 ≤ R ≤ 128, 1 ≤ C ≤ 16).

Then R lines follow, each of which contains si
(1 ≤ |si| ≤ C).
All characters of si are uppercase letters.

Output

Output the maximum total point in a line.

Sample Input 1

2 4
ACM
ICPC

Output for the Sample Input 1

2

Sample Input 2

2 9
PROBLEMF
CONNECT

Output for the Sample Input 2

6

Sample Input 3

4 16
INTERNATIONAL
COLLEGIATE
PROGRAMMING
CONTEST

Output for the Sample Input 3

18

Solution in C++ (judge verdict: Time Limit Exceeded)

#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

typedef long long ll;
int R,C;
string ss[201];
int dp[2][1<<16][20];
const int INF=1<<30;
int bitPos[1<<16][17];
int bitCnt[1<<16][17];
int main(){
  for(int mask=0;mask<(1<<16);mask++){
    int a=0;
    for(int i=0;i<16;i++){
      if(((mask>>i)&1))bitPos[mask][i]=a++;
      else bitPos[mask][i]=-1;
      bitCnt[mask][i+1]=bitCnt[mask][i]+((mask>>i)&1);
    }
  }
  cin>>R>>C;
  for(int i=0;i<R;i++)cin>>ss[i];
  for(int i=R*C;i>=0;i--){
    int cur=i%2;
    int nxt=cur^1;
    const int cy=i/C;
    const int cx=i%C;
    for(int mask=0;mask<(1<<C);mask++){
      for(int k=0;k<16;k++){
	int res=-INF;
	if(i==R*C)res=0;
	else{
	  const int cnt=k;
	  const int ct=bitCnt[mask][cx];
	  if(ct<(int)ss[cy].size()){
	    int nmask=mask|(1<<cx);
	    int add=0;
	    if(cx-1>=0&&bitPos[mask][cx-1]!=-1
	       &&bitPos[mask][cx-1]<(int)ss[cy].size()
	       &&bitPos[mask][cx-1]+1<(int)ss[cy].size()
	       &&ss[cy][bitPos[mask][cx-1]]==ss[cy][bitPos[mask][cx-1]+1]){
	      add+=2;
	    }
	    if(cy-1>=0&&bitPos[mask][cx]!=-1
	       &&cnt<(int)ss[cy-1].size()
	       &&bitCnt[mask][cx]<(int)ss[cy].size()
	       &&ss[cy-1][cnt]==ss[cy][bitCnt[mask][cx]]){
	      add+=2;
	    }
	    if(cx!=C-1||ct+1==(int)ss[cy].size()){
	      if(cx==C-1)res=max(res,dp[nxt][nmask][0]+add);
	      else res=max(res,dp[nxt][nmask][k+((mask>>cx)&1)]+add);
	    }
	  }
	  {
	    int nmask=mask&~(1<<cx);
	    if(cx!=C-1||ct==(int)ss[cy].size()){
	      if(cx==C-1)res=max(res,dp[nxt][nmask][0]);
	      else res=max(res,dp[nxt][nmask][k+((mask>>cx)&1)]);
	    }
	  }
	}
	dp[cur][mask][k]=res;
      }
    }
  }
  cout<<dp[0][0][0]<<endl;
  return 0;
}