#include <bits/stdc++.h>
using namespace std;
int vis[263000];
int val[18][18];
string s;
long int n, p;
long int cnt[18];
long int ans;
int res[263000];
int hogya[131073][18][18];
void cal(long int x) {
  vis[x] = 1;
  long int tot = 0;
  for (int i = 1; i <= p; ++i) {
    if ((x & (1 << i)) != 0) tot += cnt[i];
  }
  ans = max(ans, tot);
  for (int i = 1; i <= p; ++i) {
    if (cnt[i] == 0) continue;
    long int tmp = x | (1 << i);
    if (vis[tmp] == 1 || res[tmp] == 1) continue;
    cal(tmp);
  }
}
void all(long int x, int y, int z) {
  if (hogya[x >> 1][y][z] == 1) return;
  res[x] = 1;
  hogya[x >> 1][y][z] = 1;
  for (int i = 1; i <= p; ++i) {
    if (i == y || i == z) continue;
    all(x | (1 << i), y, z);
  }
}
void fn(int x, int y) {
  int tmp;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'a' - 1 + x) {
      tmp = i;
      break;
    }
  }
  long int temp = 0;
  for (int i = tmp + 1; i < n; ++i) {
    if ((s[i] != 'a' - 1 + x) && (s[i] != 'a' - 1 + y))
      temp |= (1 << (s[i] - 'a' + 1));
    else if ((s[i] == 'a' - 1 + y)) {
      res[temp] = 1;
      all(temp, x, y);
      temp = 0;
      if (x == y) continue;
      i++;
      while (i < n && s[i] != 'a' - 1 + x) i++;
      i--;
    } else
      temp = 0;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> p;
  cin >> s;
  for (long int i = 0; i < n; ++i) cnt[s[i] - 'a' + 1]++;
  for (int i = 1; i <= p; ++i) {
    for (int j = 1; j <= p; ++j) cin >> val[i][j];
  }
  for (int i = 1; i <= p; ++i) {
    for (int j = 1; j <= p; ++j) {
      if (val[i][j] == 1) continue;
      fn(i, j);
    }
  }
  cal(0);
  cout << n - ans;
  return 0;
}
