/*input
3 3
cbc
0 0 0
0 0 1
0 1 0

7 4
bacadab
0 1 1 1
1 0 0 0
1 0 0 0
1 0 0 0

7 3
abacaba
1 1 1
1 0 0
1 0 0

*/
#include <bits/stdc++.h>
using namespace std;
void build() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}
#define pii pair<int, int>
#define int long long
#define F first
#define S second
const int MX = 3e5 + 100;
const int oo = 1ll << 60;
int v[MX], dp[MX];
int T[MX * 4];
void insert(int n, int l, int r, int p, int v) {
	T[n] |= v;
	if(l == r) return;
	int m = l + r >> 1;
	if(p <= m) insert(n * 2 + 1,     l, m, p, v);
	if(p >  m) insert(n * 2 + 2, m + 1, r, p, v);
}
int query(int n, int l, int r, int s, int t) {
	if(l == s and r == t) return T[n];
	int m = l + r >> 1;
	if(t <= m) return query(n * 2 + 1,     l, m, s, t);
	if(s >  m) return query(n * 2 + 2, m + 1, r, s, t);
	return query(n * 2 + 1, l, m, s, m) | query(n * 2 + 2, m + 1, r, m + 1, t);
}
int Yeee[1 << 18], pass[1 << 18], haha[1 << 18];
int ptr[20], cnt[20], ok[20][20], p;
void dfs2(int v) {
	if(Yeee[v] or haha[v]) return;
	haha[v] = true;
	for(int i = 0; i < p; i ++) {
		if(!(v & (1 << i))) dfs2(v ^ (1 << i));
	}
}
main () {
	build();
	int n; cin >> n >> p;
	string s; cin >> s; s = " " + s;
	for(int i = 0; i < p; i ++) {
		for(int j = 0; j < p; j ++) {
			cin >> ok[i][j];
		}
	}
	for(int i = 1; i <= n; i ++) {
		vector<int> GG;
		for(int j = 0; j < p; j ++) {
			if(!ok[j][s[i] - 'a'] and ptr[j]) {
				int val = query(0, 1, n, ptr[j] + 1, i - 1);
				Yeee[val           ] +=  1;
				Yeee[val | (1 << j)] += -1;
			}
		}
		ptr[s[i] - 'a'] = i;
		cnt[s[i] - 'a'] ++;
		insert(0, 1, n, i, 1 << (s[i] - 'a'));
	}
	for(int i = 0; i < p; i ++) {
		for(int j = 0; j < (1 << p); j ++) {
			if(j & (1 << i)) Yeee[j] += Yeee[j ^ (1 << i)];
		}
	}
	dfs2(0);
	int ans = n;
	// for(int i = 0; i < (1 << p); i ++) cout << Yeee[i] << ' '; cout << endl;
	for(int i = 0; i < (1 << p); i ++) {
		if(haha[i]) {
			int sum = n;
			for(int j = 0; j < p; j ++) {
				if((1 << j) & i) sum -= cnt[j];
			}
			ans = min(ans, sum);
		}
	}
	cout << ans;
}