#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 13;
const int P = 17;
int n, p;
string s;
int A[P][P];
vector<int> pos[P];
int pr[P][N];
bitset<(1 << P)> legal, cur, dp;
int cnt[P];
int main() {
  scanf("%d%d", &n, &p);
  char buf[N];
  scanf("%s", buf);
  s = buf;
  for (int i = 0; i < int(p); i++)
    for (int j = 0; j < int(p); j++) scanf("%d", &A[i][j]);
  for (int i = 0; i < int(n); i++) {
    pos[s[i] - 'a'].push_back(i);
    for (int j = 0; j < int(p); j++)
      pr[j][i + 1] = pr[j][i] + (s[i] == 'a' + j);
  }
  legal.reset();
  legal.flip();
  int fl = (1 << p) - 1;
  for (int c = 0; c < int(p); c++)
    for (int d = 0; d < int(c + 1); d++) {
      if (A[c][d]) continue;
      cur.reset();
      cur.flip();
      int i = 0, j = 0;
      while (i < pos[c].size() && j < pos[d].size()) {
        if (c == d && i == j) {
          ++j;
          continue;
        }
        int mask = 0;
        if (pos[c][i] < pos[d][j]) {
          for (int e = 0; e < int(p); e++)
            if ((pr[e][pos[d][j]] - pr[e][pos[c][i] + 1]) != 0)
              mask |= (1 << e);
          ++i;
        } else {
          for (int e = 0; e < int(p); e++)
            if ((pr[e][pos[c][i]] - pr[e][pos[d][j] + 1]) != 0)
              mask |= (1 << e);
          ++j;
        }
        if ((mask >> c) & 1) continue;
        if ((mask >> d) & 1) continue;
        cur[mask ^ fl] = 0;
      }
      for (int mask = fl; mask > 0; --mask) {
        if (cur[mask]) continue;
        for (int e = 0; e < int(p); e++)
          if (c != e && d != e && ((mask >> e) & 1)) cur[mask ^ (1 << e)] = 0;
      }
      legal &= cur;
    }
  dp[fl] = 1;
  for (int mask = fl; mask > 0; --mask) {
    if (!dp[mask]) continue;
    for (int i = 0; i < int(p); i++)
      if ((mask >> i) & 1) {
        int nmask = mask ^ (1 << i);
        if (dp[nmask]) continue;
        dp[nmask] = legal[nmask];
      }
  }
  for (int i = 0; i < int(1 << p); i++) cout << legal[i] << ' ';
  for (int i = 0; i < int(n); i++) ++cnt[s[i] - 'a'];
  int ans = n;
  for (int mask = 0; mask < int(1 << p); mask++)
    if (dp[mask]) {
      int cur = 0;
      for (int i = 0; i < int(p); i++)
        if ((mask >> i) & 1) cur += cnt[i];
      ans = min(ans, cur);
    }
  printf("%d\n", ans);
  return 0;
}
