//Date: 22/02/19 21:02
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
#define inc(i,n) for (int i=0;i<n;i++)

int ok[17][17],b[17][17],ans[1<<18],cnt[17];

int m; char a[100010];
vector<int> ed[17];
bool fib[1<<18];

void dfsmark(int s,int ec1, int ec2){
	inc(i,m) if (i!=ec1 && i!=ec2 && (s>>i&1)==0 && !fib[s|1<<i]){
		fib[s|1<<i]=1;
		dfsmark(s|1<<i,ec1,ec2);
	}
}
void dfs(int s, int sum){
	if (ans[s]>=0) return;
	ans[s]=sum;
	inc(i,m) if ((s>>i&1)==0 && !fib[s|1<<i]){
		dfs(s|1<<i,sum-cnt[i]);
		ans[s]=min(ans[s],ans[s|1<<i]);
	}
}

int main(){
	int n; cin>>n>>m>>a;
	inc(i,n) cnt[a[i]-'a']++;
	inc(i,m) inc(j,m) cin>>ok[i][j];
	inc(i,m) {
		int mask=-1;
		inc(j,n){
			if (a[j]-'a'==i) {
				if (mask && !ok[i][i]) fib[mask]=1,dfsmark(mask,i,i);
				mask=0;
			}
			else if (mask>=0){
				if (!ok[i][a[j]-'a']) fib[mask]=1,dfsmark(mask,i,a[j]-'a');
				mask|=1<<a[j]-'a';
			}
		}
	}
	//inc(i,1<<m) cout<<fib[i]<<' ';cout<<'\n';
	memset(ans,-1,sizeof ans);
	dfs(0,n);
	cout<<ans[0];
	return 0;
}
