#include <bits/stdc++.h>
using namespace std;
int n, m, ans, num[150010], cnt[150010], mm[20][20];
char str[150010];
bool gg[150010], vis[150010];
void dfs(int now) {
  int i;
  vis[now] = 1;
  for (i = 1; i <= m; i++) {
    if ((1 << (i - 1)) & now) {
      if (vis[now ^ (1 << (i - 1))]) continue;
      dfs(now ^ (1 << (i - 1)));
      gg[now] |= gg[now ^ (1 << (i - 1))];
    }
  }
}
inline void work(int u, int v) {
  int i, j, t;
  for (i = 1; i <= n;) {
    if (num[i] != u) {
      i++;
      continue;
    }
    t = 0;
    for (j = i + 1; j <= n; j++) {
      if (num[j] == u || num[j] == v) break;
      t |= (1 << (num[j] - 1));
    }
    if (j > n) break;
    if (num[j] == v) {
      i = j + 1;
      gg[t] = 1;
    }
    if (num[j] == u) i = j;
  }
  memset(vis, 0, sizeof(vis));
  if (u != v)
    dfs(((1 << m) - 1) ^ (1 << (u - 1)) ^ (1 << (v - 1)));
  else
    dfs(((1 << m) - 1) ^ (1 << (u - 1)));
}
void Dfs(int now) {
  ans = min(ans, cnt[((1 << m) - 1) ^ now]);
  int i;
  for (i = 1; i <= m; i++) {
    if ((1 << (i - 1)) & now) continue;
    if (gg[now | (1 << (i - 1))]) continue;
    Dfs(now | (1 << (i - 1)));
  }
}
int main() {
  int i, j;
  cin >> n >> m;
  ans = n;
  scanf("%s", str + 1);
  for (i = 1; i <= n; i++) {
    num[i] = str[i] - 'a' + 1;
    cnt[(1 << (num[i] - 1))]++;
  }
  for (i = 1; i < (1 << m); i++) {
    for (j = 1; j <= m; j++) {
      if ((1 << (j - 1)) & i) {
        cnt[i] = cnt[i ^ (1 << (j - 1))] + cnt[(1 << (j - 1))];
        break;
      }
    }
  }
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%d", &mm[i][j]);
    }
  }
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= m; j++) {
      if (mm[i][j]) continue;
      work(i, j);
    }
  }
  Dfs(0);
  cout << ans;
}
