#include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b,
                 long long mod = (long long)(1e9 + 7)) {
  if (!b) return 1;
  a %= mod;
  return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxn = 1e5, mxp = 17, inf = 1e9;
string s;
int n, p, nxt[mxn], sz[mxn], res[mxn], freq[mxp], nxtmap[mxp];
int matrix[mxp][mxp], dp[1 << mxp][mxp][mxp];
void solve() {
  scanf("%d", &n);
  scanf("%d", &p);
  char buffer[2 * mxn];
  scanf("%s", buffer);
  s = string(buffer);
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) scanf("%d", &matrix[i][j]);
  for (int i = 0; i < 1 << p; i++)
    for (int j = 0; j < p; j++)
      for (int k = 0; k < p; k++) dp[i][j][k] = 0;
  for (int i = 0; i < p; i++) freq[i] = 0, nxtmap[i] = n;
  for (int i = n - 1; i >= 0; i--) {
    nxt[i] = nxtmap[s[i] - 'a'];
    freq[s[i] - 'a']++;
    nxtmap[s[i] - 'a'] = i;
  }
  for (int mask = 0; mask < 1 << p; mask++) {
    sz[mask] = 0;
    for (int i = 0; i < p; i++)
      if (mask & (1 << i)) sz[mask] += freq[i];
    res[mask] = sz[mask];
  }
  for (int a = 0; a < p; a++) {
    for (int i = nxtmap[a]; i < n; i = nxt[i]) {
      int j = i + 1, mask = 0;
      while (j < nxt[i]) {
        if (!(mask >> (s[j] - 'a') & 1) && !matrix[a][s[j] - 'a'])
          dp[mask][a][s[j] - 'a'] = dp[mask][s[j] - 'a'][a] = true;
        mask |= 1 << (s[j++] - 'a');
      }
      dp[mask][a][a] |= !matrix[a][a] && nxt[i] != n;
    }
    for (int b = a; b <= p - 1; b++) {
      for (int i = 0; i < p; i++)
        if (i != a && i != b)
          for (int mask = 0; mask < 1 << p; mask++)
            if (mask >> i & 1) dp[mask][a][b] |= dp[mask ^ (1 << i)][a][b];
      for (int mask = 0; mask < 1 << p; mask++)
        if (dp[mask][a][b]) res[((1 << p) - 1) ^ mask] = inf;
    }
  }
  for (int mask = 0; mask < 1 << p; mask++)
    if (res[mask] != inf) {
      for (int i = 0; i < p; i++)
        if (mask >> i & 1) {
          (res[mask]) = min((res[mask]), (res[mask ^ (1 << i)]));
        }
    }
  cout << res[(1 << p) - 1] << endl;
  ;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
