#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100100;
const int PMAX = 17;
const int INF = 0x3f3f3f3f;
long long n;
int p;
char a[PMAX][PMAX];
char forb[PMAX][PMAX][1 << PMAX];
char forbb[1 << PMAX];
int dp[1 << PMAX];
char s[NMAX];
int loc[PMAX];
int cnt[PMAX];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> p;
  cin >> (s + 1);
  for (int i = 1; i <= n; ++i) {
    s[i] -= 'a';
    cnt[s[i]]++;
  }
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      cin >> a[i][j];
      a[i][j] -= '0';
    }
  }
  map<int, int> ord;
  for (int i = 1; i <= n; ++i) {
    int bits = 0;
    for (auto it = ord.rbegin(); it != ord.rend(); ++it) {
      if (a[it->second][s[i]] == 0) {
        forb[it->second][s[i]][bits] = 1;
      }
      bits |= (1 << (it->second));
      if (it->second == s[i]) break;
    }
    if (loc[s[i]]) {
      ord.erase(loc[s[i]]);
    }
    ord[i] = s[i];
    loc[s[i]] = i;
  }
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      int m = (1 << p) - 1;
      m ^= ((1 << j) | (1 << i));
      for (int b = 0; b < (1 << p); ++b) {
        for (int k = 0; k < p; ++k) {
          if (!(b & (1 << k))) continue;
          forb[i][j][b] |= forb[i][j][b ^ (1 << k)];
        }
        forbb[b & m] |= forb[i][j][b];
      }
    }
  }
  int ans = 1;
  dp[0] = 1;
  for (int i = 1; i < (1 << p); ++i) {
    if (forbb[i]) continue;
    for (int j = 0; j < p; ++j) {
      if (((1 << j) & i) && dp[i ^ (1 << j)]) {
        dp[i] = dp[i ^ (1 << j)] + cnt[j];
        ans = max(ans, dp[i]);
        break;
      }
    }
  }
  cout << n - ans + 1 << endl;
  return 0;
}
