#include <bits/stdc++.h>
using namespace std;
template <class T, class T2>
inline int chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2>
inline int chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const int MAXN = (1 << 17);
const int MAXP = 17;
int n, p;
string s;
int a[MAXN];
int A[MAXP][MAXP];
void read() {
  cin >> n >> p;
  cin >> s;
  for (int i = 0; i < n; i++) a[i] = s[i] - 'a';
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> A[i][j];
}
int answer;
int cnt_let[MAXP];
bool bad_mask[MAXP][MAXP][MAXN];
bool used[MAXN];
void dfs(int mask) {
  if (used[mask]) return;
  used[mask] = true;
  int c_len = n;
  for (int i = 0; i < p; i++)
    if (!(mask & (1 << i))) {
      bool ok = 1;
      int nmask = (1 << i) ^ mask;
      for (int k = 0; k < p; k++)
        if (!(nmask & (1 << k)))
          for (int j = k; j < p; j++)
            if (!(nmask & (1 << j))) ok &= (0 == bad_mask[k][j][nmask]);
      if (ok) dfs(nmask);
    } else
      c_len -= cnt_let[i];
  chkmin(answer, c_len);
}
int CNT[MAXN][MAXP];
int C(int c, int l, int r) {
  if (l == 0) return CNT[r][c];
  return CNT[r][c] - CNT[l - 1][c];
}
void solve() {
  answer = n;
  int ini_mask = 0;
  for (int i = 0; i < n; i++) {
    cnt_let[a[i]]++;
    ini_mask |= (1 << a[i]);
    if (i)
      for (int j = 0; j < p; j++) CNT[i][j] = CNT[i - 1][j];
    CNT[i][a[i]]++;
  }
  for (int x = 0; x < p; x++)
    for (int y = 0; y < p; y++)
      if (A[x][y] == 0 && (ini_mask & (1 << x)) && (ini_mask & (1 << y))) {
        int last_y = -1;
        for (int i = 0; i < n; i++) {
          if (a[i] == x && last_y != -1) {
            int mask = 0;
            for (int c = 0; c < p; c++)
              if (ini_mask & (1 << c))
                mask |= (C(c, last_y + 1, i - 1) != 0) << c;
            bad_mask[x][y][mask] = 1;
            bad_mask[y][x][mask] = 1;
          }
          if (a[i] == y) last_y = i;
        }
      }
  for (int let = 0; let < p; let++)
    for (int let2 = let; let2 < p; let2++)
      for (int mask = 0; mask < (1 << p); mask++) {
        for (int i = 0; i < p; i++)
          if (mask & (1 << i))
            bad_mask[let][let2][mask] |= bad_mask[let][let2][mask ^ (1 << i)];
      }
  dfs(0);
  cout << answer << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  read();
  solve();
  return 0;
}
