Under construction

Still being built. Things may change

Connect

Time limit5sMemory limit512 MB

Summary
Place each string in its row in order across the C columns so that the number of adjacent equal-letter cell pairs is maximized.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, String
Solved
No attempts yet

Problem

Connect is a solitaire puzzle that uses several letter tiles.

The table has R×CR \times C empty cells. For each ii (1≤i≤R)(1 \le i \le R), you must put a string sis_i (1≤∣si∣≤C)(1 \le |s_i| \le C) in the ii-th row of the table. The letter order cannot change. In other words, choose an integer sequence {aj}\{a_j\} with 1≤a1<a2<⋯<a∣si∣≤C1 \le a_1 < a_2 < \cdots < a_{|s_i|} \le C, and put the jj-th character of sis_i in column aja_j.

For example, when C=8C = 8 and sis_i is "ICPC", you can place it like this:

I_C_P_C_
ICPC____
_IC___PC

'_' stands for an empty cell.

For each non-empty cell xx, you get one point for every adjacent cell that has the same character as xx. Two cells are adjacent if they share an edge. Find the maximum total point you can get.

Input

The first line contains two integers RR and CC (1≤R≤128,1≤C≤16)(1 \le R \le 128, 1 \le C \le 16). Then RR lines follow, each containing a string sis_i (1≤∣si∣≤C)(1 \le |s_i| \le C). All characters of sis_i are uppercase letters.

Output

Print the maximum total point in one line.

Examples3

  1. Example 1

    Input
    2 4
    ACM
    ICPC
    
    Expected output
    2
    
  2. Example 2

    Input
    2 9
    PROBLEMF
    CONNECT
    
    Expected output
    6
    
  3. Example 3

    Input
    4 16
    INTERNATIONAL
    COLLEGIATE
    PROGRAMMING
    CONTEST
    
    Expected output
    18