#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX / 3;
class Solution {
 public:
  int minimize(string& s, int n, int p, vector<vector<int>>& adjs) {
    int N = 1 << p;
    int mask = N - 1;
    int S = 0;
    vector<vector<int>> cnts(p, vector<int>(n + 1, 0));
    vector<int> pos(p, -1);
    vector<bitset<320>> forbids(N);
    vector<bitset<320>> remains(N);
    for (int i = 0; i < n; ++i) {
      int j = s[i] - 'a';
      S |= 1 << j;
      for (int k = 0; k < p; ++k) {
        cnts[k][i + 1] = cnts[k][i];
      }
      ++cnts[j][i + 1];
      for (int k = 0; k < p; ++k) {
        if (adjs[j][k] == 0) {
          int l = pos[k];
          if (l == -1) {
            continue;
          }
          int r = i - 1;
          int code = 0;
          for (int c = 0; c < p; ++c) {
            int cnt = cnts[c][r + 1] - cnts[c][l + 1];
            if (cnt > 0) {
              code |= 1 << c;
            }
          }
          if (code != 0) {
            forbids[code].set(j * p + k);
            forbids[code].set(k * p + j);
          }
        }
      }
      pos[j] = i;
    }
    for (int i = 0; i < p; ++i) {
      for (int j = 0; j < N; ++j) {
        if ((j & (1 << i)) == 0) {
          forbids[j | 1 << i] |= forbids[j];
        }
      }
    }
    for (int i = 1; i < N; ++i) {
      int b = i & -i;
      int ni = i ^ b;
      remains[i] = remains[ni];
      int k = p - 1;
      for (int j = 0; j < p; ++j) {
        if ((1 << j) == b) {
          k = j;
        }
        if (i & (1 << j)) {
          remains[i].set(k * p + j);
          remains[i].set(j * p + k);
        }
      }
    }
    vector<int> dp(N, INF);
    dp[S] = n;
    for (int i = S; i >= 0; --i) {
      if (dp[i] == INF) {
        continue;
      }
      for (int j = 0; j < p; ++j) {
        if (i & (1 << j)) {
          int ni = i ^ (1 << j);
          int rmv = ni ^ mask;
          if ((remains[ni] & forbids[rmv]) == 0) {
            dp[ni] = min(dp[ni], dp[i] - cnts[j][n]);
          }
        }
      }
    }
    int res = INF;
    for (int i = 0; i < N; ++i) {
      res = min(res, dp[i]);
    }
    return res;
  }
};
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution sol;
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  vector<vector<int>> adjs(p, vector<int>(p, 0));
  for (int i = 0; i < p; ++i) {
    for (int j = 0; j < p; ++j) {
      cin >> adjs[i][j];
    }
  }
  cout << sol.minimize(s, n, p, adjs) << "\n";
  return 0;
}
