/*alurquiza*/
//1117E
#include <bits/stdc++.h>

using namespace std;

const int SIZE = 17;
bool MA[SIZE][SIZE];
bool Mk[1 << SIZE],DP[1 << SIZE];
bool bad[SIZE][SIZE][1 << SIZE];

int ascii(char l){
    return l - 'a';
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

//    freopen(".in","r",stdin);

    int N,P;
    cin >> N >> P;

    string S;
    cin >> S;

    vector<int> F(26);
    for(int i = 0;i < N;i++)
        F[ascii(S[i])]++;

    for(int i = 0;i < P;i++)
        for(int j = 0;j < P;j++)
            cin >> MA[i][j];

    fill(Mk,Mk + (1 << P),1);

    for(int i = 0;i < P;i++){
        for(int j = 0;j < P;j++){
            if(MA[i][j])
                continue;
            int pi = -1,pj = -1;
            int mask = 0;
            for(int k = 0;k < N;k++){
                if(ascii(S[k]) == i){
                    if(i == j && pi != -1){
                        bad[i][j][mask] = 1;
                        mask = 0;
                    }
                    pi = k;
                    pj = -1;
                }
                else if(ascii(S[k]) == j){
                    if(pi != -1 && pj == -1){
                        bad[i][j][mask] = 1;
                        mask = 0;
                    }
                    pi = pj = -1;
                }
                else
                    mask |= (1 << ascii(S[k]));
            }
        }
    }

    for(int i = 0;i < P;i++){
        for(int j = 0;j < P;j++){
            if(MA[i][j])
                continue;
            for(int mask = 0;mask < (1 << P);mask++){
                int mask_ij = (1 << i) | (1 << j);
                if(bad[i][j][mask] == 0 || ((mask_ij & mask)))
                    continue;
                Mk[mask] = false;
                for(int k = 0;k < P;k++){
                    if(k == i || k == j || ((1 << k) & mask)){
                        continue;
                    }
                    bad[i][j][mask | (1 << k)] = 1;
                }
            }
        }
    }

    DP[0] = true;
    for(int mask = 0;mask < (1 << P);mask++){
        if(DP[mask]){
            for(int k = 0;k < P;k++){
                if(!((1 << k) & mask)){
                    DP[mask ^ (1 << k)] = Mk[mask ^ (1 << k)] != 0 ? 1 : 0;
                }
            }
        }
    }

    int sol = N;
    for(int mask = 0;mask < (1 << P);mask++){
        if(DP[mask] == 1){
            int temp = 0;
            for(int k = 0;k < P;k++)
                if(!((1 << k) & mask))
                    temp += F[k];

            sol = min(sol,temp);
        }
    }

    cout << sol << '\n';

    return 0;
}
