#include <bits/stdc++.h>
using namespace std;
const int N_MAX = 1e5, P = 17;
int n, p, a[N_MAX], A[P][P], cnt[P];
string s;
bool wrong[P][1 << P], bad[1 << P], ok[1 << P];
vector<int> k_sets[P + 1];
void supersets(bool B[1 << P], int x, int y) {
  for (int k = 0; k < p; k++) {
    for (int b : k_sets[k]) {
      if (B[b]) {
        for (int i = 0; i < p; i++) {
          if (i != x && i != y) {
            B[b | (1 << i)] = true;
          }
        }
      }
    }
  }
}
int main() {
  cin >> n >> p;
  cin >> s;
  for (int i = 0; i < n; i++) a[i] = s[i] - 'a';
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> A[i][j];
  for (int b = 0; b < (1 << p); b++) {
    int c = 0, bb = b;
    while (bb) {
      if (bb % 2) c++;
      bb /= 2;
    }
    k_sets[c].push_back(b);
  }
  for (int i = 0; i < p; i++) {
    for (int j = i; j < p; j++) {
      if (A[i][j]) continue;
      for (int b = 0; b < (1 << p); b++) bad[b] = false;
      int b = 0;
      bool last[2] = {false, false};
      for (int k = 0; k < n; k++) {
        if (a[k] == i) {
          if (last[j] || (i == j && last[i])) bad[b] = true;
          last[i] = true;
          if (i != j) last[j] = false;
          b = 0;
        } else if (a[k] == j) {
          if (last[i]) bad[b] = true;
          last[i] = false;
          last[j] = true;
          b = 0;
        } else
          b |= (1 << a[k]);
      }
      supersets(bad, i, j);
      for (int bb = 0; bb < (1 << p); bb++) {
        wrong[i][bb] |= bad[bb];
        wrong[j][bb] |= bad[bb];
      }
    }
  }
  ok[0] = true;
  for (int k = 0; k < p; k++) {
    for (int b : k_sets[k]) {
      if (!ok[b]) continue;
      for (int i = 0; i < p; i++) {
        if (b & (1 << i)) continue;
        bool can_add = true;
        for (int j = 0; j < p; j++) {
          if (!(b & (1 << j)) && i != j && wrong[j][b | (1 << i)]) {
            can_add = false;
            break;
          }
        }
        if (can_add) ok[b | (1 << i)] = true;
      }
    }
  }
  for (int i = 0; i < n; i++) cnt[a[i]]++;
  int sol = 0;
  for (int b = 0; b < (1 << p); b++) {
    if (!ok[b]) continue;
    int act = 0;
    for (int i = 0; i < p; i++)
      if (b & (1 << i)) act += cnt[i];
    sol = max(sol, act);
  }
  cout << n - sol << endl;
  return 0;
}
