#include <bits/stdc++.h>
using namespace std;
int dp[1 << 18];
int res[1 << 18];
int arr[1 << 18];
int go[18][18];
int num[18];
int cnt1[18];
int tmp[1 << 17][17][17];
int main() {
  int n, p;
  cin >> n >> p;
  for (int i = 0; i < p; i++) {
    cnt1[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    char c;
    cin >> c;
    int a = (int)c - 97;
    arr[i] = a;
    cnt1[a]++;
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      cin >> go[i][j];
    }
  }
  for (int i = 0; i < (1 << p); i++) {
    res[i] = 1;
  }
  for (int i = 0; i < (1 << p); i++) {
    for (int j = 0; j < p; j++) {
      for (int k = 0; k < p; k++) {
        tmp[i][j][k] = 1;
      }
    }
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      if (i == j) {
        int mask = 0;
        int ex = 0;
        if (go[i][j] == 0) {
          for (int k = 1; k <= n; k++) {
            if (arr[k] == j && ex == 0) {
              ex = 1;
              mask = 0;
              continue;
            }
            if (arr[k] == i && ex == 1) {
              tmp[mask][i][i] = 0;
              mask = 0;
              continue;
            }
            mask = mask | (1 << arr[k]);
          }
        }
      }
      int mask = 0;
      int ex = 0;
      if (go[i][j] == 0) {
        for (int k = 1; k <= n; k++) {
          if (arr[k] == j) {
            ex = 1;
            mask = 0;
            continue;
          }
          if (arr[k] == i && ex == 1) {
            if ((mask & (1 << arr[k])) == 0) {
              tmp[mask][min(i, j)][max(i, j)] = 0;
            }
          }
          mask = mask | (1 << arr[k]);
        }
      }
    }
  }
  int c, d;
  for (int i = 1; i < (1 << p); i++) {
    for (int j = 0; j < p; j++) {
      for (int k = 0; k < p; k++) {
        if (tmp[i][j][k] == 0) {
          res[i] = 0;
        }
      }
      if (res[i] == 0) {
        break;
      }
    }
    if (res[i] == 1) {
      continue;
    }
    for (int j = 0; j < p; j++) {
      for (int k = 0; k < p; k++) {
        if (tmp[i][j][k] == 1) {
          continue;
        }
        for (int d = 0; d < p; d++) {
          if (((i & (1 << d)) == 0) && (j != d && k != d)) {
            if (tmp[i][j][k] == 0) {
              tmp[i | (1 << d)][j][k] = 0;
            }
          }
        }
      }
    }
  }
  dp[0] = 1;
  for (int i = 1; i < (1 << p); i++) {
    dp[i] = 0;
  }
  for (int i = 0; i < (1 << p); i++) {
    d = 0;
    c = i;
    while (c > 0) {
      if (c & 1) {
        if (dp[i ^ (1 << d)] == 1) {
          dp[i] = 1;
        }
      }
      c >>= 1;
      d++;
    }
    dp[i] = min(res[i], dp[i]);
  }
  int ans = n;
  for (int i = 0; i < (1 << p); i++) {
    int cnt = 0;
    if (dp[i] == 0) {
      continue;
    }
    c = i;
    d = 0;
    while (c > 0) {
      if (c & 1) {
        cnt += cnt1[d];
      }
      c >>= 1;
      d++;
    }
    ans = min(ans, n - cnt);
  }
  cout << ans << "\n";
}
