#include <bits/stdc++.h>
using namespace std;
bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); }
int dp2[(1 << 17) + 5];
bool vis2[(1 << 17) + 5];
int dp[(1 << 17) + 5][18];
int vis[(1 << 17) + 5][18], casio = 1;
int taken[(1 << 17) + 5];
int much[100005][19];
int store[18][(1 << 17) + 5], ch, cur, p;
int query(int st, int en) {
  if (st > en) return 0;
  int k = log2(en - st + 1);
  int v = much[st][k];
  int w = much[en - (1 << k) + 1][k];
  return w | v;
}
int dp_func(int mask, int pos) {
  if (pos == -1) {
    return taken[mask];
  }
  if (vis[mask][pos] == casio) return dp[mask][pos];
  vis[mask][pos] = casio;
  int ret = 0;
  ret |= dp_func(mask, pos - 1);
  if (check(mask, pos)) ret |= dp_func(mask ^ (1 << pos), pos - 1);
  return dp[mask][pos] = ret;
}
int big = -200000;
int mat[20][20], cnt[20];
int dp_func2(int mask) {
  if (mask == (1 << p) - 1) return 0;
  if (vis2[mask]) return dp2[mask];
  vis2[mask] = 1;
  for (int i = 0; i < p; i++) {
    if (check(mask, i)) continue;
    for (int j = i; j < p; j++) {
      if (check(mask, j)) continue;
      if (mat[i][j]) continue;
      if (check(store[i][mask], j)) return dp2[mask] = big;
    }
  }
  int ret = 0;
  for (int i = 0; i < p; i++) {
    if (check(mask, i)) continue;
    ret = max(ret, dp_func2(mask | (1 << i)) + cnt[i]);
  }
  return dp2[mask] = ret;
}
char str[300000];
int main() {
  int i, j, k, l, m, n;
  scanf("%d%d", &n, &ch);
  scanf("%s", str);
  p = ch;
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      scanf("%d", &mat[i][j]);
    }
  }
  for (int i = 0; str[i]; i++) {
    int f = str[i] - 97;
    cnt[f]++;
    much[i][0] |= (1 << f);
  }
  for (int j = 1; (1 << j) <= n; j++) {
    for (k = 0; k + (1 << (j - 1)) < n; k++) {
      much[k][j] = much[k][j - 1] | much[k + (1 << (j - 1))][j - 1];
    }
  }
  for (cur = 0; cur < p; cur++) {
    int last = -1;
    for (int i = 0; i < (1 << ch); i++) {
      taken[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      if (last != -1) taken[query(last + 1, i - 1)] |= (1 << (str[i] - 97));
      if (str[i] - 97 == cur) last = i;
    }
    last = -1;
    for (int i = n - 1; i >= 0; i--) {
      if (last != -1) taken[query(i + 1, last - 1)] |= (1 << (str[i] - 97));
      if (str[i] - 97 == cur) last = i;
    }
    casio++;
    for (int j = 0; j < (1 << ch); j++) {
      store[cur][j] = dp_func(j, p - 1);
    }
  }
  n -= dp_func2(0);
  cout << n << endl;
}
