import java.io.*;
import java.util.*;

public class F implements Runnable {
	public static void main (String[] args) {new Thread(null, new F(), "_cf", 1 << 28).start();}
	
	int n, p;
	char[] str;
	int[][] adj;
	
	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("");

		n = fs.nextInt(); p = fs.nextInt();
		str = fs.next().toCharArray();
		adj = new int[p][p];
		int[] freq = new int[p];
		for(int i = 0; i < p; i++) adj[i] = fs.nextIntArray(p);
		int[] vs = new int[n];
		for(int i = 0; i < n; i++) {
			vs[i] = 1 << (str[i]-'a');
			freq[str[i] - 'a']++;
		}
		RMQ rmq = new RMQ(vs);
		int[][] right = new int[p][n], left = new int[p][n];
		int[] last = new int[p]; Arrays.fill(last, n);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < p; j++) left[j][i] = last[j];
			last[str[i]-'a'] = i;
		}
		Arrays.fill(last, -1);
		for(int i = n-1; i >= 0; i--) {
			for(int j = 0; j < p; j++) right[j][i] = last[j];
			last[str[i]-'a'] = i;
		}
		
		ArrayList<Integer>[] badMasks = new ArrayList[p];
		for(int i = 0; i < p; i++) badMasks[i] = new ArrayList<Integer>();
		for(int i = 0; i < n; i++) {
			int me = str[i]-'a';
			for(int j = 0; j < p; j++) if(adj[me][j] == 0) {
				int idx = left[j][i];
				if(idx != n) {
					int msk = rmq.query(idx+1, i-1);
					badMasks[me].add(msk);
				}
				idx = right[j][i];
				if(idx != -1) {
					int msk = rmq.query(i+1, idx-1);
					badMasks[me].add(msk);
				}
			}
		}
		
		//0 = dead 1 = alive
		for(int i = 0; i < p; i++) Collections.sort(badMasks[i]);
		boolean[] bad = new boolean[1 << p];
		int AND = bad.length-1;
		for(int mask = 0; mask < bad.length; mask++) {
			int dead = (~mask) & AND;
			for(int i = 0; i < p; i++) {
				int on = (1 << i) & mask;
				if(on == 0) continue;
				for(int j = 0; j < badMasks[i].size() && !bad[mask]; j++) {
					int now = badMasks[i].get(j);
					on = dead & now;
					if(on == now) bad[mask] = true;
					int k = j;
					while(k < badMasks[i].size() && badMasks[i].get(k) == badMasks[i].get(j)) k++;
					j = --k;
				}
			}
		}
		
		int[] dp = new int[1 << p];
		Arrays.fill(dp, n);
		int res = n;
		for(int mask = dp.length-1; mask >= 0; mask--) if(!bad[mask]) {
			//who to delete next?
			for(int i = 0; i < p; i++) {
				int on = mask & (1<<i);
				if(on == 0) continue;
				int nmask = mask ^ (1 << i);
				if(bad[nmask]) continue;
				dp[nmask] = Math.min(dp[nmask], dp[mask] - freq[i]);
			}
			res = Math.min(res, dp[mask]);
		}
		
		out.println(res);
		out.close();
	}
	
	class RMQ {
		int[] vs;
		int[][] lift;

		public RMQ(int[] vs) {
			this.vs = vs;
			int n = vs.length;
			int maxlog = Integer.numberOfTrailingZeros(Integer.highestOneBit(n)) + 2;
			lift = new int[maxlog][n];
			for (int i = 0; i < n; i++)
				lift[0][i] = vs[i];
			int lastRange = 1;
			for (int lg = 1; lg < maxlog; lg++) {
				for (int i = 0; i < n; i++) {
					lift[lg][i] = lift[lg - 1][i] | lift[lg - 1][Math.min(i + lastRange, n - 1)];
				}
				lastRange *= 2;
			}
		}

		public int query(int low, int hi) {
			if(hi < low) return 0;
			int range = hi - low + 1;
			int exp = Integer.highestOneBit(range);
			int lg = Integer.numberOfTrailingZeros(exp);
			return lift[lg][low] | lift[lg][hi - exp + 1];
		}
	}
	
	String pad(String s, int p) {
		while(s.length() < p) s = "0" + s;
		return s;
	}
	
	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

	
}