#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 1e5 + 10, maxm = 1e3 + 10, lg = 17, mod = 1e9 + 7,
                inf = 1e18;
const long long M = (1 << lg) + 20;
long long n, p, ok[M], tt[lg], ans, mw[maxn];
string s;
bool b[lg][lg], dp[M];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> p;
  cin >> s;
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++) cin >> b[i][j];
  for (int i = 0; i < n; i++) {
    long long c = s[i] - 'a';
    for (int j = 0; j < p; j++) {
      if (tt[j] && !b[j][c] && (mw[j] >> c & 1) == 0) {
        ok[mw[j]]--;
        ok[mw[j] ^ (1 << c)]++;
        if (j != c) {
          ok[mw[j] ^ (1 << j)]++;
          ok[mw[j] ^ (1 << c) ^ (1 << j)]--;
        }
      }
      mw[j] |= (1 << c);
    }
    mw[c] = 0;
    tt[c]++;
  }
  for (int i = 0; i < p; i++)
    for (int msk = 0; msk < (1 << p); msk++)
      if (msk >> i & 1) ok[msk] += ok[msk ^ (1 << i)];
  ans = n;
  dp[0] = 1;
  for (int msk = 1; msk < (1 << p); msk++)
    if (ok[msk] >= 0) {
      long long tot = 0;
      for (int i = 0; i < p; i++)
        if (msk >> i & 1) {
          dp[msk] |= dp[msk ^ (1 << i)];
          tot += tt[i];
        }
      if (dp[msk]) ans = min(ans, n - tot);
    }
  cout << ans;
  return 0;
}
