#include <bits/stdc++.h>
using namespace std;
const int mxn = 100000, mxk = 17;
int n, k;
int a[mxn], b[mxk];
int first[1 << mxk], ff[1 << mxk], dp[1 << mxk];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    b[a[i] = c - 'a']++;
  }
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k; j++) {
      int x;
      cin >> x;
      memset(first, 0, sizeof(first));
      if (!x)
        for (int l = 0, y = 0, z = 0; l < n; l++) {
          if (a[l] == i) first[z] |= y, y = z = 0;
          if (a[l] == j)
            z = 0, y = 1;
          else
            z |= y << a[l];
        }
      for (int l = 0; l < (1 << k); l++) {
        for (int p = 0; p < k; p++) {
          if (((l >> p) & 1) && p != i && p != j)
            first[l] |= first[l ^ (1 << p)];
        }
        ff[l] |= first[l];
      }
    }
  int ret = n;
  dp[0] = 1;
  for (int i = 0; i < (1 << k); i++)
    if (!ff[i]) {
      int x = 0;
      for (int j = 0; j < k; j++)
        if ((i >> j) & 1) {
          x += b[j];
          dp[i] |= dp[i ^ (1 << j)];
        }
      if (dp[i]) ret = min(ret, n - x);
    }
  cout << ret << '\n';
  return 0;
}
