#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
  return y <= x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
  return x <= y ? x = y, 1 : 0;
}
const long double eps = 1e-9;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 60;
const int MAXN = 100005;
const int MAXM = 2000005;
const int MX = 5000;
const int mods = 998244353;
const int SZ = 131;
const int inv2 = (mods + 1) >> 1;
const int INF = 0x3f3f3f3f;
namespace FastIO {
constexpr int SIZE = (1 << 21) + 1;
int num = 0, f;
char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf,
                                                *oT = obuf + SIZE - 1, c;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char c) {
  *oS++ = c;
  if (oS == oT) flush();
}
inline void getc(char &c) {
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       !isalpha(c) && c != EOF;
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    ;
}
inline void reads(char *st) {
  char c;
  int n = 0;
  getc(st[++n]);
  for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++);
       isalpha(c);
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    st[++n] = c;
  st[++n] = '\0';
}
template <class I>
inline void read(I &x) {
  for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c >= '0' && c <= '9';
       c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 3) + (x << 1) + (c & 15);
  x *= f;
}
template <class I>
inline void print(I x) {
  if (x < 0) putc('-'), x = -x;
  if (!x) putc('0');
  while (x) que[++num] = x % 10 + 48, x /= 10;
  while (num) putc(que[num--]);
}
inline void putstr(string st) {
  for (int i = 0; i < (int)st.size(); ++i) putc(st[i]);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_Flusher_;
}  // namespace FastIO
using FastIO ::print;
using FastIO ::putc;
using FastIO ::putstr;
using FastIO ::read;
using FastIO ::reads;
char st[MAXN];
int flag[1 << 17], cnt[17], s[1 << 17], f[1 << 17], g[1 << 17], id[17], wh[17],
    n, m;
void addtag(int x, int y) {
  int M = m - 1;
  for (int i = 0; i < m; ++i) id[i] = i;
  for (int i = y; i < m; ++i) id[i] = id[i + 1];
  if (x != y) {
    --M;
    for (int i = x; i < m - 1; ++i) id[i] = id[i + 1];
  }
  for (int i = 0; i < M; ++i) wh[id[i]] = i;
  for (int i = 0; i < 1 << M; ++i) g[i] = 0;
  for (int i = 1, lst = 0, nw = 0; i <= n; ++i) {
    if (st[i] - 'a' != x && st[i] - 'a' != y) {
      nw |= 1 << wh[st[i] - 'a'];
      continue;
    }
    if (lst && (st[lst] != st[i] || x == y)) g[nw] = 1;
    lst = i, nw = 0;
  }
  for (int i = 0; i < M; ++i)
    for (int j = 0; j < 1 << M; ++j)
      if (!((j >> i) & 1)) g[j ^ (1 << i)] |= g[j];
  for (int i = 0; i < 1 << M; ++i) {
    if (!g[i]) continue;
    int t = 0;
    for (int j = 0; j < M; ++j)
      if ((i >> j) & 1) t |= 1 << id[j];
    flag[t] = 1;
  }
}
int main() {
  read(n), read(m), reads(st);
  for (int i = 1; i <= n; ++i) ++cnt[st[i] - 'a'];
  for (int i = 0; i < m; ++i)
    for (int j = 0, X; j < m; ++j) {
      read(X);
      if (X == 1) continue;
      if (i <= j) addtag(i, j);
    }
  f[0] = 1;
  for (int i = 0; i < 1 << m; ++i) {
    for (int j = 0; j < m; ++j)
      if ((i >> j) & 1) {
        s[i] = s[i ^ (1 << j)] + cnt[j];
      }
    if (flag[i]) {
      f[i] = 0;
      continue;
    }
    for (int j = 0; j < m; ++j)
      if (!((i >> j) & 1)) f[i | (1 << j)] |= f[i];
  }
  int ans = 0;
  for (int i = 0; i < 1 << m; ++i)
    if (f[i]) upmax(ans, s[i]);
  print(n - ans);
  return 0;
}
