#include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
const int N = 1e5 + 5, P = 17;
int a[P][P], last[P], b[P], sz[1 << P];
bool z[P][P][1 << P], ban[1 << P], dp[1 << P];
string s;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, p;
  cin >> n >> p >> s;
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < p; i++) {
    b[i] = i;
  }
  for (int i = 0; i < n; i++) {
    int c = s[i] - 'a';
    sz[1 << c]++;
    sort(b, b + p, [](int x, int y) { return last[x] > last[y]; });
    for (int j = 0, m = 0; j < p; j++) {
      if (last[b[j]] == -1) {
        break;
      }
      if (!a[c][b[j]]) {
        z[b[j]][c][m] = z[c][b[j]][m] = 1;
      }
      m |= 1 << b[j];
      if (b[j] == c) break;
    }
    last[c] = i;
  }
  for (int i = 1; i < (1 << p); i++) {
    sz[i] = sz[i & -i] + sz[i ^ (i & -i)];
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      for (int k = 1; k < (1 << p); k++) {
        if (!z[i][j][k]) {
          if (!((1 << i) & k) && !((1 << j) & k)) {
            int x = k;
            while (x > 0) {
              int y = x & -x;
              x ^= y;
              if (z[i][j][k ^ y]) {
                z[i][j][k] = 1;
                ban[k] = 1;
                break;
              }
            }
          }
        }
      }
    }
  }
  dp[0] = 1;
  for (int i = 0; i < (1 << p); i++) {
    if (!dp[i] || ban[i]) continue;
    for (int j = 0; j < p; j++) {
      if (!((i >> j) & 1)) {
        int k = i ^ (1 << k);
        dp[k] = 1;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < (1 << p); i++) {
    if (dp[i]) ans = max(ans, n - sz[i]);
  }
  cout << ans;
  return 0;
}
