#include <bits/stdc++.h>
using namespace std;
const int MN = 100005, MP = 20, MM = (1 << 17) + 6, inf = 1000000005,
          mod = 1000000007;
const long long INF = 1000000000000000005LL;
int n, p;
char used[MP][MP][MM], S[MN];
int forb[MM], moge[MM];
int ile[MP], mask[MP], lewy[MN][MP], ost[MP];
void dfs(int a, int b, int mask) {
  if (used[a][b][mask]) return;
  used[a][b][mask] = 1;
  forb[mask] = 1;
  for (int j = 0; j < p; ++j) {
    if (j == a || j == b) continue;
    if (!((mask >> j) & 1)) dfs(a, b, mask + (1 << j));
  }
}
void wywal(int a, int b) {
  for (int i = 0; i < n; ++i) {
    int cur = S[i] - 'a';
    if (cur == a && lewy[i][b] != -1) dfs(a, b, lewy[i][b]);
  }
}
int main() {
  scanf("%d%d", &n, &p);
  scanf("%s", S);
  for (int i = 0; i < p; ++i) ost[i] = inf;
  for (int i = n - 1; i >= 0; --i) {
    int cur = S[i] - 'a';
    for (int j = 0; j < p; ++j)
      if (ile[j] && ost[j] <= ost[cur])
        lewy[i][j] = mask[j];
      else
        lewy[i][j] = -1;
    ++ile[cur];
    for (int j = 0; j < p; ++j) mask[j] |= (1 << cur);
    mask[cur] = 0;
    ost[cur] = i;
  }
  for (int i = 0; i < p; ++i)
    for (int j = 0; j < p; ++j) {
      int v;
      scanf("%d", &v);
      if (!v) wywal(i, j);
    }
  int P = (1 << p), ans = n;
  moge[0] = 1;
  for (int i = 1; i < P; ++i) {
    if (forb[i]) continue;
    for (int j = 0; j < p; ++j)
      if (((i >> j) & 1) && moge[i - (1 << j)]) moge[i] = 1;
    if (moge[i]) {
      int cans = n;
      for (int j = 0; j < p; ++j)
        if ((i >> j) & 1) cans -= ile[j];
      ans = min(ans, cans);
    }
  }
  printf("%d", ans);
}
