#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
inline char readch() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n') ch = getchar();
  return ch;
}
inline string readstr() {
  char ch = getchar();
  string ans = "";
  while (ch == ' ' || ch == '\n') ch = getchar();
  while (ch != ' ' && ch != '\n') {
    ans += ch;
    ch = getchar();
  }
  return ans;
}
inline void chmax(int &x, int y) { x = max(x, y); }
inline void chmin(int &x, int y) { x = min(x, y); }
const int MAXN = 100005;
const int MAXM = 150000;
int n, p;
int a[MAXN], cnt[MAXN];
bool ok[20][20];
int nex[MAXN][20];
int rua;
bool vis[MAXM][20][20];
bool f[MAXM];
bool dp[MAXM];
int ans = 0;
void print(int x) {
  int cnt = p;
  while (x) {
    cout << x % 2;
    x /= 2;
    cnt--;
  }
  while (cnt--) cout << 0;
  cout << ' ';
}
void foo(int x, int y, int sta) {
  if (vis[sta][x][y] || sta == rua) return;
  vis[sta][x][y] = f[sta] = true;
  for (int i = 0; i < p; i++)
    if (i != x && i != y && !(sta & (1 << i))) foo(x, y, sta | (1 << i));
}
signed main() {
  n = read();
  p = read();
  rua = (1 << p) - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = readch() - 'a';
    cnt[a[i]]++;
    for (int j = i - 1; j >= 1; j--) {
      if (!nex[j][a[i]])
        nex[j][a[i]] = i;
      else
        break;
    }
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) ok[i][j] = read();
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < p; j++) {
      int r = nex[i][j];
      if (!r || (nex[i][a[i]] < r && nex[i][a[i]]) || ok[a[i]][j]) continue;
      int sta = 0;
      for (int k = 0; k < p; k++)
        if (nex[i][k] && nex[i][k] < r) sta |= (1 << k);
      foo(a[i], j, sta);
    }
  }
  dp[0] = true;
  for (int i = 0; i <= rua; i++) {
    if (!dp[i]) continue;
    int tmp = 0;
    for (int j = 0; j < p; j++)
      if (i & (1 << j)) tmp += cnt[j];
    ans = max(ans, tmp);
    for (int j = 0; j < p; j++) {
      if (i & (1 << j)) continue;
      int tar = i | (1 << j);
      if (!f[tar]) dp[tar] = true;
    }
  }
  cout << (n - ans) << endl;
  return 0;
}
