#include <bits/stdc++.h>
using namespace std;
int N, P;
string S;
int A[17][17];
int nex[18];
int num[18];
vector<int> ngpat[1 << 17];
int ok[1 << 17];
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N >> P >> S;
  for (i = 0; i < (P); i++) nex[i] = N;
  for (auto& c : S) {
    c -= 'a';
    num[c]++;
  }
  for (y = 0; y < (P); y++)
    for (x = 0; x < (P); x++) cin >> A[y][x];
  for (i = N - 1; i >= 0; i--) {
    vector<pair<int, int>> V;
    for (j = 0; j < (P); j++)
      if (nex[j] < N) V.push_back({nex[j], j});
    sort((V.begin()), (V.end()));
    int fixed = (1 << P) - 1 - (1 << S[i]);
    int mat = 1 << S[i];
    for (auto& v : V) {
      if (v.second != S[i]) {
        fixed ^= 1 << (v.second);
        mat ^= 1 << (v.second);
      }
      if (A[S[i]][v.second] == 0) ngpat[fixed].push_back(mat);
      if (v.second == S[i]) break;
      mat ^= 1 << (v.second);
    }
    nex[S[i]] = i;
  }
  int mask;
  for (mask = 0; mask < (1 << P); mask++) ok[mask] = 1;
  for (mask = 0; mask < (1 << P); mask++) {
    sort((ngpat[mask].begin()), (ngpat[mask].end()));
    ngpat[mask].erase(unique((ngpat[mask].begin()), (ngpat[mask].end())),
                      ngpat[mask].end());
    for (int submask = mask; submask >= 0; submask--) {
      submask &= mask;
      for (auto& p : ngpat[mask]) ok[submask | p] = 0;
    }
  }
  int ma = 0;
  for (i = 0; i < (P); i++) ma += num[i];
  for (mask = (1 << P) - 2; mask >= 0; mask--)
    if (ok[mask]) {
      ok[mask] = 0;
      for (i = 0; i < (P); i++)
        if ((mask & (1 << i)) == 0 && ok[mask | (1 << i)]) ok[mask] = 1;
      if (ok[mask]) {
        int sum = 0;
        for (i = 0; i < (P); i++)
          if (mask & (1 << i)) sum += num[i];
        ma = min(ma, sum);
      }
    }
  cout << ma << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}
