#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXALPH = 17;
int n, nalph;
char s[MAXN + 1];
int allowed[MAXALPH][MAXALPH];
int nxt[26];
int adjmask[1 << MAXALPH][MAXALPH];
bool valid[1 << MAXALPH];
bool can[1 << MAXALPH];
int q[1 << MAXALPH], qhead, qtail;
int cnt[MAXALPH];
pair<int, int> o[MAXALPH];
int no;
int solve() {
  for (int i = (0); i < (nalph); ++i) nxt[i] = -1;
  for (int mask = (0); mask < (1 << nalph); ++mask)
    for (int j = (0); j < (nalph); ++j) adjmask[mask][j] = 0;
  for (int i = n - 1; i >= 0; --i) {
    no = 0;
    for (int j = (0); j < (nalph); ++j)
      if (nxt[j] != -1) o[no++] = make_pair(nxt[j], j);
    sort(o, o + no);
    int mask = 0, x = s[i] - 'a';
    for (int j = (0); j < (no); ++j) {
      int y = o[j].second;
      if (!allowed[x][y]) adjmask[mask][x] |= 1 << y;
      if (y == x) break;
      mask |= 1 << y;
    }
    nxt[x] = i;
  }
  for (int mask = (0); mask < (1 << nalph); ++mask)
    for (int x = (0); x < (nalph); ++x)
      for (int z = (0); z < (nalph); ++z)
        if (z != x) adjmask[mask | (1 << z)][x] |= adjmask[mask][x] & ~(1 << z);
  for (int mask = (0); mask < (1 << nalph); ++mask) {
    valid[mask] = true;
    for (int x = (0); x < (nalph); ++x)
      for (int y = (0); y < (nalph); ++y)
        if (!allowed[x][y] && (adjmask[mask][x] & (1 << y)) != 0)
          valid[mask] = false;
  }
  for (int mask = (0); mask < (1 << nalph); ++mask) can[mask] = false;
  qhead = qtail = 0;
  assert(valid[0]);
  can[0] = true, q[qhead++] = 0;
  while (qtail < qhead) {
    int mask = q[qtail++];
    for (int z = (0); z < (nalph); ++z)
      if (valid[mask | (1 << z)] && !can[mask | (1 << z)])
        can[mask | (1 << z)] = true, q[qhead++] = mask | (1 << z);
  }
  for (int i = (0); i < (nalph); ++i) cnt[i] = 0;
  for (int i = (0); i < (n); ++i) ++cnt[s[i] - 'a'];
  int ret = INT_MAX;
  for (int mask = (0); mask < (1 << nalph); ++mask)
    if (can[mask]) {
      int cur = 0;
      for (int x = (0); x < (nalph); ++x)
        if ((mask & (1 << x)) == 0) cur += cnt[x];
      ret = min(ret, cur);
    }
  return ret;
}
void run() {
  scanf("%d%d", &n, &nalph);
  scanf("%s", s);
  for (int i = (0); i < (nalph); ++i)
    for (int j = (0); j < (nalph); ++j) scanf("%d", &allowed[i][j]);
  printf("%d\n", solve());
}
int main() {
  run();
  return 0;
}
