#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];
bool matrix[mxp][mxp], dp[1 << mxp][mxp][mxp];
void solve() {
  cin >> n >> p >> s;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> 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;
  for (int i = 0; i < n; i++) freq[s[i] - 'a']++;
  for (int i = 0; i < p; i++) nxtmap[i] = n;
  for (int i = n - 1; i >= 0; i--) {
    nxt[i] = nxtmap[s[i] - 'a'];
    nxtmap[s[i] - 'a'] = i;
  }
  for (int c = 0; c < p; c++)
    if (nxtmap[c]) {
      for (int i = nxtmap['a' + c]; i < n; i = nxt[i]) {
        int j = i + 1, mask = 0;
        while (j < nxt[i]) {
          if (!(mask >> (s[j] - 'a') & 1) && !matrix[c][s[j] - 'a']) {
            dp[mask][c][s[j] - 'a'] = true;
          }
          mask |= 1 << (s[j++] - 'a');
        }
        if (!matrix[c][c] && nxt[i] != n) dp[mask][c][c] = true;
      }
    }
  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 b = 0; b < p; 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;
}
