import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FCrispString solver = new FCrispString();
        solver.solve(1, in, out);
        out.close();
    }

    static class FCrispString {
        int n;
        int p;
        char[] arr;
        boolean[][] valid;
        int[] memo;
        boolean[][][] invalidMsks;
        int[] count;
        boolean[] inValidMsks2;

        public void readInput(Scanner sc) {
            n = sc.nextInt();
            p = sc.nextInt();
            arr = sc.next().toCharArray();
            valid = new boolean[p][p];
            for (int i = 0; i < p; i++)
                for (int j = 0; j < p; j++)
                    valid[i][j] = sc.nextInt() == 1;
        }

        public void solve(int testNumber, Scanner sc, PrintWriter pw) {
            int tc = 1;
            while (tc-- > 0) {
                readInput(sc);
                invalidMsks = new boolean[p][p][1 << p];
                count = new int[26];
                for (int i = 0; i < n; i++)
                    count[arr[i] - 'a']++;
                int[] prev = new int[p];
                Arrays.fill(prev, -1);
                for (int i = 0; i < n; i++) {
                    int cur = arr[i] - 'a';
                    for (int j = 0; j < p; j++) {
                        if (prev[j] == -1 || valid[cur][j])
                            continue;
                        int msk = 0;
                        for (int k = 0; k < p; k++) {
                            if (prev[k] > prev[j])
                                msk |= 1 << k;
                        }
                        invalidMsks[cur][j][msk] = true;
                    }
                    prev[cur] = i;
                }
                inValidMsks2 = new boolean[1 << p];
                for (int l = 0; l < p; l++)
                    for (int k = 0; k < p; k++)
                        for (int i = 0; i < 1 << p; i++) {
                            if ((i & 1 << l) == 0 && (i & 1 << k) == 0)
                                inValidMsks2[i] |= invalidMsks[l][k][i];
                            for (int j = 0; j < p; j++) {
                                if (j != k && j != l)
                                    invalidMsks[l][k][i | 1 << j] |= invalidMsks[l][k][i];
                            }
                        }
                memo = new int[1 << p];
                Arrays.fill(memo, (int) 1e9);
                memo[(1 << p) - 1] = 0;
                for (int msk = (1 << p) - 2; msk >= 0; msk--) {
                    int cur = 0;
                    for (int i = 0; i < p; i++)
                        if ((msk & 1 << i) == 0)
                            cur += count[i];
                    for (int i = 0; i < p; i++) {
                        if ((msk & 1 << i) == 0 && !inValidMsks2[msk | 1 << i])
                            cur = Math.min(cur, memo[msk | 1 << i]);
                    }
                    memo[msk] = cur;
                }
                pw.print(memo[0]);
            }
        }

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() {
            try {
                while (st == null || !st.hasMoreTokens())
                    st = new StringTokenizer(br.readLine());
                return st.nextToken();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

