#include <bits/stdc++.h>
using namespace std;
const int P = 17;
const int N = 1 << 17;
int cnt[P];
bool bigbad[N], smallbad[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  for (char &c : s) {
    c -= 'a';
    cnt[c]++;
  }
  set<int> bigbadset;
  for (int i = 0; i < (p); i++)
    for (int j = 0; j < (p); j++) {
      bool gud;
      cin >> gud;
      if (gud) continue;
      fill(smallbad, smallbad + N, 0);
      int snek;
      for (char c : s) {
        if (gud) {
          if (c == j) {
            smallbad[snek] = 1;
            gud = 0;
          } else if (c == i) {
            snek = 0;
          } else {
            snek |= 1 << c;
          }
        }
        if (!gud) {
          if (c == i) {
            gud = 1;
            snek = 0;
          }
        }
      }
      for (int a = 0; a < (N); a++)
        if (smallbad[a]) {
          for (int k = 0; k < (p); k++)
            if (k != i && k != j) smallbad[a | (1 << k)] = 1;
          bigbad[a] = 1;
        }
    }
  int best = 1e9;
  bigbad[0] = 1;
  vector<int> v;
  v.push_back(0);
  while (!v.empty()) {
    int kek = 0;
    int a = v.back();
    v.pop_back();
    for (int i = 0; i < (p); i++) {
      if (a & (1 << i)) continue;
      kek += cnt[i];
      if (!bigbad[a | (1 << i)]) {
        bigbad[a | (1 << i)] = 1;
        v.push_back(a | (1 << i));
      }
    }
    best = min(best, kek);
  }
  cout << best << endl;
}
