You've got string *s*, consisting of small English letters. Some of the English letters are good, the rest are bad.

A substring *s*[*l*...*r*] (1 ≤ *l* ≤ *r* ≤ |*s*|) of string *s*  =  *s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>...*s*<sub class="lower-index">|*s*|</sub> (where |*s*| is the length of string *s*) is string  *s*<sub class="lower-index">*l*</sub>*s*<sub class="lower-index">*l* + 1</sub>...*s*<sub class="lower-index">*r*</sub>.

The substring *s*[*l*...*r*] is good, if among the letters  *s*<sub class="lower-index">*l*</sub>, *s*<sub class="lower-index">*l* + 1</sub>, ..., *s*<sub class="lower-index">*r*</sub> there are at most *k* bad ones (look at the sample's explanation to understand it more clear).

Your task is to find the number of distinct good substrings of the given string *s*. Two substrings *s*[*x*...*y*] and *s*[*p*...*q*] are considered distinct if their content is different, i.e. *s*[*x*...*y*] ≠ *s*[*p*...*q*].
The first line of the input is the non-empty string *s*, consisting of small English letters, the string's length is at most 1500 characters.

The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the *i*-th character of this string equals "1", then the *i*-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.

The third line of the input consists a single integer *k* (0 ≤ *k* ≤ |*s*|) — the maximum acceptable number of bad characters in a good substring.
import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.math.BigInteger;
 
public class Main {
    static Kattio io = new Kattio();
    static int mod = 1000003559;
    static int mod2 = 998244353;
    
    public static void main(String[] args) {
        Main obj = new Main();
        char[] a = io.next().toCharArray();
        char[] b = io.next().toCharArray();
        obj.solve(a, b, io.nextInt());
        io.close();
    }

    public void solve(char[] a, char[] b, int cnt) {
        var p = getDoubleHashingBase();
        int p1 = p[0];
        int p2 = p[1];
        boolean[] good = new boolean[26];
        for (int i = 0; i < 26; i++) {
            if (b[i] == '1') good[i] = true;
        }

        Set<String> set = new HashSet();
        for (int i = 0; i < a.length; i++) {
            int k = cnt;
            StringBuilder sb = new StringBuilder();
            for (int j = i; j < a.length; j++) {
                sb.append(a[j]);
                if (!good[a[j] - 'a']) k--;
                if (k >= 0) {
                    set.add(sb.toString());
                } else break;
            }
        }
        io.println(set.size());
    }

    // shuang ha xi
    public int[] getDoubleHashingBase() {
        Random rnd = new Random();
        int p1 = 131 + rnd.nextInt(233);
        int p2 = 114 + rnd.nextInt(514);
        return new int[]{p1, p2};
    }

    // dong tai kai dian segment tree
    class Node {
        Node left;
        Node right;
        int a;
        int b;
        int c;
        int l;
        int r;
        public Node(int x, int y) {
            l = x;
            r = y;
        }

        public int[] query(int x, int y) {
            if (l == x && r == y) return new int[]{a, b, c};

            int m = (l + r) >> 1;
            if (m >= y) {
                return left.query(x, y);
            } else if (m < x) {
                return right.query(x, y);
            } else {
                int[] la = left.query(x, m);
                int[] ra = right.query(m + 1, y);
                int t = Math.min(la[1], ra[2]);
                return new int[]{la[0] + ra[0] + t, la[1] + ra[1] - t, la[2] + ra[2] - t};
            }
        }
    }

    public void swap(int i, int j, int[] a) {
        a[i] ^= a[j];
        a[j] ^= a[i];
        a[i] ^= a[j];
    }

    void shuffleArray(long[] arr){
        int n = arr.length;
        Random rnd = new Random();
        for(int i=0; i<n; ++i){
            long tmp = arr[i];
            int randomPos = i + rnd.nextInt(n-i);
            arr[i] = arr[randomPos];
            arr[randomPos] = tmp;
        }   
    }

    void shuffleArray(int[] arr){
        int n = arr.length;
        Random rnd = new Random();
        for(int i = n - 1; i >= 0; i--){
            int tmp = arr[i];
            int randomPos = rnd.nextInt(i + 1);
            arr[i] = arr[randomPos];
            arr[randomPos] = tmp;
        }   
    }

    // union find
    class UnionFind {
        int[] p;
        public UnionFind(int n) {
            this.p = new int[n];
            for (int i = 0; i < n; i++) {
                this.p[i] = i;
            }
        }
        
        public void union(int a, int b) {
            p[find(a)] = find(b);
        }

        public int find(int a) {
            return p[a] == a ? a : (p[a] = find(p[a]));
        }

        public boolean isUnioned(int a, int b) {
            return find(a) == find(b);
        }
    }


    // union find with cnt
    class UnionFind1 {
        int[] p;
        int[] cnt;
        public UnionFind1(int n) {
            this.p = new int[n];
            for (int i = 0; i < n; i++) {
                this.p[i] = i;
            }
            cnt = new int[n];
            for (int i = 0; i < n; i++) cnt[i] = 1;
        }
        
        public void union(int a, int b) {
            if (!isUnioned(a, b)) {
                int pa = find(a);
                int pb = find(b);
                cnt[pb] += cnt[pa];
                p[pa] = pb;
            }
        }

        public int find(int a) {
            return p[a] == a ? a : (p[a] = find(p[a]));
        }

        public boolean isUnioned(int a, int b) {
            return find(a) == find(b);
        }

        public int cnt(int a) {
            return cnt[find(a)];
        }
    }

    // 树状数组
    public int sum(int x, int[] tree) {
        int ans = 0;
        for (int i = x; i > 0; i -= lowbit(i)) {
            ans += tree[i];
        }
        return ans;
    }

    public void update(int x, int y, int[] tree) {
        for (int i = x; i < tree.length; i += lowbit(i)) {
            tree[i] += y;
        }
    }

    public int lowbit(int x) {
        return (~x + 1) & x;
    }

    public int gcd(int a, int b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    // modular inverse for combination
    class ModInverse {
        long[] p, pi;
        int m;
 
        public ModInverse(int n, int mod) {
            n++;
            m = mod;
            p = new long[n];
            pi = new long[n];
            p[0] = 1;
            pi[0] = 1;
            for (int i = 1; i < n; i++) {
                p[i] = p[i - 1] * i % m;
            }
            pi[n - 1] = BigInteger.valueOf(p[n - 1])
                    .modInverse(BigInteger.valueOf(m)).longValue();
            for (int i = n - 1; i > 1; i--) {
                pi[i - 1] = pi[i] * i % m;
            }
        }

        // C(n, r)
        public long comb(int n, int r) {
            if (n < r) return 0;
            return p[n] * pi[r] % m * pi[n - r] % m;
        }

        // P(n, r)
        public long perm(int n, int r) {
            if (n < r) return 0;
            return p[n] * pi[n - r] % m;
        }
    }
}
 
class Kattio extends PrintWriter {
    private BufferedReader r;
    private StringTokenizer st;
    // 标准 IO
    public Kattio() { this(System.in, System.out); }
    public Kattio(InputStream i, OutputStream o) {
        super(o);
        r = new BufferedReader(new InputStreamReader(i));
    }
    // 文件 IO
    public Kattio(String intput, String output) throws IOException {
        super(output);
        r = new BufferedReader(new FileReader(intput));
    }
    // 在没有其他输入时返回 null
    public String next() {
        try {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(r.readLine());
            return st.nextToken();
        } catch (Exception e) {}
        return null;
    }
    public int nextInt() { return Integer.parseInt(next()); }
    public double nextDouble() { return Double.parseDouble(next()); }
    public long nextLong() { return Long.parseLong(next()); }
}