#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
int n, m;
string s;
bool used[18][18];
int sz[18];
int ind[18];
int r[N][18];
bool dp[(1 << 17) + 5];
bool check[17][17][(1 << 17) + 2];
bool mark[(1 << 17) + 5];
int main() {
  cin >> n >> m;
  cin >> s;
  for (int i = 0; i < n; i++) {
    sz[s[i] - 'a']++;
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      cin >> used[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    ind[i] = n;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < m; j++) {
      r[i][j] = ind[j];
    }
    ind[s[i] - 'a'] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int id = r[i][j];
      if (id == n || used[s[i] - 'a'][j] || r[i][s[i] - 'a'] < id) continue;
      int mask = 0;
      for (int h = 0; h < m; h++) {
        if (r[i][h] < id) {
          mask |= (1 << h);
        }
      }
      check[s[i] - 'a'][j][mask] = true;
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      for (int h = 0; h < (1 << m); h++) {
        if (check[i][j][h]) {
          mark[h] = true;
          for (int t = 0; t < m; t++) {
            if (t != i && t != j) {
              check[i][j][h | (1 << t)] = true;
            }
          }
        }
      }
    }
  }
  dp[0] = true;
  int mx = 0;
  for (int i = 0; i < (1 << m); i++) {
    if (dp[i] == false) continue;
    int sum = 0;
    for (int j = 0; j < m; j++) {
      if ((i >> j) & 1) sum += sz[j];
    }
    mx = max(mx, sum);
    for (int j = 0; j < m; j++) {
      if (((i >> j) & 1) == 0) {
        int mask = i + (1 << j);
        if (mark[mask] == false) {
          dp[mask] = true;
        }
      }
    }
  }
  cout << n - mx << endl;
  return 0;
}
