Tofu Seller Jang Hongjun 3

Given an N by M grid of letter grades, pick disjoint horizontal or vertical dominoes to maximize the sum of pairwise prices from the price table.

Medium7Dynamic programmingBit manipulationMatrixNo attempts yetTime limit1sMemory limit512 MB

Problem

Jang Hongjun is a rather unusual tofu seller. He has a tofu board with height NN and width MM, and he cuts it into 2×12 \times 1 pieces to sell. Each cell of the board has its own grade, and the price of a 2×12 \times 1 piece depends on the grades of the two cells it contains. The price table is:

ABCDF
A108751
B86431
C74321
D53221
F11110

For example, a piece made of an A cell and a C cell costs 7, and a piece made of a D cell and a B cell costs 3. The table is symmetric, so the order of the two cells does not affect the price.

Every cell of the board has one of the grades A, B, C, D, or F. A piece consists of two horizontally adjacent cells or two vertically adjacent cells, and each cell can belong to at most one piece. Hongjun wants to cut the board so that the total price of the cut pieces is as large as possible. Single cells left over after cutting out the 2×12 \times 1 pieces are worth 0, so he throws them away.

Write a program that helps Hongjun find the maximum total price of the pieces he cuts out.

Input

The first line contains the height NN and the width MM of the tofu board. NN and MM are integers between 1 and 200, inclusive.

Each of the next NN lines contains MM characters with no spaces. Each character is the grade of the tofu in that cell and is one of A, B, C, D, and F.

Output

Print the maximum total price of the cut pieces on the first line.