#include <bits/stdc++.h>
std::string s;
int real_size;
int get_next_isnt(int p, char isnt) {
  while (true) {
    p++;
    if (p >= real_size) return s.size() - 1;
    if (s[p] != isnt) return p;
  }
}
int get_prev_isnt(int p, char isnt) {
  while (true) {
    p--;
    if (p < 0) return s.size() - 1;
    if (s[p] != isnt) return p;
  }
}
int main() {
  int n, p;
  bool m[18][18];
  std::cin >> n >> p >> s;
  real_size = s.size();
  s += ('a' + 17);
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      int a;
      std::cin >> a;
      m[i][j] = (bool)a;
    }
  }
  for (int i = 0; i < 18; i++) {
    m[i][17] = true;
    m[17][i] = true;
  }
  bool can_del;
  while (true) {
    int del_ch = -1;
    for (int ch = 0; ch < p; ch++) {
      can_del = true;
      bool any = (s[0] == ch + 'a') || (s[real_size - 1] == ch + 'a');
      int nxt;
      for (int i = 1; i < real_size - 1; i++) {
        if (s[i] == ch + 'a') {
          any = true;
          nxt = get_next_isnt(i, ch + 'a');
          if (!m[s[get_prev_isnt(i, ch + 'a')] - 'a'][s[nxt] - 'a']) {
            can_del = false;
            break;
          }
          i = nxt - 1;
        }
      }
      if (can_del && any) {
        del_ch = ch;
        break;
      }
    }
    if (del_ch == -1) break;
    int z = 0;
    for (int i = 0; i < real_size; i++) {
      s[i - z] = s[i];
      z += s[i] == del_ch + 'a';
    }
    real_size -= z;
    if (real_size == 0) break;
  }
  std::cout << real_size;
  return 0;
}
