#include <bits/stdc++.h>
using namespace std;
const long long INF =
    sizeof(long long) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const long long MOD = (long long)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
using pii = pair<long long, long long>;
template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T>
void printvv(const vector<vector<T>> &v) {
  cerr << endl;
  for (long long i = (0); i < (v.size()); i++)
    for (long long j = (0); j < (v[i].size()); j++) {
      if (typeid(v[i][j]).name() == typeid(INF).name() and v[i][j] == INF) {
        cerr << "INF";
      } else
        cerr << v[i][j];
      cerr << (j == v[i].size() - 1 ? '\n' : ' ');
    }
  cerr << endl;
}
void YES(bool f) { cout << (f ? "YES" : "NO") << endl; }
void Yes(bool f) { cout << (f ? "Yes" : "No") << endl; }
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
signed main(signed argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  long long N, P;
  cin >> N >> P;
  string s;
  cin >> s;
  vector<vector<long long>> A(P, vector<long long>(P));
  for (long long i = (0); i < (P); i++)
    for (long long j = (0); j < (P); j++) {
      cin >> A[i][j];
    }
  vector<vector<long long>> v(P, vector<long long>(N + 1));
  vector<vector<long long>> w(P);
  for (long long i = (0); i < (s.size()); i++) {
    v[s[i] - 'a'][i + 1]++;
    w[s[i] - 'a'].emplace_back(i + 1);
  };
  ;
  for (long long i = (1); i < (N + 1); i++)
    for (long long j = (0); j < (P); j++) v[j][i] += v[j][i - 1];
  ;
  long long fl = (1ll << P) - 1;
  vector<long long> dp(1ll << P);
  for (long long i = (0); i < (P); i++)
    for (long long j = (i); j < (P); j++) {
      if (A[i][j]) continue;
      for (long long k = (0); k < (w[i].size()); k++) {
        long long l = w[i][k];
        if (upper_bound(begin(w[j]), end(w[j]), l) == w[j].end()) continue;
        long long u = *upper_bound(begin(w[j]), end(w[j]), l);
        u--;
        long long mask = 0;
        for (long long a = (0); a < (P); a++) {
          mask |= (1ll << a) * (v[a][u] - v[a][l] > 0);
        }
        if ((mask >> i) & 1 || (mask >> j) & 1) continue;
        assert(mask != 0);
        dp[fl ^ mask] = INF;
      }
      swap(i, j);
      for (long long k = (0); k < (w[i].size()); k++) {
        long long l = w[i][k];
        if (upper_bound(begin(w[j]), end(w[j]), l) == w[j].end()) continue;
        long long u = *upper_bound(begin(w[j]), end(w[j]), l);
        u--;
        long long mask = 0;
        for (long long a = (0); a < (P); a++) {
          mask |= (1ll << a) * (v[a][u] - v[a][l] > 0);
        }
        if ((mask >> i) & 1 || (mask >> j) & 1) continue;
        assert(mask != 0);
        dp[fl ^ mask] = INF;
      }
      swap(i, j);
      ;
      for (long long bits = (1ll << P) - 1; bits >= (0); bits--) {
        if (((bits >> i) & 1) and ((bits >> j) & 1)) {
          for (long long k = (0); k < (P); k++) {
            if (((bits >> k) & 1) and k != i and k != j)
              chmax(dp[bits ^ (1ll << k)], dp[bits]);
          }
        }
      };
    };
  for (long long bits = (0); bits < (1ll << P); bits++) {
    long long cnt = 0;
    for (long long k = (0); k < (P); k++) {
      cnt += v[k][N] * ((bits >> k) & 1);
    }
    chmax(dp[bits], cnt);
  };
  for (long long bits = (0); bits < (1ll << P); bits++) {
    if (dp[bits] == INF) continue;
    for (long long k = (0); k < (P); k++) {
      if ((bits >> k) & 1) chmin(dp[bits], dp[bits ^ (1ll << k)]);
    }
  };
  cout << dp[fl] << endl;
  return 0;
}
