#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 998244353;
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll modInv(ll a) { return binpow(a, MOD - 2); }
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
int arr[mxN];
int badmask[mxN];
int board[17][17];
bool ok[1 << 17];
bool seen[1 << 17];
int cnt[17];
int val[1 << 17];
bool dp[1 << 17];
int n, p;
void upd(int mask, int a, int b) {
  if (seen[mask]) return;
  seen[mask] = true;
  ok[mask] = false;
  for (int i = (0); i < (p); ++i) {
    if (i == a || i == b) continue;
    if ((mask & (1 << i)) == 0) upd((mask | (1 << i)), a, b);
  }
}
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> p;
  string str;
  cin >> str;
  memset(ok, true, sizeof(ok));
  for (int i = (0); i < (n); ++i) {
    arr[i] = str[i] - 'a';
    cnt[arr[i]]++;
  }
  for (int i = (0); i < (1 << p); ++i) {
    for (int j = (0); j < (p); ++j) {
      if (i & (1 << j)) val[i] += cnt[j];
    }
  }
  for (int i = (0); i < (p); ++i) {
    for (int j = (0); j < (p); ++j) {
      cin >> board[i][j];
    }
  }
  for (int i = (0); i < (p); ++i) {
    for (int j = (0); j < (p); ++j) {
      memset(seen, false, sizeof(seen));
      if (board[i][j] == 0) {
        int mask = 0;
        int first = -1;
        for (int nn = (0); nn < (n); ++nn) {
          if (first > -1 && arr[nn] == j) {
            upd(mask, i, j);
            mask = 0;
            first = -1;
          } else if (first > -1) {
            mask |= (1 << arr[nn]);
          }
          if (arr[nn] == i) {
            first = nn;
            mask = 0;
          }
        }
      }
    }
  }
  int best = 0;
  dp[0] = true;
  for (int msk = (1); msk < (1 << p); ++msk) {
    if (!ok[msk]) continue;
    for (int nxt = (0); nxt < (p); ++nxt) {
      if (msk & (1 << nxt) && dp[msk ^ (1 << nxt)]) {
        dp[msk] = true;
      }
    }
    if (dp[msk]) {
      best = max(best, val[msk]);
    }
  }
  cout << (n - best) << "\n";
  return 0;
}
