#include <bits/stdc++.h>
using namespace std;
const int maxn = 1 << 17;
int n, p;
int has[maxn];
string s;
int v[maxn];
int g[17][17][maxn];
int a[17][17];
int vs[17];
int que[maxn];
int co[17];
int qua[maxn];
int getbit(int u, int p) { return 1 & (u >> p); }
int main() {
  cin >> n >> p;
  cin >> s;
  for (int i = (0); i < (n); i++) {
    int cur = s[i] - 'a';
    co[cur]++;
  }
  for (int mask = (0); mask < ((1 << p)); mask++) {
    for (int j = (0); j < (p); j++) {
      if (getbit(mask, j)) {
        has[mask] += co[j];
      }
    }
  }
  for (int i = (0); i < (p); i++) {
    for (int j = (0); j < (p); j++) cin >> a[i][j];
  }
  int mask = 0;
  for (int i = (0); i < (p); i++) {
    int isF = 1;
    char c = i + 'a';
    for (int j = (0); j < (n); j++) {
      if (s[j] == c) {
        if (!isF) {
          g[i][i][mask] = 1;
        }
        for (int k = (0); k < (p); k++) vs[k] = 0;
        mask = 0;
        isF = 0;
      } else {
        int cur = s[j] - 'a';
        if (!vs[cur]) {
          vs[cur] = 1;
          if (!isF) {
            g[i][cur][mask] = 1;
          }
          mask |= (1 << cur);
        }
      }
    }
  }
  for (int i = (0); i < (p); i++) {
    for (int j = (0); j < (p); j++) {
      int check = 0;
      if (a[i][j] == 0) {
        check = 1;
      }
      for (int mask = (0); mask < ((1 << p)); mask++) {
        if (g[i][j][mask]) {
          v[mask] |= check;
        }
        if (getbit(mask, i) || getbit(mask, j)) {
          continue;
        }
        for (int k = (0); k < (p); k++) {
          if (k == i || k == j) continue;
          if (getbit(mask, k)) continue;
          g[i][j][mask | (1 << k)] |= g[i][j][mask];
        }
      }
    }
  }
  int qsize = 0;
  que[qsize++] = 0;
  int ans = n;
  for (int i = (0); i < (qsize); i++) {
    int now = que[i];
    for (int j = (0); j < (p); j++) {
      if (!getbit(now, j) && !v[now ^ (1 << j)] && !qua[now ^ (1 << j)]) {
        qua[now ^ (1 << j)] = 1;
        que[qsize++] = now ^ (1 << j);
        ans = min(ans, n - has[now ^ (1 << j)]);
      }
    }
  }
  cout << ans;
  return 0;
}
