#include <bits/stdc++.h>
using namespace std;
const int P = 17;
const int N = 1e5 + 5;
int cnt[P];
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;
      int snek;
      set<int> cnec;
      for (char c : s) {
        if (gud) {
          if (c == j) {
            cnec.insert(snek);
            gud = 0;
          } else {
            snek |= 1 << c;
          }
        }
        if (!gud) {
          if (c == i) {
            gud = 1;
            snek = 0;
          }
        }
      }
      for (int s : cnec) {
        for (int k = 0; k < (p); k++)
          if (k != i && k != j) cnec.insert(s | (1 << k));
        bigbadset.insert(s);
      }
    }
  int best = 1e9;
  bigbadset.insert(0);
  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 (!bigbadset.count(a | (1 << i))) {
        bigbadset.insert(a | (1 << i));
        v.push_back(a | (1 << i));
      }
    }
    best = min(best, kek);
  }
  cout << best << endl;
}
