Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official.
×

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

The problem statement has recently been changed. View the changes.

×
F. Crisp String

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputYou are given a string of length $$$n$$$. Each character is one of the first $$$p$$$ lowercase Latin letters.

You are also given a matrix $$$A$$$ with binary values of size $$$p \times p$$$. This matrix is symmetric ($$$A_{ij} = A_{ji}$$$). $$$A_{ij} = 1$$$ means that the string can have the $$$i$$$-th and $$$j$$$-th letters of Latin alphabet adjacent.

Let's call the string crisp if all of the adjacent characters in it can be adjacent (have 1 in the corresponding cell of matrix $$$A$$$).

You are allowed to do the following move. Choose any letter, remove all its occurrences and join the remaining parts of the string without changing their order. For example, removing letter 'a' from "abacaba" will yield "bcb".

The string you are given is crisp. The string should remain crisp after every move you make.

You are allowed to do arbitrary number of moves (possible zero). What is the shortest resulting string you can obtain?

Input

The first line contains two integers $$$n$$$ and $$$p$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le p \le 17$$$) — the length of the initial string and the length of the allowed prefix of Latin alphabet.

The second line contains the initial string. It is guaranteed that it contains only first $$$p$$$ lowercase Latin letters and that is it crisp. Some of these $$$p$$$ first Latin letters might not be present in the string.

Each of the next $$$p$$$ lines contains $$$p$$$ integer numbers — the matrix $$$A$$$ ($$$0 \le A_{ij} \le 1$$$, $$$A_{ij} = A_{ji}$$$). $$$A_{ij} = 1$$$ means that the string can have the $$$i$$$-th and $$$j$$$-th letters of Latin alphabet adjacent.

Output

Print a single integer — the length of the shortest string after you make arbitrary number of moves (possible zero).

Examples

Input

7 3 abacaba 0 1 1 1 0 0 1 0 0

Output

7

Input

7 3 abacaba 1 1 1 1 0 0 1 0 0

Output

0

Input

7 4 bacadab 0 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0

Output

5

Input

3 3 cbc 0 0 0 0 0 1 0 1 0

Output

0

Note

In the first example no letter can be removed from the initial string.

In the second example you can remove letters in order: 'b', 'c', 'a'. The strings on the intermediate steps will be: "abacaba" $$$\rightarrow$$$ "aacaa" $$$\rightarrow$$$ "aaaa" $$$\rightarrow$$$ "".

In the third example you can remove letter 'b' and that's it.

In the fourth example you can remove letters in order 'c', 'b', but not in the order 'b', 'c' because two letters 'c' can't be adjacent.

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/22/2021 21:56:32 (i1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|