#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  vector<vector<long long>> a((size_t)p, vector<long long>((size_t)p));
  for (long long i = 0; i < p; i++) {
    for (long long j = 0; j < p; j++) {
      cin >> a[i][j];
    }
  }
  vector<bool> b((size_t)1 << p, true);
  vector<bool> h((size_t)1 << p, false);
  vector<long long> c;
  for (long long i = 0; i < n; i++) {
    for (long long j = (long long)c.size() - 1, k = 0; j >= 0; j--) {
      if (a[s[i] - 'a'][c[j]] == 0) {
        b[k] = false;
        if (!h[k | (1 << (s[i] - 'a')) | (1 << c[j])]) {
          h[k | (1 << (s[i] - 'a')) | (1 << c[j])] = true;
          for (long long l =
                   (k | (1 << (s[i] - 'a')) | (1 << c[j])) ^ ((1 << p) - 1);
               l > 0; l = (l - 1) & ((k | (1 << (s[i] - 'a')) | (1 << c[j])) ^
                                     ((1 << p) - 1))) {
            b[k | l] = false;
          }
        }
      }
      if (c[j] == s[i] - 'a') {
        break;
      }
      k |= 1 << c[j];
    }
    c.erase(remove(c.begin(), c.end(), s[i] - 'a'), c.end());
    c.push_back(s[i] - 'a');
  }
  vector<long long> d((size_t)p);
  for (long long i = 0; i < p; i++) {
    d[i] = count(s.begin(), s.end(), 'a' + i);
  }
  vector<long long> e((size_t)1 << p);
  iota(e.begin(), e.end(), (long long)0);
  sort(e.begin(), e.end(), [](long long x, long long y) {
    return __builtin_popcountll(x) < __builtin_popcountll(y);
  });
  vector<bool> f((size_t)1 << p);
  f[0] = true;
  long long m = (long long)1e14;
  for (long long i : e) {
    if (f[i]) {
      long long t = n;
      for (long long j = 0; j < p; j++) {
        if ((i & (1 << j)) == 0) {
          if (b[i | (1 << j)]) {
            f[i | (1 << j)] = true;
          }
        } else {
          t -= d[j];
        }
      }
      m = min(m, t);
    }
  }
  cout << m << endl;
  return 0;
}
