Moortal Cowmbat
Bessie has been playing the popular fighting game Moortal Cowmbat for a long time. However, the game developers have recently rolled out an update that is forcing Bessie to change her play style.
The game uses buttons, labeled with the first lowercase letters. Bessie's favorite combo of moves in the game is a string of length , consisting of button presses. Due to the most recent update, every combo must now be made from a series of "streaks," where a streak is defined as a series of the same button pressed at least times in a row. Bessie wants to modify her favorite combo to produce a new combo of the same length , but composed of streaks of button presses to satisfy with the new rules.
It takes days for Bessie to train herself to press button instead of button at any specific location in her combo (i.e., it costs days to change a single specific letter in from to ). Note that it might take less time to switch from using button to an intermediate button and then from button to button rather than directly from to (more generally, there may be a path of changes starting with and ending with that provides the best overall cost for switching from button ultimately to button ).
Help Bessie determine the smallest possible number of days she needs to create a combo that meets the new requirements.
Input
The first line contains three integers: , and . The second line contains the string . The next lines contain an matrix of values , where is an integer in the range and for all .
Output
Print a single number representing the minimum number of days Bessie needs to change her combo into one that satisfies the new requirements.
Examples
In the optimal solution for this example, change the into , the into , and both 's into 's. This will take days, and the final combo string will be .