#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cmath>
#include <utility>

#define endl '\n'
#define _ ios::sync_with_stdio(false), cin.tie(NULL)
#define iread freopen("i1.txt", "r", stdin)

using namespace std;

// clock_t tstart;void sc(){tstart=clock();} void pc(){printf("time=%.3f\n", (clock()-tstart)/(CLOCKS_PER_SEC*1.0));}

int t;

long long l, r;
string sl, sr;

long long calc(int pos, int n, string x, int nonzero, bool fl) {
	if(pos==n) return 1;
	long long ans=0;
	if(nonzero<3) {
		for(long long i=1; i<=9; ++i) {
			if(fl || (!fl && i<=x[pos]-'0'))
				ans+=calc(pos+1, n, x, nonzero+1, fl|(i<x[pos]-'0'));
		}
	}
	ans+=calc(pos+1, n, x, nonzero, fl|(0<x[pos]-'0'));
	return ans;
}

int main () {
    //iread;
    _;
    cin>>t;
    while(t--) {
    	cin>>l>>r;
    	sl=sr="";
    	l--;
    	if(l==0) sl+='0';
    	while(l>0) {
    		sl+=(l%10)+'0';
    		l/=10;
    	}
    	if(r==0) sr+='0';
    	while(r>0) {
    		sr+=(r%10)+'0';
    		r/=10;
    	}
    	while((int)sl.size()<(int)sr.size()) sl+='0';
    	reverse(sl.begin(), sl.end());
    	reverse(sr.begin(), sr.end());
    	//cout<<sl<<' '<<sr<<endl;
    	cout<<calc(0, (int)sr.size(), sr, 0, 0)-calc(0, (int)sl.size(), sl, 0, 0)<<endl;
    }
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
ll o[1000005],L,R;int T,len;
void dfs(int i,int j,ll sum){
	if (i==18) {o[++len]=sum;return;}
	dfs(i+1,j,sum*10);
	if (j<3)
		for (int k=1;k<=9;++k)
			dfs(i+1,j+1,sum*10+k);
}
int main(){
	dfs(0,0,0);o[++len]=1e18;
	scanf("%d",&T);while (T--){
		scanf("%I64d%I64d",&L,&R);
		printf("%d\n",upper_bound(o+1,o+len+1,R)-lower_bound(o+1,o+len+1,L));
	}
	return 0;
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st=new StringTokenizer("");
    static BufferedReader br;

    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////

        String s=ns();
        String a=ns();
        int k=ni();
        int dp[]=new int[s.length()+1];
        for(int i=0;i<s.length();i++)
            if(a.charAt((int)s.charAt(i)-'a')=='0') dp[i+1]=1;
        for(int i=1;i<dp.length;i++) dp[i]+=dp[i-1];
        int cnt=0;
        int n=s.length();
        for (int i = 1; i <=n; i++)
        {
            HashSet<String> hashSet = new HashSet<>();
            for (int j = 0; j + i <=n && j<n ; j++)
            {
                String l = s.substring(j,j + i);
                if (dp[j + i] - dp[j] > k || hashSet.contains(l))continue;
                    hashSet.add(l);
            }
            cnt += hashSet.size();
        }
        out.print(cnt);

        //////////////////////////////////////////////////////////////////////////////////////
        out.flush();
	out.close();
    }

    ///////////////////////////////////////////////////////////////////////////////

    private static long gcd(long a,long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(second>  o.second)
                return 1;
            else if(second < o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st;//=new StringTokenizer("");
    static BufferedReader br;
    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////

        String s = br.readLine();
        String s2 = br.readLine();
        long MOD = Long.parseLong("9223372036854775783");
        int k = Integer.parseInt(br.readLine());
        boolean[] good = new boolean[26];
        for(int i=0; i<26; i++) good[i] = (s2.charAt(i) == '1');
        int[] count = new int[s.length() + 1];
        count[0] = 0;
        Set<Long> codes = new HashSet<Long>();
        for(int i=1; i<=s.length(); i++) count[i] = count[i-1] + (good[s.charAt(i-1)-'a'] ? 0 : 1);
        for(int start = 0; start<s.length(); start++) {
                long code = 0;
                for(int end = start+1; end<=s.length(); end++) {
                        code *= 29; code += s.charAt(end-1) - 'a' + 1; code %= MOD;
                        if(count[end] - count[start] <= k) {
                                //System.out.println(cur + " " + code);
                                codes.add(code);
                        }
                }
        }

        //int c = 0;
        //for(int i=0; i<codes.length; i++) if(codes[i]) c++;
        System.out.println(codes.size());

        //////////////////////////////////////////////////////////////////////////////////////
	out.close();
    }

    static class RMQ
    {
        int INF=(1<<30)-1;
        int bit[];
        int n1;
        RMQ(int n)
        {
            n1=1;
            for(;n1<n;n1*=2);
            bit=new int[2*n1-1];
            for(int i=0;i<2*n1-1;i++)
                bit[i]=INF;
        }
        void insert(int i,int val)
        {
            i+=n1-1;
            bit[i]=val;
            while(i>0)
            {
                i=(i-1)/2;
                bit[i]=bit[2*i+1]&bit[2*i+2];
            }
        }
        int and(int a,int b,int k,int l,int r)
        {
            if(r<=a || l>=b) return INF;
            if(l>=a && r<=b) return bit[k];
            int v1=and(a,b,2*k+1,l,(l+r)/2);
            int v2=and(a,b,2*k+2,(l+r)/2,r);
            return v1&v2;
        }
    }
    static class BIT
    {
        private int c[];
        BIT(int n){c=new int[n+1];}
        public int lowbit(int i)
        {
            return i&(i^(i-1));
        }
        public int sum(int k)
        {
            int res=0;
            while(k>0)
            {
                res+=c[k];
                k-=lowbit(k);
            }
            return res;
        }
        public void insert(int i,int v)
        {
            while(i<c.length)
            {
                c[i]+=v;
                i+=lowbit(i);
            }
        }
    }
    ///////////////////////////////////////////////////////////////////////////////

    private static int gcd(int a,int b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(first+second >  o.first+o.second)
                return 1;
            else if(first+second < o.first+o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st=new StringTokenizer("");
    static BufferedReader br;

    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////

        String s=ns();
        String a=ns();
        int k=ni();
        int dp[]=new int[s.length()+1];
        for(int i=0;i<s.length();i++)
            if(a.charAt((int)s.charAt(i)-'a')=='0') dp[i+1]=1;
        for(int i=1;i<dp.length;i++) dp[i]+=dp[i-1];
        HashSet<String> set=new HashSet<String>();
        for(int i=0;i<s.length();i++)
        {
            StringBuffer temp=new StringBuffer("");
            for(int j=i;j<s.length();j++)
            {
                temp.append(s.charAt(j));
                if(dp[j+1]-dp[i]<=k) set.add(temp.toString());
                else break;
            }
        }
        out.print(set.size());

        //////////////////////////////////////////////////////////////////////////////////////
        out.flush();
	out.close();
    }

    ///////////////////////////////////////////////////////////////////////////////

    private static long gcd(long a,long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(second>  o.second)
                return 1;
            else if(second < o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st=new StringTokenizer("");
    static BufferedReader br;

    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////

        String s=ns();
        String a=ns();
        int k=ni();
        int dp[]=new int[s.length()+1];
        for(int i=0;i<s.length();i++)
            if(a.charAt((int)s.charAt(i)-'a')=='0') dp[i+1]=1;
        for(int i=1;i<dp.length;i++) dp[i]+=dp[i-1];
        int cnt=0;
        for(int i=1;i<=s.length();i++)
        {
            HashSet<String> set=new HashSet<String>();
            for(int j=0;j+i<=s.length();j++)
            {
                String temp=s.substring(j,j+i);
                if(dp[j+i]-dp[j]>k || set.contains(temp) )continue;
                set.add(temp);
            }
            cnt+=set.size();
        }
        out.print(cnt);

        //////////////////////////////////////////////////////////////////////////////////////
        out.flush();
	out.close();
    }

    ///////////////////////////////////////////////////////////////////////////////

    private static long gcd(long a,long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(second>  o.second)
                return 1;
            else if(second < o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st=new StringTokenizer("");
    static BufferedReader br;
    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////

        String s = ns();
        String all = ns();
        int []good = new int[26];
        for (int i = 0; i < 26; i++) {
            good[i] = (all.codePointAt(i) - 48);
        }
        int k = ni();
        int []sum = new int[s.length() + 2];
        int n = s.length();
        int lp = 0;
        for (int i = 0; i < n; i++) {
            sum[i + 1] = sum[i] + (1 - good[s.codePointAt(i) - 97]);
            if (s.charAt(i) == 'a')lp++;
        }
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            HashSet<String> hashSet = new HashSet<>();
            for (int j = 0; j + i <= n && j < n; j++) {
                String l = s.substring(j,j + i);
                if (sum[j + i] - sum[j] > k || hashSet.contains(l))continue;
                    hashSet.add(l);
            }
            cnt += hashSet.size();
        }
        out.print(cnt);

        //////////////////////////////////////////////////////////////////////////////////////
	out.close();
    }

    static class RMQ
    {
        int INF=(1<<30)-1;
        int bit[];
        int n1;
        RMQ(int n)
        {
            n1=1;
            for(;n1<n;n1*=2);
            bit=new int[2*n1-1];
            for(int i=0;i<2*n1-1;i++)
                bit[i]=INF;
        }
        void insert(int i,int val)
        {
            i+=n1-1;
            bit[i]=val;
            while(i>0)
            {
                i=(i-1)/2;
                bit[i]=bit[2*i+1]&bit[2*i+2];
            }
        }
        int and(int a,int b,int k,int l,int r)
        {
            if(r<=a || l>=b) return INF;
            if(l>=a && r<=b) return bit[k];
            int v1=and(a,b,2*k+1,l,(l+r)/2);
            int v2=and(a,b,2*k+2,(l+r)/2,r);
            return v1&v2;
        }
    }
    static class BIT
    {
        private int c[];
        BIT(int n){c=new int[n+1];}
        public int lowbit(int i)
        {
            return i&(i^(i-1));
        }
        public int sum(int k)
        {
            int res=0;
            while(k>0)
            {
                res+=c[k];
                k-=lowbit(k);
            }
            return res;
        }
        public void insert(int i,int v)
        {
            while(i<c.length)
            {
                c[i]+=v;
                i+=lowbit(i);
            }
        }
    }
    ///////////////////////////////////////////////////////////////////////////////

    private static int gcd(int a,int b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(first+second >  o.first+o.second)
                return 1;
            else if(first+second < o.first+o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st=new StringTokenizer("");
    static BufferedReader br;

    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////

        String s=ns();
        String a=ns();
        int k=ni();
        int dp[]=new int[s.length()+1];
        for(int i=0;i<s.length();i++)
            if(a.charAt((int)s.charAt(i)-'a')=='0') dp[i+1]=1;
        for(int i=1;i<dp.length;i++) dp[i]+=dp[i-1];
        int cnt=0;
        for (int i = 1; i <= s.length(); i++)
        {
            HashSet<String> hashSet = new HashSet<>();
            for (int j = 0; j + i <= s.length() ; j++)
            {
                String l = s.substring(j,j + i);
                if (dp[j + i] - dp[j] > k || hashSet.contains(l))continue;
                    hashSet.add(l);
            }
            cnt += hashSet.size();
        }
        out.print(cnt);

        //////////////////////////////////////////////////////////////////////////////////////
        out.flush();
	out.close();
    }

    ///////////////////////////////////////////////////////////////////////////////

    private static long gcd(long a,long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(second>  o.second)
                return 1;
            else if(second < o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main
{
    static StringTokenizer st;//=new StringTokenizer("");
    static BufferedReader br;

    ///////////////////////////////////////////////////////////////////////////////////

    public static void main(String args[]) throws FileNotFoundException, IOException, Exception
    {
        //Scanner in =new Scanner(new FileReader("input.txt"));
        //PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
        //Scanner in=new Scanner(System.in);
        br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out=new PrintWriter(System.out);
        //StringTokenizer st;//=new StringTokenizer(br.readLine());
        //////////////////////////////////////////////////////////////////////////////////////
        st=new StringTokenizer(br.readLine());
        String s=st.nextToken();
        st=new StringTokenizer(br.readLine());
        String a=st.nextToken();
        int k=ni();
        int dp[]=new int[s.length()+1];
        for(int i=0;i<s.length();i++)
            if(a.charAt((int)s.charAt(i)-'a')=='0') dp[i+1]=1;
        for(int i=1;i<dp.length;i++) dp[i]+=dp[i-1];
        int cnt=0;
        int n=s.length();
        for (int i = 1; i <=n; i++)
        {
            HashSet<String> hashSet = new HashSet<>();
            for (int j = 0; j + i <=n && j<n ; j++)
            {
                String l = s.substring(j,j + i);
                if (dp[j + i] - dp[j] > k || hashSet.contains(l))continue;
                    hashSet.add(l);
            }
            cnt += hashSet.size();
        }
        out.print(cnt);

        //////////////////////////////////////////////////////////////////////////////////////
        out.flush();
	out.close();
    }

    ///////////////////////////////////////////////////////////////////////////////

    private static long gcd(long a,long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    private static class pair implements Comparable<pair>
    {
        int first;
        int second;
        pair()
        {
            first=0;
            second=0;
        }
        pair(int f,int s)
        {
            first=f;
            second=s;
        }

        public int compareTo(pair o)
        {
            if(second>  o.second)
                return 1;
            else if(second < o.second)
                return -1;
            else
                return 0;
        }
    }

    public static Integer ni() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    public static BigInteger nb()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return BigInteger.valueOf(Long.parseLong(st.nextToken()));
    }
    public static Long nl() throws Exception
    {
        if(!st.hasMoreTokens())
            st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    public static Double nd()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    public static String ns()throws Exception
    {
        if(!st.hasMoreElements())
            st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class GoodSubstring
{

    /*
     * Create the LCP array from the suffix array
     * @param s the input array populated from 0..N-1, with available pos N
     * @param sa the already-computed suffix array 0..N-1
     * @param LCP the resulting LCP array 0..N-1
     */
    public static void makeLCPArray( int [ ] s, int [ ] sa, int [ ] LCP )
    {
        int N = sa.length;
        int [ ] rank = new int[ N ];

        s[ N ] = -1;
        for( int i = 0; i < N; i++ )
            rank[ sa[ i ] ] = i;

        int h = 0;
        for( int i = 0; i < N; i++ )
            if( rank[ i ] > 0 )
            {
                int j = sa[ rank[ i ] - 1 ];

                while( s[ i + h ] == s[ j + h ] )
                    h++;

                LCP[ rank[ i ] ] = h;
                if( h > 0 )
                    h--;
            }
    }

    /*
     * Fill in the suffix array information for String str
     * @param str the input String
     * @param sa existing array to place the suffix array
     */
    public static void createSuffixArray( String str, int [ ] sa, int [ ] LCP )
    {
        int N = str.length( );

        int [ ] s = new int[ N + 3 ];
        int [ ] SA = new int[ N + 3 ];

        for( int i = 0; i < N; i++ )
            s[ i ] = str.charAt( i );

        makeSuffixArray( s, SA, N, 256 );

        for( int i = 0; i < N; i++ )
            sa[ i ] = SA[ i ];

        makeLCPArray( s, sa, LCP );
    }

    // find the suffix array SA of s[0..n-1] in {1..K}^n
    // require s[n]=s[n+1]=s[n+2]=0, n>=2
    public static void makeSuffixArray( int [ ] s, int [ ] SA, int n, int K )
    {
        int n0 = ( n + 2 ) / 3;
        int n1 = ( n + 1 ) / 3;
        int n2 = n / 3;
        int t = n0 - n1;  // 1 iff n%3 == 1
        int n12 = n1 + n2 + t;

        int [ ] s12  = new int[ n12 + 3 ];
        int [ ] SA12 = new int[ n12 + 3 ];
        int [ ] s0   = new int[ n0 ];
        int [ ] SA0  = new int[ n0 ];

        // generate positions in s for items in s12
        // the "+t" adds a dummy mod 1 suffix if n%3 == 1
        // at that point, the size of s12 is n12
        for( int i = 0, j = 0; i < n + t; i++ )
            if( i % 3 != 0 )
                s12[ j++ ] = i;

        int K12 = assignNames( s, s12, SA12, n0, n12, K );

        computeS12( s12, SA12, n12, K12 );
        computeS0( s, s0, SA0, SA12, n0, n12, K );
        merge( s, s12, SA, SA0, SA12, n, n0, n12, t );
    }

    // Assigns the new supercharacter names.
    // At end of routine, SA will have indices into s, in sorted order
    // and s12 will have new character names
    // Returns the number of names assigned; note that if
    // this value is the same as n12, then SA is a suffix array for s12.
    private static int assignNames( int [ ] s, int [ ] s12, int [ ] SA12,
                                   int n0, int n12, int K )
    {
           // radix sort the new character trios
        radixPass( s12 , SA12, s, 2, n12, K );
        radixPass( SA12, s12 , s, 1, n12, K );
        radixPass( s12 , SA12, s, 0, n12, K );

          // find lexicographic names of triples
        int name = 0;
        int c0 = -1, c1 = -1, c2 = -1;

        for( int i = 0; i < n12; i++ )
        {
            if( s[ SA12[ i ] ] != c0 || s[ SA12[ i ] + 1 ] != c1
                                     || s[ SA12[ i ] + 2 ] != c2 )
            {
                name++;
                c0 = s[ SA12[ i ] ];
                c1 = s[ SA12[ i ] + 1 ];
                c2 = s[ SA12[ i ] + 2 ];
            }

            if( SA12[ i ] % 3 == 1 )
                s12[ SA12[ i ] / 3 ]      = name;
            else
                s12[ SA12[ i ] / 3 + n0 ] = name;
       }

       return name;
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]; sort is relative to offset into s
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int offset,
                                  int n, int K )
    {
        int [ ] count = new int[ K + 2 ];            // counter array

        for( int i = 0; i < n; i++ )
            count[ s[ in[ i ] + offset ] + 1 ]++;    // count occurences

        for( int i = 1; i <= K + 1; i++ )            // compute exclusive sums
            count[ i ] += count[ i - 1 ];

        for( int i = 0; i < n; i++ )
            out[ count[ s[ in[ i ] + offset ] ]++ ] = in[ i ];      // sort
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int n, int K )
    {
        radixPass( in, out, s, 0, n, K );
    }

    // Compute the suffix array for s12, placing result into SA12
    private static void computeS12( int [ ] s12, int [ ] SA12, int n12, int K12 )
    {
        if( K12 == n12 ) // if unique names, don't need recursion
            for( int i = 0; i < n12; i++ )
                SA12[ s12[i] - 1 ] = i;
        else
        {
            makeSuffixArray( s12, SA12, n12, K12 );
            // store unique names in s12 using the suffix array
            for( int i = 0; i < n12; i++ )
                s12[ SA12[ i ] ] = i + 1;
        }
    }

    private static void computeS0( int [ ] s, int [ ] s0, int [ ] SA0, int [ ] SA12,
                               int n0, int n12, int K )
    {
        for( int i = 0, j = 0; i < n12; i++ )
            if( SA12[ i ] < n0 )
                s0[ j++ ] = 3 * SA12[ i ];

        radixPass( s0, SA0, s, n0, K );
    }

    // merge sorted SA0 suffixes and sorted SA12 suffixes
    private static void merge( int [ ] s, int [ ] s12,
                              int [ ] SA, int [ ] SA0, int [ ] SA12,
                              int n, int n0, int n12, int t )
    {
        int p = 0, k = 0;

        while( t != n12 && p != n0 )
        {
            int i = getIndexIntoS( SA12, t, n0 ); // S12
            int j = SA0[ p ];                     // S0

            if( suffix12IsSmaller( s, s12, SA12, n0, i, j, t ) )
            {
                SA[ k++ ] = i;
                t++;
            }
            else
            {
                SA[ k++ ] = j;
                p++;
            }
        }

        while( p < n0 )
            SA[ k++ ] = SA0[ p++ ];
        while( t < n12 )
            SA[ k++ ] = getIndexIntoS( SA12, t++, n0 );
    }

    private static int getIndexIntoS( int [ ] SA12, int t, int n0 )
    {
        if( SA12[ t ] < n0 )
            return SA12[ t ] * 3 + 1;
        else
            return ( SA12[ t ] - n0 ) * 3 + 2;
    }

    private static boolean leq( int a1, int a2, int b1, int b2 )
      { return a1 < b1 || a1 == b1 && a2 <= b2; }

    private static boolean leq( int a1, int a2, int a3, int b1, int b2, int b3 )
      { return a1 < b1 || a1 == b1 && leq( a2, a3,b2, b3 ); }

    private static boolean suffix12IsSmaller( int [ ] s, int [ ] s12, int [ ] SA12,
                                             int n0, int i, int j, int t )
    {
        if( SA12[ t ] < n0 )  // s1 vs s0; can break tie after 1 character
            return leq( s[ i ], s12[ SA12[ t ] + n0 ],
                        s[ j ], s12[ j / 3 ] );
        else                  // s2 vs s0; can break tie after 2 characters
            return leq( s[ i ], s[ i + 1 ], s12[ SA12[ t ] - n0 + 1 ],
                        s[ j ], s[ j + 1 ], s12[ j / 3 + n0 ] );
    }

    public static void printV( int [ ]  a, String comment )
    {
        System.out.print( comment + ":" );
        for( int x : a )
            System.out.print( x + " " );

        System.out.println( );
    }

    public static boolean isPermutation( int [ ] SA, int n )
    {
        boolean [ ] seen = new boolean [ n ];

        for( int i = 0; i < n; i++ )
            seen[ i ] = false;

        for( int i = 0; i < n; i++ )
            seen[ SA[ i ] ] = true;

        for( int i = 0; i < n; i++ )
            if( !seen[ i ] )
                return false;

        return true;
    }

    public static boolean sleq( int  [ ] s1, int start1, int [ ] s2, int start2 )
    {
        for( int i = start1, j = start2; ; i++, j++ )
        {
            if( s1[ i ] < s2[ j ] )
                return true;

            if( s1[ i ] > s2[ j ] )
                return false;
        }
    }

    // Check if SA is a sorted suffix array for s
    public static boolean isSorted( int [ ] SA, int [ ] s, int n )
    {
        for( int i = 0; i < n-1; i++ )
            if( !sleq( s, SA[ i ], s, SA[ i + 1 ] ) )
                return false;

        return true;
    }

    public static void test( String str )
    {
        int [ ] sufarr = new int[ str.length( ) ];
        int [ ] LCP = new int[ str.length( ) ];

        createSuffixArray( str, sufarr, LCP );

        System.out.println( str + ":" );
        for( int i = 0; i < str.length( ); i++ )
            System.out.println( i + " " + sufarr[ i ] + " " + LCP[ i ] );
        System.out.println( );
    }

    public static void main( String [ ] args ) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int[]sa = new int[s.length()];
        int[]lcp = new int[s.length()];
        createSuffixArray(s, sa, lcp);
        /*for (int i = 0; i < sa.length; i++)
        {
            System.out.println(s.substring(sa[i]));

        }*/
        String allowed2 = br.readLine();
        boolean[]allowed = new boolean[26];
        for(int i = 0; i < 26; i++) {
            if(allowed2.charAt(i)=='1')
                allowed[i] = true;
        }
        int ans = 0;
        int k = Integer.parseInt(br.readLine());
        int bad = 0;
        //System.out.println();
        int prev = 0;
        for(int j = sa[0]; j < sa.length; j++) {
            if(!allowed[s.charAt(j)-'a']) {
                bad++;
                if(sa.length>1 && j-sa[0] < lcp [1] ) {
                    prev++;
                }
                if(bad>k)
                    break;
            }
                //System.out.println(s.substring(sa[0], j+1));
                ans++;
        }
        if(sa.length>1) {
            prev = Math.min(prev, lcp[1]);
        }
        for (int i = 1; i < sa.length; i++)
        {
            bad = prev;

            for(int j = sa[i] + lcp[i]; j < sa.length; j++) {
                if(!allowed[s.charAt(j)-'a']) {
                    bad++;
                    if(i < sa.length-1 && j - sa[i] < lcp[i+1]) {
                        prev++;
                    }
                    if(bad>k)
                        break;
                }
                //System.out.println(s.substring(sa[i], j+1));
                ans++;
            }
            if(i < sa.length-1) {
                prev = Math.min(prev, lcp[i+1]);
            }
        }
        System.out.println(ans);
    }

    /*
     * Returns the LCP for any two strings
     */
    public static int computeLCP( String s1, String s2 )
    {
        int i = 0;

        while( i < s1.length( ) && i < s2.length( ) && s1.charAt( i ) == s2.charAt( i ) )
            i++;

        return i;
    }

    /*
     * Fill in the suffix array and LCP information for String str
     * @param str the input String
     * @param SA existing array to place the suffix array
     * @param LCP existing array to place the LCP information
     * Note: Starting in Java 7, this will use quadratic space.
     */
    public static void createSuffixArraySlow( String str, int [ ] SA, int [ ] LCP )
    {
        if( SA.length != str.length( ) || LCP.length != str.length( ) )
            throw new IllegalArgumentException( );

        int N = str.length( );

        String [ ] suffixes = new String[ N ];
        for( int i = 0; i < N; i++ )
            suffixes[ i ] = str.substring( i );

        Arrays.sort( suffixes );

        for( int i = 0; i < N; i++ )
            SA[ i ] = N - suffixes[ i ].length( );

        LCP[ 0 ] = 0;
        for( int i = 1; i < N; i++ )
            LCP[ i ] = computeLCP( suffixes[ i - 1 ], suffixes[ i ] );
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class GoodSubstring
{

    /*
     * Create the LCP array from the suffix array
     * @param s the input array populated from 0..N-1, with available pos N
     * @param sa the already-computed suffix array 0..N-1
     * @param LCP the resulting LCP array 0..N-1
     */
    public static void makeLCPArray( int [ ] s, int [ ] sa, int [ ] LCP )
    {
        int N = sa.length;
        int [ ] rank = new int[ N ];

        s[ N ] = -1;
        for( int i = 0; i < N; i++ )
            rank[ sa[ i ] ] = i;

        int h = 0;
        for( int i = 0; i < N; i++ )
            if( rank[ i ] > 0 )
            {
                int j = sa[ rank[ i ] - 1 ];

                while( s[ i + h ] == s[ j + h ] )
                    h++;

                LCP[ rank[ i ] ] = h;
                if( h > 0 )
                    h--;
            }
    }

    /*
     * Fill in the suffix array information for String str
     * @param str the input String
     * @param sa existing array to place the suffix array
     */
    public static void createSuffixArray( String str, int [ ] sa, int [ ] LCP )
    {
        int N = str.length( );

        int [ ] s = new int[ N + 3 ];
        int [ ] SA = new int[ N + 3 ];

        for( int i = 0; i < N; i++ )
            s[ i ] = str.charAt( i );

        makeSuffixArray( s, SA, N, 256 );

        for( int i = 0; i < N; i++ )
            sa[ i ] = SA[ i ];

        makeLCPArray( s, sa, LCP );
    }

    // find the suffix array SA of s[0..n-1] in {1..K}^n
    // require s[n]=s[n+1]=s[n+2]=0, n>=2
    public static void makeSuffixArray( int [ ] s, int [ ] SA, int n, int K )
    {
        int n0 = ( n + 2 ) / 3;
        int n1 = ( n + 1 ) / 3;
        int n2 = n / 3;
        int t = n0 - n1;  // 1 iff n%3 == 1
        int n12 = n1 + n2 + t;

        int [ ] s12  = new int[ n12 + 3 ];
        int [ ] SA12 = new int[ n12 + 3 ];
        int [ ] s0   = new int[ n0 ];
        int [ ] SA0  = new int[ n0 ];

        // generate positions in s for items in s12
        // the "+t" adds a dummy mod 1 suffix if n%3 == 1
        // at that point, the size of s12 is n12
        for( int i = 0, j = 0; i < n + t; i++ )
            if( i % 3 != 0 )
                s12[ j++ ] = i;

        int K12 = assignNames( s, s12, SA12, n0, n12, K );

        computeS12( s12, SA12, n12, K12 );
        computeS0( s, s0, SA0, SA12, n0, n12, K );
        merge( s, s12, SA, SA0, SA12, n, n0, n12, t );
    }

    // Assigns the new supercharacter names.
    // At end of routine, SA will have indices into s, in sorted order
    // and s12 will have new character names
    // Returns the number of names assigned; note that if
    // this value is the same as n12, then SA is a suffix array for s12.
    private static int assignNames( int [ ] s, int [ ] s12, int [ ] SA12,
                                   int n0, int n12, int K )
    {
           // radix sort the new character trios
        radixPass( s12 , SA12, s, 2, n12, K );
        radixPass( SA12, s12 , s, 1, n12, K );
        radixPass( s12 , SA12, s, 0, n12, K );

          // find lexicographic names of triples
        int name = 0;
        int c0 = -1, c1 = -1, c2 = -1;

        for( int i = 0; i < n12; i++ )
        {
            if( s[ SA12[ i ] ] != c0 || s[ SA12[ i ] + 1 ] != c1
                                     || s[ SA12[ i ] + 2 ] != c2 )
            {
                name++;
                c0 = s[ SA12[ i ] ];
                c1 = s[ SA12[ i ] + 1 ];
                c2 = s[ SA12[ i ] + 2 ];
            }

            if( SA12[ i ] % 3 == 1 )
                s12[ SA12[ i ] / 3 ]      = name;
            else
                s12[ SA12[ i ] / 3 + n0 ] = name;
       }

       return name;
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]; sort is relative to offset into s
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int offset,
                                  int n, int K )
    {
        int [ ] count = new int[ K + 2 ];            // counter array

        for( int i = 0; i < n; i++ )
            count[ s[ in[ i ] + offset ] + 1 ]++;    // count occurences

        for( int i = 1; i <= K + 1; i++ )            // compute exclusive sums
            count[ i ] += count[ i - 1 ];

        for( int i = 0; i < n; i++ )
            out[ count[ s[ in[ i ] + offset ] ]++ ] = in[ i ];      // sort
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int n, int K )
    {
        radixPass( in, out, s, 0, n, K );
    }

    // Compute the suffix array for s12, placing result into SA12
    private static void computeS12( int [ ] s12, int [ ] SA12, int n12, int K12 )
    {
        if( K12 == n12 ) // if unique names, don't need recursion
            for( int i = 0; i < n12; i++ )
                SA12[ s12[i] - 1 ] = i;
        else
        {
            makeSuffixArray( s12, SA12, n12, K12 );
            // store unique names in s12 using the suffix array
            for( int i = 0; i < n12; i++ )
                s12[ SA12[ i ] ] = i + 1;
        }
    }

    private static void computeS0( int [ ] s, int [ ] s0, int [ ] SA0, int [ ] SA12,
                               int n0, int n12, int K )
    {
        for( int i = 0, j = 0; i < n12; i++ )
            if( SA12[ i ] < n0 )
                s0[ j++ ] = 3 * SA12[ i ];

        radixPass( s0, SA0, s, n0, K );
    }

    // merge sorted SA0 suffixes and sorted SA12 suffixes
    private static void merge( int [ ] s, int [ ] s12,
                              int [ ] SA, int [ ] SA0, int [ ] SA12,
                              int n, int n0, int n12, int t )
    {
        int p = 0, k = 0;

        while( t != n12 && p != n0 )
        {
            int i = getIndexIntoS( SA12, t, n0 ); // S12
            int j = SA0[ p ];                     // S0

            if( suffix12IsSmaller( s, s12, SA12, n0, i, j, t ) )
            {
                SA[ k++ ] = i;
                t++;
            }
            else
            {
                SA[ k++ ] = j;
                p++;
            }
        }

        while( p < n0 )
            SA[ k++ ] = SA0[ p++ ];
        while( t < n12 )
            SA[ k++ ] = getIndexIntoS( SA12, t++, n0 );
    }

    private static int getIndexIntoS( int [ ] SA12, int t, int n0 )
    {
        if( SA12[ t ] < n0 )
            return SA12[ t ] * 3 + 1;
        else
            return ( SA12[ t ] - n0 ) * 3 + 2;
    }

    private static boolean leq( int a1, int a2, int b1, int b2 )
      { return a1 < b1 || a1 == b1 && a2 <= b2; }

    private static boolean leq( int a1, int a2, int a3, int b1, int b2, int b3 )
      { return a1 < b1 || a1 == b1 && leq( a2, a3,b2, b3 ); }

    private static boolean suffix12IsSmaller( int [ ] s, int [ ] s12, int [ ] SA12,
                                             int n0, int i, int j, int t )
    {
        if( SA12[ t ] < n0 )  // s1 vs s0; can break tie after 1 character
            return leq( s[ i ], s12[ SA12[ t ] + n0 ],
                        s[ j ], s12[ j / 3 ] );
        else                  // s2 vs s0; can break tie after 2 characters
            return leq( s[ i ], s[ i + 1 ], s12[ SA12[ t ] - n0 + 1 ],
                        s[ j ], s[ j + 1 ], s12[ j / 3 + n0 ] );
    }

    public static void printV( int [ ]  a, String comment )
    {
        System.out.print( comment + ":" );
        for( int x : a )
            System.out.print( x + " " );

        System.out.println( );
    }

    public static boolean isPermutation( int [ ] SA, int n )
    {
        boolean [ ] seen = new boolean [ n ];

        for( int i = 0; i < n; i++ )
            seen[ i ] = false;

        for( int i = 0; i < n; i++ )
            seen[ SA[ i ] ] = true;

        for( int i = 0; i < n; i++ )
            if( !seen[ i ] )
                return false;

        return true;
    }

    public static boolean sleq( int  [ ] s1, int start1, int [ ] s2, int start2 )
    {
        for( int i = start1, j = start2; ; i++, j++ )
        {
            if( s1[ i ] < s2[ j ] )
                return true;

            if( s1[ i ] > s2[ j ] )
                return false;
        }
    }

    // Check if SA is a sorted suffix array for s
    public static boolean isSorted( int [ ] SA, int [ ] s, int n )
    {
        for( int i = 0; i < n-1; i++ )
            if( !sleq( s, SA[ i ], s, SA[ i + 1 ] ) )
                return false;

        return true;
    }

    public static void test( String str )
    {
        int [ ] sufarr = new int[ str.length( ) ];
        int [ ] LCP = new int[ str.length( ) ];

        createSuffixArray( str, sufarr, LCP );

        System.out.println( str + ":" );
        for( int i = 0; i < str.length( ); i++ )
            System.out.println( i + " " + sufarr[ i ] + " " + LCP[ i ] );
        System.out.println( );
    }

    public static void main( String [ ] args ) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int[]sa = new int[s.length()];
        int[]lcp = new int[s.length()];
        createSuffixArray(s, sa, lcp);
        /*for (int i = 0; i < sa.length; i++)
        {
            System.out.println(s.substring(sa[i]));

        }*/
        String allowed2 = br.readLine();
        boolean[]allowed = new boolean[26];
        for(int i = 0; i < 26; i++) {
            if(allowed2.charAt(i)=='1')
                allowed[i] = true;
        }
        int ans = 0;
        int k = Integer.parseInt(br.readLine());
        int bad = 0;
        //System.out.println();
        int prev = 0;
        for(int j = sa[0]; j < sa.length; j++) {
            if(!allowed[s.charAt(j)-'a']) {
                bad++;
                if(sa.length>1 && j-sa[0] < lcp [1] ) {
                    prev++;
                }
            }
            if(bad<=k) {
                //System.out.println(s.substring(sa[0], j+1));
                ans++;
            }
        }
        for (int i = 1; i < sa.length; i++)
        {
            bad = prev;
            if(lcp[i] == s.length()-sa[i-1]) {
                for(int j = sa[i] + lcp[i]; j < sa.length; j++) {
                    if(!allowed[s.charAt(j)-'a']) {
                        bad++;
                        if(i < sa.length-1 && j - sa[i] < lcp[i+1]) {
                            prev++;
                        }
                    }
                    if(bad<=k) {
                        //System.out.println(s.substring(sa[i], j+1));
                        ans++;
                    }
                }
            }
            else {
                for(int j = sa[i] + lcp[i]; j < sa.length; j++) {
                    if(!allowed[s.charAt(j)-'a']) {
                        bad++;
                        if(i < sa.length-1 && j - sa[i] < lcp[i+1]) {
                            prev++;
                        }
                    }
                    if(bad<=k) {
                        //System.out.println(s.substring(sa[i], j+1));
                        ans++;
                    }
                }
            }
            if(i < sa.length-1 && lcp[i+1] == 0)
                prev = 0;
        }
        System.out.println(ans);
    }

    /*
     * Returns the LCP for any two strings
     */
    public static int computeLCP( String s1, String s2 )
    {
        int i = 0;

        while( i < s1.length( ) && i < s2.length( ) && s1.charAt( i ) == s2.charAt( i ) )
            i++;

        return i;
    }

    /*
     * Fill in the suffix array and LCP information for String str
     * @param str the input String
     * @param SA existing array to place the suffix array
     * @param LCP existing array to place the LCP information
     * Note: Starting in Java 7, this will use quadratic space.
     */
    public static void createSuffixArraySlow( String str, int [ ] SA, int [ ] LCP )
    {
        if( SA.length != str.length( ) || LCP.length != str.length( ) )
            throw new IllegalArgumentException( );

        int N = str.length( );

        String [ ] suffixes = new String[ N ];
        for( int i = 0; i < N; i++ )
            suffixes[ i ] = str.substring( i );

        Arrays.sort( suffixes );

        for( int i = 0; i < N; i++ )
            SA[ i ] = N - suffixes[ i ].length( );

        LCP[ 0 ] = 0;
        for( int i = 1; i < N; i++ )
            LCP[ i ] = computeLCP( suffixes[ i - 1 ], suffixes[ i ] );
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class GoodSubstring
{

    /*
     * Create the LCP array from the suffix array
     * @param s the input array populated from 0..N-1, with available pos N
     * @param sa the already-computed suffix array 0..N-1
     * @param LCP the resulting LCP array 0..N-1
     */
    public static void makeLCPArray( int [ ] s, int [ ] sa, int [ ] LCP )
    {
        int N = sa.length;
        int [ ] rank = new int[ N ];

        s[ N ] = -1;
        for( int i = 0; i < N; i++ )
            rank[ sa[ i ] ] = i;

        int h = 0;
        for( int i = 0; i < N; i++ )
            if( rank[ i ] > 0 )
            {
                int j = sa[ rank[ i ] - 1 ];

                while( s[ i + h ] == s[ j + h ] )
                    h++;

                LCP[ rank[ i ] ] = h;
                if( h > 0 )
                    h--;
            }
    }

    /*
     * Fill in the suffix array information for String str
     * @param str the input String
     * @param sa existing array to place the suffix array
     */
    public static void createSuffixArray( String str, int [ ] sa, int [ ] LCP )
    {
        int N = str.length( );

        int [ ] s = new int[ N + 3 ];
        int [ ] SA = new int[ N + 3 ];

        for( int i = 0; i < N; i++ )
            s[ i ] = str.charAt( i );

        makeSuffixArray( s, SA, N, 256 );

        for( int i = 0; i < N; i++ )
            sa[ i ] = SA[ i ];

        makeLCPArray( s, sa, LCP );
    }

    // find the suffix array SA of s[0..n-1] in {1..K}^n
    // require s[n]=s[n+1]=s[n+2]=0, n>=2
    public static void makeSuffixArray( int [ ] s, int [ ] SA, int n, int K )
    {
        int n0 = ( n + 2 ) / 3;
        int n1 = ( n + 1 ) / 3;
        int n2 = n / 3;
        int t = n0 - n1;  // 1 iff n%3 == 1
        int n12 = n1 + n2 + t;

        int [ ] s12  = new int[ n12 + 3 ];
        int [ ] SA12 = new int[ n12 + 3 ];
        int [ ] s0   = new int[ n0 ];
        int [ ] SA0  = new int[ n0 ];

        // generate positions in s for items in s12
        // the "+t" adds a dummy mod 1 suffix if n%3 == 1
        // at that point, the size of s12 is n12
        for( int i = 0, j = 0; i < n + t; i++ )
            if( i % 3 != 0 )
                s12[ j++ ] = i;

        int K12 = assignNames( s, s12, SA12, n0, n12, K );

        computeS12( s12, SA12, n12, K12 );
        computeS0( s, s0, SA0, SA12, n0, n12, K );
        merge( s, s12, SA, SA0, SA12, n, n0, n12, t );
    }

    // Assigns the new supercharacter names.
    // At end of routine, SA will have indices into s, in sorted order
    // and s12 will have new character names
    // Returns the number of names assigned; note that if
    // this value is the same as n12, then SA is a suffix array for s12.
    private static int assignNames( int [ ] s, int [ ] s12, int [ ] SA12,
                                   int n0, int n12, int K )
    {
           // radix sort the new character trios
        radixPass( s12 , SA12, s, 2, n12, K );
        radixPass( SA12, s12 , s, 1, n12, K );
        radixPass( s12 , SA12, s, 0, n12, K );

          // find lexicographic names of triples
        int name = 0;
        int c0 = -1, c1 = -1, c2 = -1;

        for( int i = 0; i < n12; i++ )
        {
            if( s[ SA12[ i ] ] != c0 || s[ SA12[ i ] + 1 ] != c1
                                     || s[ SA12[ i ] + 2 ] != c2 )
            {
                name++;
                c0 = s[ SA12[ i ] ];
                c1 = s[ SA12[ i ] + 1 ];
                c2 = s[ SA12[ i ] + 2 ];
            }

            if( SA12[ i ] % 3 == 1 )
                s12[ SA12[ i ] / 3 ]      = name;
            else
                s12[ SA12[ i ] / 3 + n0 ] = name;
       }

       return name;
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]; sort is relative to offset into s
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int offset,
                                  int n, int K )
    {
        int [ ] count = new int[ K + 2 ];            // counter array

        for( int i = 0; i < n; i++ )
            count[ s[ in[ i ] + offset ] + 1 ]++;    // count occurences

        for( int i = 1; i <= K + 1; i++ )            // compute exclusive sums
            count[ i ] += count[ i - 1 ];

        for( int i = 0; i < n; i++ )
            out[ count[ s[ in[ i ] + offset ] ]++ ] = in[ i ];      // sort
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int n, int K )
    {
        radixPass( in, out, s, 0, n, K );
    }

    // Compute the suffix array for s12, placing result into SA12
    private static void computeS12( int [ ] s12, int [ ] SA12, int n12, int K12 )
    {
        if( K12 == n12 ) // if unique names, don't need recursion
            for( int i = 0; i < n12; i++ )
                SA12[ s12[i] - 1 ] = i;
        else
        {
            makeSuffixArray( s12, SA12, n12, K12 );
            // store unique names in s12 using the suffix array
            for( int i = 0; i < n12; i++ )
                s12[ SA12[ i ] ] = i + 1;
        }
    }

    private static void computeS0( int [ ] s, int [ ] s0, int [ ] SA0, int [ ] SA12,
                               int n0, int n12, int K )
    {
        for( int i = 0, j = 0; i < n12; i++ )
            if( SA12[ i ] < n0 )
                s0[ j++ ] = 3 * SA12[ i ];

        radixPass( s0, SA0, s, n0, K );
    }

    // merge sorted SA0 suffixes and sorted SA12 suffixes
    private static void merge( int [ ] s, int [ ] s12,
                              int [ ] SA, int [ ] SA0, int [ ] SA12,
                              int n, int n0, int n12, int t )
    {
        int p = 0, k = 0;

        while( t != n12 && p != n0 )
        {
            int i = getIndexIntoS( SA12, t, n0 ); // S12
            int j = SA0[ p ];                     // S0

            if( suffix12IsSmaller( s, s12, SA12, n0, i, j, t ) )
            {
                SA[ k++ ] = i;
                t++;
            }
            else
            {
                SA[ k++ ] = j;
                p++;
            }
        }

        while( p < n0 )
            SA[ k++ ] = SA0[ p++ ];
        while( t < n12 )
            SA[ k++ ] = getIndexIntoS( SA12, t++, n0 );
    }

    private static int getIndexIntoS( int [ ] SA12, int t, int n0 )
    {
        if( SA12[ t ] < n0 )
            return SA12[ t ] * 3 + 1;
        else
            return ( SA12[ t ] - n0 ) * 3 + 2;
    }

    private static boolean leq( int a1, int a2, int b1, int b2 )
      { return a1 < b1 || a1 == b1 && a2 <= b2; }

    private static boolean leq( int a1, int a2, int a3, int b1, int b2, int b3 )
      { return a1 < b1 || a1 == b1 && leq( a2, a3,b2, b3 ); }

    private static boolean suffix12IsSmaller( int [ ] s, int [ ] s12, int [ ] SA12,
                                             int n0, int i, int j, int t )
    {
        if( SA12[ t ] < n0 )  // s1 vs s0; can break tie after 1 character
            return leq( s[ i ], s12[ SA12[ t ] + n0 ],
                        s[ j ], s12[ j / 3 ] );
        else                  // s2 vs s0; can break tie after 2 characters
            return leq( s[ i ], s[ i + 1 ], s12[ SA12[ t ] - n0 + 1 ],
                        s[ j ], s[ j + 1 ], s12[ j / 3 + n0 ] );
    }

    public static void printV( int [ ]  a, String comment )
    {
        System.out.print( comment + ":" );
        for( int x : a )
            System.out.print( x + " " );

        System.out.println( );
    }

    public static boolean isPermutation( int [ ] SA, int n )
    {
        boolean [ ] seen = new boolean [ n ];

        for( int i = 0; i < n; i++ )
            seen[ i ] = false;

        for( int i = 0; i < n; i++ )
            seen[ SA[ i ] ] = true;

        for( int i = 0; i < n; i++ )
            if( !seen[ i ] )
                return false;

        return true;
    }

    public static boolean sleq( int  [ ] s1, int start1, int [ ] s2, int start2 )
    {
        for( int i = start1, j = start2; ; i++, j++ )
        {
            if( s1[ i ] < s2[ j ] )
                return true;

            if( s1[ i ] > s2[ j ] )
                return false;
        }
    }

    // Check if SA is a sorted suffix array for s
    public static boolean isSorted( int [ ] SA, int [ ] s, int n )
    {
        for( int i = 0; i < n-1; i++ )
            if( !sleq( s, SA[ i ], s, SA[ i + 1 ] ) )
                return false;

        return true;
    }

    public static void test( String str )
    {
        int [ ] sufarr = new int[ str.length( ) ];
        int [ ] LCP = new int[ str.length( ) ];

        createSuffixArray( str, sufarr, LCP );

        System.out.println( str + ":" );
        for( int i = 0; i < str.length( ); i++ )
            System.out.println( i + " " + sufarr[ i ] + " " + LCP[ i ] );
        System.out.println( );
    }

    public static void main( String [ ] args ) throws IOException
    {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int[]sa = new int[s.length()];
        int[]lcp = new int[s.length()];
        createSuffixArray(s, sa, lcp);
        /*for (int i = 0; i < sa.length; i++)
        {
        	System.out.println(s.substring(sa[i]));

		}*/
        String allowed2 = br.readLine();
        boolean[]allowed = new boolean[26];
        for(int i = 0; i < 26; i++) {
        	if(allowed2.charAt(i)=='1')
        		allowed[i] = true;
        }
        int ans = 0;
        int k = Integer.parseInt(br.readLine());
        int bad = 0;
        //System.out.println();
        for(int j = sa[0]; j < sa.length; j++) {
    		if(!allowed[s.charAt(j)-'a'])
    			bad++;
			if(bad<=k) {
				//System.out.println(s.substring(sa[0], j+1));
				ans++;
			}
    	}
        for (int i = 1; i < sa.length; i++)
        {
        	if(lcp[i] == s.length()-sa[i-1]) {
	        	for(int j = sa[i] + lcp[i]; j < sa.length; j++) {
	        		if(!allowed[s.charAt(j)-'a'])
	        			bad++;
	        		if(bad<=k) {
	        			//System.out.println(s.substring(sa[i], j+1));
	        			ans++;
	        		}
	        	}
        	}
        	else {
        		bad = lcp[i];
        		for(int j = sa[i] + lcp[i]; j < sa.length; j++) {
	        		if(!allowed[s.charAt(j)-'a'])
	        			bad++;
	        		if(bad<=k) {
	        			//System.out.println(s.substring(sa[i], j+1));
	        			ans++;
	        		}
        		}
        	}
        }
        System.out.println(ans);
    }

    /*
     * Returns the LCP for any two strings
     */
    public static int computeLCP( String s1, String s2 )
    {
        int i = 0;

        while( i < s1.length( ) && i < s2.length( ) && s1.charAt( i ) == s2.charAt( i ) )
            i++;

        return i;
    }

    /*
     * Fill in the suffix array and LCP information for String str
     * @param str the input String
     * @param SA existing array to place the suffix array
     * @param LCP existing array to place the LCP information
     * Note: Starting in Java 7, this will use quadratic space.
     */
    public static void createSuffixArraySlow( String str, int [ ] SA, int [ ] LCP )
    {
        if( SA.length != str.length( ) || LCP.length != str.length( ) )
            throw new IllegalArgumentException( );

        int N = str.length( );

        String [ ] suffixes = new String[ N ];
        for( int i = 0; i < N; i++ )
            suffixes[ i ] = str.substring( i );

        Arrays.sort( suffixes );

        for( int i = 0; i < N; i++ )
            SA[ i ] = N - suffixes[ i ].length( );

        LCP[ 0 ] = 0;
        for( int i = 1; i < N; i++ )
            LCP[ i ] = computeLCP( suffixes[ i - 1 ], suffixes[ i ] );
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class GoodSubstring
{

    /*
     * Create the LCP array from the suffix array
     * @param s the input array populated from 0..N-1, with available pos N
     * @param sa the already-computed suffix array 0..N-1
     * @param LCP the resulting LCP array 0..N-1
     */
    public static void makeLCPArray( int [ ] s, int [ ] sa, int [ ] LCP )
    {
        int N = sa.length;
        int [ ] rank = new int[ N ];

        s[ N ] = -1;
        for( int i = 0; i < N; i++ )
            rank[ sa[ i ] ] = i;

        int h = 0;
        for( int i = 0; i < N; i++ )
            if( rank[ i ] > 0 )
            {
                int j = sa[ rank[ i ] - 1 ];

                while( s[ i + h ] == s[ j + h ] )
                    h++;

                LCP[ rank[ i ] ] = h;
                if( h > 0 )
                    h--;
            }
    }

    /*
     * Fill in the suffix array information for String str
     * @param str the input String
     * @param sa existing array to place the suffix array
     */
    public static void createSuffixArray( String str, int [ ] sa, int [ ] LCP )
    {
        int N = str.length( );

        int [ ] s = new int[ N + 3 ];
        int [ ] SA = new int[ N + 3 ];

        for( int i = 0; i < N; i++ )
            s[ i ] = str.charAt( i );

        makeSuffixArray( s, SA, N, 256 );

        for( int i = 0; i < N; i++ )
            sa[ i ] = SA[ i ];

        makeLCPArray( s, sa, LCP );
    }

    // find the suffix array SA of s[0..n-1] in {1..K}^n
    // require s[n]=s[n+1]=s[n+2]=0, n>=2
    public static void makeSuffixArray( int [ ] s, int [ ] SA, int n, int K )
    {
        int n0 = ( n + 2 ) / 3;
        int n1 = ( n + 1 ) / 3;
        int n2 = n / 3;
        int t = n0 - n1;  // 1 iff n%3 == 1
        int n12 = n1 + n2 + t;

        int [ ] s12  = new int[ n12 + 3 ];
        int [ ] SA12 = new int[ n12 + 3 ];
        int [ ] s0   = new int[ n0 ];
        int [ ] SA0  = new int[ n0 ];

        // generate positions in s for items in s12
        // the "+t" adds a dummy mod 1 suffix if n%3 == 1
        // at that point, the size of s12 is n12
        for( int i = 0, j = 0; i < n + t; i++ )
            if( i % 3 != 0 )
                s12[ j++ ] = i;

        int K12 = assignNames( s, s12, SA12, n0, n12, K );

        computeS12( s12, SA12, n12, K12 );
        computeS0( s, s0, SA0, SA12, n0, n12, K );
        merge( s, s12, SA, SA0, SA12, n, n0, n12, t );
    }

    // Assigns the new supercharacter names.
    // At end of routine, SA will have indices into s, in sorted order
    // and s12 will have new character names
    // Returns the number of names assigned; note that if
    // this value is the same as n12, then SA is a suffix array for s12.
    private static int assignNames( int [ ] s, int [ ] s12, int [ ] SA12,
                                   int n0, int n12, int K )
    {
           // radix sort the new character trios
        radixPass( s12 , SA12, s, 2, n12, K );
        radixPass( SA12, s12 , s, 1, n12, K );
        radixPass( s12 , SA12, s, 0, n12, K );

          // find lexicographic names of triples
        int name = 0;
        int c0 = -1, c1 = -1, c2 = -1;

        for( int i = 0; i < n12; i++ )
        {
            if( s[ SA12[ i ] ] != c0 || s[ SA12[ i ] + 1 ] != c1
                                     || s[ SA12[ i ] + 2 ] != c2 )
            {
                name++;
                c0 = s[ SA12[ i ] ];
                c1 = s[ SA12[ i ] + 1 ];
                c2 = s[ SA12[ i ] + 2 ];
            }

            if( SA12[ i ] % 3 == 1 )
                s12[ SA12[ i ] / 3 ]      = name;
            else
                s12[ SA12[ i ] / 3 + n0 ] = name;
       }

       return name;
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]; sort is relative to offset into s
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int offset,
                                  int n, int K )
    {
        int [ ] count = new int[ K + 2 ];            // counter array

        for( int i = 0; i < n; i++ )
            count[ s[ in[ i ] + offset ] + 1 ]++;    // count occurences

        for( int i = 1; i <= K + 1; i++ )            // compute exclusive sums
            count[ i ] += count[ i - 1 ];

        for( int i = 0; i < n; i++ )
            out[ count[ s[ in[ i ] + offset ] ]++ ] = in[ i ];      // sort
    }

    // stably sort in[0..n-1] with indices into s that has keys in 0..K
    // into out[0..n-1]
    // uses counting radix sort
    private static void radixPass( int [ ] in, int [ ] out, int [ ] s, int n, int K )
    {
        radixPass( in, out, s, 0, n, K );
    }

    // Compute the suffix array for s12, placing result into SA12
    private static void computeS12( int [ ] s12, int [ ] SA12, int n12, int K12 )
    {
        if( K12 == n12 ) // if unique names, don't need recursion
            for( int i = 0; i < n12; i++ )
                SA12[ s12[i] - 1 ] = i;
        else
        {
            makeSuffixArray( s12, SA12, n12, K12 );
            // store unique names in s12 using the suffix array
            for( int i = 0; i < n12; i++ )
                s12[ SA12[ i ] ] = i + 1;
        }
    }

    private static void computeS0( int [ ] s, int [ ] s0, int [ ] SA0, int [ ] SA12,
                               int n0, int n12, int K )
    {
        for( int i = 0, j = 0; i < n12; i++ )
            if( SA12[ i ] < n0 )
                s0[ j++ ] = 3 * SA12[ i ];

        radixPass( s0, SA0, s, n0, K );
    }

    // merge sorted SA0 suffixes and sorted SA12 suffixes
    private static void merge( int [ ] s, int [ ] s12,
                              int [ ] SA, int [ ] SA0, int [ ] SA12,
                              int n, int n0, int n12, int t )
    {
        int p = 0, k = 0;

        while( t != n12 && p != n0 )
        {
            int i = getIndexIntoS( SA12, t, n0 ); // S12
            int j = SA0[ p ];                     // S0

            if( suffix12IsSmaller( s, s12, SA12, n0, i, j, t ) )
            {
                SA[ k++ ] = i;
                t++;
            }
            else
            {
                SA[ k++ ] = j;
                p++;
            }
        }

        while( p < n0 )
            SA[ k++ ] = SA0[ p++ ];
        while( t < n12 )
            SA[ k++ ] = getIndexIntoS( SA12, t++, n0 );
    }

    private static int getIndexIntoS( int [ ] SA12, int t, int n0 )
    {
        if( SA12[ t ] < n0 )
            return SA12[ t ] * 3 + 1;
        else
            return ( SA12[ t ] - n0 ) * 3 + 2;
    }

    private static boolean leq( int a1, int a2, int b1, int b2 )
      { return a1 < b1 || a1 == b1 && a2 <= b2; }

    private static boolean leq( int a1, int a2, int a3, int b1, int b2, int b3 )
      { return a1 < b1 || a1 == b1 && leq( a2, a3,b2, b3 ); }

    private static boolean suffix12IsSmaller( int [ ] s, int [ ] s12, int [ ] SA12,
                                             int n0, int i, int j, int t )
    {
        if( SA12[ t ] < n0 )  // s1 vs s0; can break tie after 1 character
            return leq( s[ i ], s12[ SA12[ t ] + n0 ],
                        s[ j ], s12[ j / 3 ] );
        else                  // s2 vs s0; can break tie after 2 characters
            return leq( s[ i ], s[ i + 1 ], s12[ SA12[ t ] - n0 + 1 ],
                        s[ j ], s[ j + 1 ], s12[ j / 3 + n0 ] );
    }

    public static void printV( int [ ]  a, String comment )
    {
        System.out.print( comment + ":" );
        for( int x : a )
            System.out.print( x + " " );

        System.out.println( );
    }

    public static boolean isPermutation( int [ ] SA, int n )
    {
        boolean [ ] seen = new boolean [ n ];

        for( int i = 0; i < n; i++ )
            seen[ i ] = false;

        for( int i = 0; i < n; i++ )
            seen[ SA[ i ] ] = true;

        for( int i = 0; i < n; i++ )
            if( !seen[ i ] )
                return false;

        return true;
    }

    public static boolean sleq( int  [ ] s1, int start1, int [ ] s2, int start2 )
    {
        for( int i = start1, j = start2; ; i++, j++ )
        {
            if( s1[ i ] < s2[ j ] )
                return true;

            if( s1[ i ] > s2[ j ] )
                return false;
        }
    }

    // Check if SA is a sorted suffix array for s
    public static boolean isSorted( int [ ] SA, int [ ] s, int n )
    {
        for( int i = 0; i < n-1; i++ )
            if( !sleq( s, SA[ i ], s, SA[ i + 1 ] ) )
                return false;

        return true;
    }

    public static void test( String str )
    {
        int [ ] sufarr = new int[ str.length( ) ];
        int [ ] LCP = new int[ str.length( ) ];

        createSuffixArray( str, sufarr, LCP );

        System.out.println( str + ":" );
        for( int i = 0; i < str.length( ); i++ )
            System.out.println( i + " " + sufarr[ i ] + " " + LCP[ i ] );
        System.out.println( );
    }

    public static void main( String [ ] args ) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int[]sa = new int[s.length()];
        int[]lcp = new int[s.length()];
        createSuffixArray(s, sa, lcp);
        String allowed2 = br.readLine();
        boolean[]allowed = new boolean[26];
        for(int i = 0; i < 26; i++) {
            if(allowed2.charAt(i)=='1')
                allowed[i] = true;
        }
        int ans = 0;
        int k = Integer.parseInt(br.readLine());
        int bad = 0;
        int[] badCharactersUpTo = new int[sa.length+1];
        for(int j = sa[0]; j < sa.length; j++) {
            if(!allowed[s.charAt(j)-'a']) {
                bad++;
            }
            badCharactersUpTo[j-sa[0]+1] = bad;
            if(bad<=k)
            ans++;
        }
        for (int i = 1; i < sa.length; i++)
        {
            bad = badCharactersUpTo[lcp[i]];
            for(int j = sa[i] + lcp[i]; j < sa.length; j++) {
                if(!allowed[s.charAt(j)-'a']) {
                    bad++;
                }
                badCharactersUpTo[j-sa[i]+1] = bad;
                if(bad<=k)
                ans++;
            }
        }
        System.out.println(ans);
    }

    /*
     * Returns the LCP for any two strings
     */
    public static int computeLCP( String s1, String s2 )
    {
        int i = 0;

        while( i < s1.length( ) && i < s2.length( ) && s1.charAt( i ) == s2.charAt( i ) )
            i++;

        return i;
    }

    /*
     * Fill in the suffix array and LCP information for String str
     * @param str the input String
     * @param SA existing array to place the suffix array
     * @param LCP existing array to place the LCP information
     * Note: Starting in Java 7, this will use quadratic space.
     */
    public static void createSuffixArraySlow( String str, int [ ] SA, int [ ] LCP )
    {
        if( SA.length != str.length( ) || LCP.length != str.length( ) )
            throw new IllegalArgumentException( );

        int N = str.length( );

        String [ ] suffixes = new String[ N ];
        for( int i = 0; i < N; i++ )
            suffixes[ i ] = str.substring( i );

        Arrays.sort( suffixes );

        for( int i = 0; i < N; i++ )
            SA[ i ] = N - suffixes[ i ].length( );

        LCP[ 0 ] = 0;
        for( int i = 1; i < N; i++ )
            LCP[ i ] = computeLCP( suffixes[ i - 1 ], suffixes[ i ] );
    }
}


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

public class Ideone {
	public static void main (String[] args) throws java.lang.Exception {
		in.init(System.in);
		String s=in.next(),p=in.next();
		int k=in.nextInt(),i,j,kq=0;
		TreeSet<Character> badChar=new TreeSet<>();
		for (i=0;i<p.length();++i) if (p.charAt(i)=='0') badChar.add((char)((int)'a'+i));
		TreeSet<String> had=new TreeSet<>();
		for (i=0;i<s.length();++i) {
			int numb=0;
			StringBuilder sb=new StringBuilder();j=i;
			while (j<s.length()) {
				sb.append(s.charAt(j));
				if (badChar.contains(s.charAt(j))) ++numb;
				if (numb>k) break;else {
					if (!had.contains(sb.toString())) {++kq;had.add(sb.toString());}
				}
				++j;
			}
		}
		System.out.println(kq);
	}
}

class in {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {

            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


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

public class Ideone {
	static boolean good[];
	static int dem=-1;
	static void trivia(trieNode curNode,int num,int max) {
	    int i;if (num>max) return ;else ++dem;
	    for (i=0;i<26;++i) if (curNode.link[i]!=null) {
		int k=(good[i])?0:1;
		trivia(curNode.link[i],num+k,max);
	    }
	}
	public static void main (String[] args) throws java.lang.Exception {
		in.init(System.in);
		trieNode Tree=trie.createTree();
		String word=in.next(),l=in.next();int k=in.nextInt(),i;
		good=new boolean[26];
		for (i=0;i<l.length();++i) good[i]=(l.charAt(i)=='1');
		char letters[]=word.toCharArray();
		int n=word.length(),offset=97;
		for (i=0;i<n;++i) {
			trieNode curNode=Tree;
			for (int j=i;j<n;++j) {
				if (curNode.link[letters[j]-offset]==null) curNode.link[letters[j]-offset]=new trieNode(letters[i],true);
				curNode=curNode.link[letters[j]-offset];
			}
			curNode.fullWord=true;
		}
		trivia(Tree,0,k);
		System.out.println(dem);
	}
}

class trie {
    static trieNode createTree() {
	return new trieNode('\0',false);
    }
}

class trieNode {
    char letter;
    trieNode[] link;
    boolean fullWord;
    trieNode(char letter,boolean fullWord) {
	this.letter=letter;
	this.fullWord=fullWord;
	link=new trieNode[26];
    }
}

class in {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {

            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


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

public class Main implements Runnable {

        public static final long MOD = 100000000007l;
        public static final int P = 31;

	public void solve() throws IOException {
		char[] c = nextToken().toCharArray();
                int N = c.length;
                char[] isok = nextToken().toCharArray();
                int MAX = nextInt();

                TreeSet<Long> hs = new TreeSet<Long>();

                for(int i = 0; i < N; i++){
                        long hash = 0;
                        int illegal = 0;
                        for(int j = i; j < N; j++){
                                hash = (hash * P + (s.charAt(j) - 'a' + 1)) % MOD;
                                if(isok[s.charAt(j) - 'a'] == '0')illegal++;
                                if(illegal > MAX) break;
                                hs.add(hash);
                        }
                }

                System.out.println(hs.size());
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


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

public class Main implements Runnable {

        public static final long MOD = 100000000007l;
        public static final int P = 31;

	public void solve() throws IOException {
		String s = nextToken();
                int N = s.length();
                char[] isok = nextToken().toCharArray();
                int MAX = nextInt();

                TreeSet<Long> hs = new TreeSet<Long>();

                for(int i = 0; i < N; i++){
                        long hash = 0;
                        int illegal = 0;
                        for(int j = i; j < N; j++){
                                hash = (hash * P + (s.charAt(j) - 'a' + 1)) % MOD;
                                if(isok[s.charAt(j) - 'a'] == '0')illegal++;
                                if(illegal > MAX) break;
                                hs.add(hash);
                        }
                }

                System.out.println(hs.size());
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


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

public class Main implements Runnable {

        public static final long MOD = 100000000007l;

	public void solve() throws IOException {
		char[] c = nextToken().toCharArray();
                int N = c.length;
                char[] isok = nextToken().toCharArray();
                boolean[] isbad = new boolean[26];

                for(int i = 0; i  < 26; i++) isbad[i] = isok[i] == '0';
                int MAX = nextInt();

                TreeSet<Long> hs = new TreeSet<Long>();

                for(int i = 0; i < N; i++){
                        long hash = 0;
                        int illegal = 0;
                        for(int j = i; j < N; j++){
                                hash = (hash * 31 + (c[j] - 'a' + 1)) % MOD;
                                if(isbad[c[j] - 'a'])illegal++;
                                if(illegal > MAX) break;
                                hs.add(hash);
                        }
                }

                System.out.println(hs.size());
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


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

public class Main implements Runnable {

        public static final long MOD = 100000000007l;
        public static final int P = 31;

	public void solve() throws IOException {
		char[] c = nextToken().toCharArray();
                int N = c.length;
                char[] isok = nextToken().toCharArray();
                int MAX = nextInt();

                TreeSet<Long> hs = new TreeSet<Long>();

                for(int i = 0; i < N; i++){
                        long hash = 0;
                        int illegal = 0;
                        for(int j = i; j < N; j++){
                                hash = (hash * P + (c[j] - 'a' + 1)) % MOD;
                                if(isok[c[j] - 'a'] == '0')illegal++;
                                if(illegal > MAX) break;
                                hs.add(hash);
                        }
                }

                System.out.println(hs.size());
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


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

public class Main implements Runnable {

        public static final long MOD = 100000000007l;

	public void solve() throws IOException {
		char[] c = nextToken().toCharArray();
                int N = c.length;
                char[] isok = nextToken().toCharArray();
                boolean[] isbad = new boolean[26];

                for(int i = 0; i  < 26; i++) isbad[i] = isok[i] == '0';
                int MAX = nextInt();

                Set<Long> hs = new HashSet<Long>();

                for(int i = 0; i < N; i++){
                        long hash = 0;
                        int illegal = 0;
                        for(int j = i; j < N; j++){
                                hash = (hash * 31 + (c[j] - 'a' + 1)) % MOD;
                                if(isbad[c[j] - 'a'])illegal++;
                                if(illegal > MAX) break;
                                hs.add(hash);
                        }
                }

                System.out.println(hs.size());
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Main implements Runnable {

	public void solve() throws IOException {
		char[] c = nextToken().toCharArray();
                int N = c.length;
                char[] isok = nextToken().toCharArray();
                boolean[] isbad = new boolean[26];

                for(int i = 0; i  < 26; i++) isbad[i] = isok[i] == '0';
                int MAX = nextInt();

                long MOD = BigInteger.probablePrime(56, new Random()).longValue();
                Set<Long> hs = new TreeSet<Long>();

                for(int i = 0; i < N; i++){
                        long hash = 0;
                        int illegal = 0;
                        for(int j = i; j < N; j++){
                                hash = (hash * 31 + (c[j] - 'a' + 1)) % MOD;
                                if(isbad[c[j] - 'a'])illegal++;
                                if(illegal > MAX) break;
                                hs.add(hash);
                        }
                }

                System.out.println(hs.size());
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}


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

public class Main implements Runnable {

	public void solve() throws IOException {
		String s = nextToken();
                int N = s.length();
                char[] isok = nextToken().toCharArray();
                int MAX = nextInt();

                int[] d = new int[N];
                for(int i = 0; i < N ; i++){
                        char c = s.charAt(i);
                        if(isok[c - 'a'] == '0') d[i] = 1;
                }

                for(int i = 1; i < N; i++) d[i] += d[i - 1];

                HashSet<String> hs = new HashSet<String>();

                int cnt = 0;
                for(int i = 0; i < N; i++){
                        for(int j = i; j < N; j++){
                                String now = s.substring(i, j + 1);
                                int r = d[j];
                                int l = (i == 0) ? 0 : d[i - 1];

                                //System.out.println(now + " " + (r - l));
                                if(r - l <= MAX){
                                        if(! hs.contains(now)){
                                                cnt++;
                                                hs.add(now);
                                        }
                                }
                        }
                }

                System.out.println(cnt);
	}

	//-----------------------------------------------------------
	public static void main(String[] args) {
		new Main().run();
	}

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }

        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			tok = null;
			solve();
			in.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	BufferedReader in;
	StringTokenizer tok;
}