#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int P = 18;
int n, p;
char second[N];
int cnt[P];
int a[P][P];
int bad[1 << P];
int tmp[1 << P];
int dp[1 << P];
int main() {
  scanf("%d%d", &n, &p);
  scanf("%s", second);
  for (int i = 0; i < n; i++) {
    second[i] -= 'a';
    cnt[second[i]]++;
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      if (a[i][j]) continue;
      int ptr = 0;
      while (ptr < n && second[ptr] != i && second[ptr] != j) ptr++;
      while (ptr < n) {
        int l = ptr;
        ptr++;
        int mask = 0;
        while (ptr < n && second[ptr] != i && second[ptr] != j)
          mask |= (1 << second[ptr++]);
        if (ptr != n && ((second[l] == i && second[ptr] == j) ||
                         (second[l] == j && second[ptr] == i))) {
          tmp[mask] = 1;
        }
      }
      for (int k = 0; k < 1 << p; k++) {
        if (k >> i & 1) continue;
        if (k >> j & 1) continue;
        for (int l = 0; l < p; l++) {
          if (l == i || l == j) continue;
          tmp[k | (1 << l)] |= tmp[k];
        }
      }
      for (int k = 0; k < 1 << p; k++) {
        bad[k] |= tmp[k];
        tmp[k] = 0;
      }
    }
  }
  dp[0] = 1;
  for (int mask = 0; mask < 1 << p; mask++) {
    if (dp[mask] == 0) continue;
    for (int j = 0; j < p; j++) {
      if (mask >> j & 1) continue;
      if (bad[mask | (1 << j)]) continue;
      dp[mask | (1 << j)] = 1;
    }
  }
  int ans = 0;
  for (int mask = 0; mask < 1 << p; mask++) {
    if (dp[mask] == 0) continue;
    int x = 0;
    for (int j = 0; j < p; j++)
      if (mask >> j & 1) x += cnt[j];
    if (ans < x) ans = x;
  }
  printf("%d\n", n - ans);
  return 0;
}
