#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());
bool debug = true;
const int mxn = 1e5, mxp = 17, inf = 1e9;
string s;
int n, p, sz[1 << mxp], res[1 << mxp], freq[mxp], c[mxn][mxp];
bool matrix[mxp][mxp];
vector<int> pos[mxp];
void solve() {
  scanf("%d", &n);
  scanf("%d", &p);
  char buffer[20 * mxn];
  scanf("%s", buffer);
  s = string(buffer);
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) {
      int x;
      scanf("%d", &x);
      matrix[i][j] = x;
    }
  for (int i = 0; i < p; i++) freq[i] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < p; j++)
      c[i][j] += (i ? c[i - 1][j] : 0) + (s[i] == 'a' + j);
  for (int i = 0; i < n; i++) pos[s[i] - 'a'].push_back(i);
  for (int i = n - 1; i >= 0; i--) freq[s[i] - 'a']++;
  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];
  }
  int fl = (1 << p) - 1;
  for (int a = 0; a < p; a++)
    for (int b = 0; b < a + 1; b++)
      if (!matrix[a][b]) {
        int dp[1 << p];
        for (int i = 0; i < 1 << p; i++) dp[i] = 0;
        for (int i = 0, j = 0;
             i < int((pos[a]).size()) && j < int((pos[b]).size());) {
          while (a == b && i == j) j++;
          if (j == int((pos[b]).size())) break;
          while (a == b && i + 1 < j) i++;
          int mask = 0;
          if (pos[a][i] < pos[b][j]) {
            for (int e = 0; e < p; e++)
              if (c[pos[b][j] - 1][e] - c[pos[a][i]][e] > 0) mask |= 1 << e;
            i++;
          } else {
            for (int e = 0; e < p; e++)
              if (c[pos[a][i] - 1][e] - c[pos[b][j]][e] > 0) mask |= 1 << e;
            j++;
          }
          if (mask >> a & 1) continue;
          if (mask >> b & 1) continue;
          dp[mask] = 1;
        }
        for (int i = 0; i < p; i++)
          if (i != a && i != b)
            for (int mask = 0; mask < 1 << p; mask++)
              if (mask & (1 << i)) dp[mask] |= dp[mask ^ (1 << i)];
        for (int mask = 0; mask < 1 << p; mask++) {
          if (dp[mask]) res[fl ^ 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[fl] << endl;
  ;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
