#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5 + 5;
const int inf = (int)1e9 + 7;
int n, p;
int a[17][17], cnt[17];
int bad[1 << 17][17][17], badf[1 << 17], can[1 << 17];
int l[17];
string s;
void go(int mask, int a, int b) {
  if (bad[mask][a][b]) {
    return;
  }
  badf[mask] = 1;
  bad[mask][a][b] = 1;
  for (int i = 0; i < p; ++i) {
    if (i == a || i == b) {
      continue;
    }
    if ((mask >> i) & 1) {
      go(mask ^ (1 << i), a, b);
    }
  }
}
int main() {
  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) {
    l[i] = -1;
  }
  for (int i = 0; i < n; ++i) {
    int cur = s[i] - 'a';
    for (int j = 0; j < p; ++j) {
      if (l[j] != -1 && !a[j][cur]) {
        int mask = 0;
        for (int t = 0; t < p; ++t) {
          if (t == j || t == cur) continue;
          if (l[t] > l[j]) {
            mask |= (1 << t);
          }
        }
        mask = ((1 << p) - 1) ^ mask;
        go(mask, j, cur);
      }
    }
    l[cur] = i;
    cnt[cur]++;
  }
  can[(1 << p) - 1] = 1;
  for (int i = (1 << p) - 1; i >= 0; --i) {
    if (!can[i]) {
      continue;
    }
    for (int j = 0; j < p; ++j) {
      if ((i >> j) & 1) {
        if (badf[i ^ (1 << j)]) continue;
        can[i ^ (1 << j)] = 1;
      }
    }
  }
  int ans = n;
  for (int i = 0; i < (1 << p); ++i) {
    if (!can[i]) {
      continue;
    }
    int cur = 0;
    for (int j = 0; j < p; ++j) {
      if ((i >> j) & 1) {
        cur += cnt[j];
      }
    }
    ans = min(ans, cur);
  }
  cout << ans;
  return 0;
}
