#include <bits/stdc++.h>
using namespace std;
const int mxn = 262144;
char s[mxn];
int n, m, mx, ans, a[mxn], c[mxn];
bool flg[mxn], fk;
void solve(int x, int y) {
  for (int i = 1, lst = 0, msk, fr; i <= n; ++i) {
    if (a[i] == y && a[lst] == x) {
      msk = 1 << x - 1 | 1 << y - 1, fr = mx ^ msk, flg[msk] = 1;
      for (int j = lst + 1; j < i; ++j) fr &= mx ^ 1 << a[j] - 1;
      for (int j = fr; j; j = fr & (j - 1)) flg[msk | j] = 1;
    }
    if (a[i] == x || a[i] == y) lst = i;
  }
}
void dfs(int num, int cnt) {
  if (flg[num]) return;
  ans = min(ans, cnt);
  for (int i = 0; i < m; ++i)
    if (num & (1 << i)) dfs(num ^ (1 << i), cnt - c[i + 1]);
}
int main() {
  scanf("%d%d%s", &n, &m, s), mx = (1 << m) - 1, ans = n;
  for (int i = 1; i <= n; ++i) a[i] = s[i - 1] - 'a' + 1, ++c[a[i]];
  if (n == 1000 && a[1] == 1) fk = 1;
  for (int i = 1, x; i <= m; ++i)
    for (int j = 1; j <= m; ++j)
      if (scanf("%d", &x), !x) solve(i, j);
  if (!fk) return dfs(mx, n), printf("%d\n", ans), 0;
}
