#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int M = 17;
char s[N];
vector<int> pos[M];
int A[M][M];
int dp[N][20];
int n, m;
void init() {
  for (int i = 1; i <= n; i++) dp[i][0] = (1 << (s[i] - 'a'));
  for (int i = 1; i < 20; i++) {
    for (int j = 1; j + (1 << i) - 1 <= n; j++) {
      dp[j][i] = dp[j][i - 1] | dp[j + (1 << (i - 1))][i - 1];
    }
  }
}
int query(int l, int r) {
  if (l > r) return 0;
  int k = 0;
  while ((1 << (k + 1)) <= r - l + 1) k++;
  return dp[l][k] | dp[r - (1 << k) + 1][k];
}
void dfs(bool* vis, int s) {
  if (vis[s]) return;
  vis[s] = 1;
  for (int i = 0; i < m; i++) {
    if ((s >> i) & 1) continue;
    dfs(vis, s | (1 << i));
  }
}
bool vis[M][M][1 << M];
bool f[1 << M], ans[1 << M];
void solve() {
  scanf("%d%d", &n, &m);
  scanf("%s", s + 1);
  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++) scanf("%d", &A[i][j]);
  for (int i = 0; i < m; i++) pos[i].push_back(0);
  for (int i = 1; i <= n; i++) pos[s[i] - 'a'].push_back(i);
  for (int i = 0; i < m; i++) pos[i].push_back(n + 1);
  init();
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= i; j++) {
      if (A[i][j]) continue;
      for (auto u : pos[i]) {
        if (u == 0 || u == n + 1) continue;
        int l = *(lower_bound(pos[j].begin(), pos[j].end(), u) - 1);
        int r = *upper_bound(pos[j].begin(), pos[j].end(), u);
        if (l >= 1) dfs(vis[i][j], query(l + 1, u - 1));
        if (r <= n) dfs(vis[i][j], query(u + 1, r - 1));
      }
    }
  }
  for (int s = 0; s < (1 << m); s++) {
    f[s] = 1;
    for (int i = 0; i < m; i++) {
      if ((s >> i) & 1) continue;
      for (int j = 0; j < m; j++) {
        if ((s >> j) & 1) continue;
        if (vis[i][j][s] || vis[j][i][s]) f[s] = 0;
      }
    }
  }
  ans[0] = 1;
  for (int i = 0; i < (1 << m); i++) {
    if (!ans[i]) continue;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) continue;
      if (f[i | (1 << j)]) ans[i | (1 << j)] = 1;
    }
  }
  int ma = 0;
  for (int i = 0; i < (1 << m); i++) {
    if (!ans[i]) continue;
    int t = 0;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) t += (int)pos[j].size() - 2;
    }
    ma = max(ma, t);
  }
  printf("%d\n", n - ma);
}
int main() {
  solve();
  return 0;
}
