#include <bits/stdc++.h>
using namespace std;
inline void setmin(int &x, int y) {
  if (y < x) x = y;
}
inline void setmax(int &x, int y) {
  if (y > x) x = y;
}
inline void setmin(long long &x, long long y) {
  if (y < x) x = y;
}
inline void setmax(long long &x, long long y) {
  if (y > x) x = y;
}
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;
bool ok[17][17];
bool good[1 << 17];
bool reach[1 << 17];
int cnt[17];
long long bad[1 << 17];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  int ccc = 0;
  for (int i = 0; i < n; i++) {
    cnt[s[i] - 'a']++;
    ccc |= (1 << (s[i] - 'a'));
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      cin >> ok[i][j];
    }
  }
  for (int c = 0; c < p; c++) {
    int pos = 0;
    while (pos < n && s[pos] - 'a' != c) {
      pos++;
    }
    pos++;
    int cur = 0;
    while (pos < n) {
      if (!ok[c][s[pos] - 'a']) {
        bad[((1 << p) - 1) ^ cur] |= (1LL << (c * p + (s[pos] - 'a')));
        bad[((1 << p) - 1) ^ cur] |= (1LL << (p * (s[pos] - 'a') + c));
      }
      if (s[pos] - 'a' == c) {
        cur = 0;
      } else {
        cur |= (1 << (s[pos] - 'a'));
      }
      pos++;
    }
  }
  for (int mask = (1 << p) - 1; mask > -1; mask--) {
    for (int i = 0; i < p; i++) {
      if ((mask >> i) & 1) {
        bad[mask ^ (1 << i)] |= bad[mask];
      }
    }
  }
  for (int mask = 0; mask < (1 << p); mask++) {
    good[mask] = true;
    for (int i = 0; i < p; i++) {
      if (!((mask >> i) & 1)) {
        continue;
      }
      for (int j = 0; j < p; j++) {
        if (!((mask >> j) & 1)) {
          continue;
        }
        if ((bad[mask] >> (i * p + j)) & 1) {
          good[mask] = false;
        }
      }
    }
  }
  reach[ccc] = true;
  for (int mask = (1 << p) - 1; mask > -1; mask--) {
    if (!reach[mask] || !good[mask]) {
      continue;
    }
    for (int i = 0; i < p; i++) {
      if ((mask >> i) & 1) {
        if (good[mask ^ (1 << i)]) {
          reach[mask ^ (1 << i)] = true;
        }
      }
    }
  }
  int ans = n;
  for (int mask = 0; mask < (1 << p); mask++) {
    if (reach[mask]) {
      int c = 0;
      for (int i = 0; i < p; i++) {
        if ((mask >> i) & 1) {
          c += cnt[i];
        }
      }
      setmin(ans, c);
    }
  }
  cout << ans << "\n";
  return 0;
}
