Problem: 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.

Examples

Input

2 4
ACM
ICPC


Output

2


Input

2 9
PROBLEMF
CONNECT


Output

6


Input

4 16
INTERNATIONAL
COLLEGIATE
PROGRAMMING
CONTEST


Output

18

Solution (in CPP):

Solution:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
const int INF = 1000000000;

int popcount[1 << 16];
int dp[2][16][1 << 16];
vector<int> patterns[17];

int main(){
	int R, C;
	cin >> R >> C;
	vector<string> words(R + 2);
	for(int i = 1; i <= R; ++i){ cin >> words[i + 1]; }
	for(int i = 0; i < (1 << C); ++i){ popcount[i] = __builtin_popcount(i); }
	for(int i = 0; i < 2; ++i){
		for(int j = 0; j < C; ++j){
			for(int k = 0; k < (1 << C); ++k){ dp[i][j][k] = -INF; }
		}
	}
	dp[1][C - 1][0] = 0;
	const int all_mask = (1 << C) - 1;
	for(int i = 1; i < R + 2; ++i){
		int crow = i & 1, nrow = 1 - crow;
		for(int j = 0; j < C; ++j){
			for(int k = 0; k < (1 << C); ++k){
				int dp_cur = dp[crow][j][k];
				dp[crow][j][k] = -INF;
				int cur = k & ((1 << j) - 1), prev = k >> j;
				int pcc = popcount[cur], pcp = popcount[prev];
				if(j == 0 && pcp != words[i - 1].size()){ continue; }
				if(pcc > words[i].size() || pcp > words[i - 1].size()){ continue; }
				int nr = (j == C - 1 ? nrow : crow), nc  = (j + 1) % C, nk = (k << 1) & all_mask;
				dp[nr][nc][nk] = max(dp[nr][nc][nk], dp_cur);
				if(pcc < words[i].size()){
					int s = dp_cur;
					char c = words[i][pcc];
					if((k & 1) && words[i][pcc - 1] == c){ s += 2; }
					if((k & (1 << (C - 1))) && words[i - 1][words[i - 1].size() - pcp] == c){ s += 2; }
					dp[nr][nc][nk | 1] = max(dp[nr][nc][nk | 1], s);
				}
			}
		}
	}
	int answer = -INF;
	for(int i = 0; i < (1 << C); ++i){
		if(popcount[i] != words.back().size()){ continue; }
		answer = max(answer, dp[R & 1][0][i]);
	}
	cout << answer << endl;
	return 0;
}