#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
const long long OO=1e15;
long long a[26][111111];
string s,ts;
int n,k;
int lens,lent;
int cnt;
long long ans;
long long mct;
int main()
{
memset(a,0,sizeof(a));
cin>>n>>k;
cin>>s;
lens=s.length();
for (int i=0;i<lens;i++)
{
int c=s[i]-'a';
a[c][0]++;
long long p=a[c][0];
a[c][p]=i;
}
while (n--)
{
ans=0;
cnt=0;
cin>>ts;
lent=ts.length();
for (int i=0;i<lent;i++)
{
int c=ts[i]-'a';
long long p=a[c][0];
if (p==0)
{
ans+=lent;
}
else
{
mct=OO;
for (int j=1;j<=p;j++)
{
if (abs(i-a[c][j])<mct)
{
mct=abs(i-a[c][j]);
}
}
ans+=mct;
}
}
cout<<ans<<endl;
}
return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>

using namespace std;

const long long OO=1e15;

long long a[26][111111];

string palabra,palabra2;

int casos,largo;
int largo_1,largo_2;
long long contador;
long long mct;

int main(){

memset(a,0,sizeof(a));

cin >> casos >> largo;

cin >> palabra;

largo_1 = palabra.size();

for(int i = 0; i < largo_1; i++){
    int c = palabra[i]-'a';
    a[c][0]++;
    long long p = a[c][0];
    a[c][p] = i;
}
while (casos--){

    contador=0;

    cin >> palabra2;
    largo_2 = palabra2.size();
    for(int i = 0; i < largo_2; i++){

        int c = palabra2[i]-'a';
        long long p = a[c][0];

        if (p==0){
            contador+=largo_2;
        }
        else{
            mct=OO;
            for (int j = 1 ; j <= p;j++){
                if (abs(i-a[c][j])<mct)
            {
                mct=abs(i-a[c][j]);
            }
            }
            contador += mct;
        }
    }
    cout << contador << endl;
}
return 0;
}


    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int mm=2e6+9;
    class node
    {
      public:char s;int id;
    }f[mm];
    bool vis[777];
    long long sum;
    int n,m;
    char s[mm],ss[mm];
    bool cmp(node a,node b)
    {
      if(a.s!=b.s)
        return a.s<b.s;
      else return a.id<b.id;
    }
    inline int aabs(int x)
    {
      return x<0?-x:x;
    }
    int blook(char z,int num)
    {
      int l=0,r=m-1,mid;
      int ret=999999;
      while(l<=r)
      {
        mid=(l+r)/2;
        if(f[mid].s>z)r=mid-1;
        else if(f[mid].s<z)l=mid+1;
        else
        { int zz=aabs(num-f[mid].id);
          if(ret>zz)ret=zz;
          if(f[mid].id>num)r=mid-1;
          else l=mid+1;
        }
      }
      return ret;
    }
    int main()
    {
      while(scanf("%d%d",&n,&m)!=EOF)
      { memset(vis,0,sizeof(vis));
        scanf("%s",s);
        for(int i=0;s[i];++i)
         {
           vis[s[i]]=1;
           f[i].s=s[i];f[i].id=i;
         }
         sort(f,f+m,cmp);
        for(int i=0;i<n;++i)
        { sum=0;
          scanf("%s",ss);
          long long len=strlen(ss);
          for(int j=0;ss[j];++j)
          if(vis[ss[j]])
            sum+=blook(ss[j],j);
          else sum+=len;
          cout<<sum<<"\n";
        }
      }
    }


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, len;

string s;

string cs[100005];

vector<pair<char, int>> ss;

int f(char c, int is) {
	auto si = lower_bound(ss.begin(), ss.end(), make_pair(c, 0));
	int minx = 999999;

	for(; si->first == c; ++si) {
		minx = min(abs(si->second - is), minx);
	}
	//cout << minx;
	return minx;
}

int main() {
	cin >> n >> len;

	cin >> s;

	ss.resize(len);

	for(int i = 0; i < len; ++i) {
		ss[i] = {s[i], i};
	}

	sort(ss.begin(), ss.end());

	int F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		//cs[i].reserve(e);
		//scanf("%s\n", cs + i);
		cin >> cs[i];
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			/*
			int right = s.find(cs[i][j], j);
			int left = s.rfind(cs[i][j], j);

			if(left == string::npos && right == string::npos) {
				F[i] += cs[i].length();
			}
			else if(right == string::npos)
			{
				F[i] += abs(left - j);
			}
			else if(left == string::npos) {
				F[i] += abs(right - j);
			}
			else {
				F[i] += min(abs(left - j), abs(right - j));
			}
			//*/

			auto minx = f(cs[i][j], j);

			if(minx != 999999)
				F[i] += minx;
			else
				F[i] += cs[i].length();

			//cout << F[i] << ", ";
		}
	}

	for(int i = 0; i < n; ++i) {
		printf("%d\n", F[i]);
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, e;

int main() {
	cin >> n >> e;

	string s;

	cin >> s;

//	s += string(100001 - e, '=');

	string cs[100005];

	int F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		cin >> cs[i];
		//cout << cs[i] << "\n";
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			int right = s.find(cs[i][j], j);
			int left = s.rfind(cs[i][j], j);

			long long diff = 0;

			if(left == string::npos && right == string::npos) {
				F[i] += cs[i].length();
			}
			else if(right == string::npos)
			{
				diff = abs(left - j);
				F[i] += diff;
			}
			else if(left == string::npos) {
				diff = abs(right - j);
				F[i] += diff;
			}
			else {
				diff = min(abs(left - j), abs(right - j));
				F[i] += diff;
			}
			//cout << F[i] << ", ";
		}
	}

	for(int i = 0; i < n; ++i) {
		cout << F[i] << "\n";
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, len;

string s;

string cs[100005];

vector<pair<char, int>> ss;

i64 f(char c, i64 is) {
	auto si = lower_bound(ss.begin(), ss.end(), make_pair(c, 0));
	i64 minx = numeric_limits<i64>::max();

	for(; si->first == c; ++si) {
		minx = min(abs(si->second - is), minx);
	}
	//cout << minx;
	return minx;
}

int main() {
	cin >> n >> len;

	cin >> s;

	ss.resize(len);

	for(int i = 0; i < len; ++i) {
		ss[i] = {s[i], i};
	}

	sort(ss.begin(), ss.end());

	i64 F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		cin >> cs[i];
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			i64 minx = f(cs[i][j], j);

			if(minx != numeric_limits<i64>::max())
				F[i] += minx;
			else
				F[i] += cs[i].length();
			}
	}

	for(int i = 0; i < n; ++i) {
		printf("%I64d\n", F[i]);
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, len;

string s;

string cs[100005];

int f(char c, int is) {
	int minx = numeric_limits<int>::max();

	for(int i = min(is, len); i < len; ++i)
	{
		if(s[i] == c) minx = min(abs(i - is), minx);
	}

	for(int i = min(is, len); i >= 0; --i) {
		if(s[i] == c) minx = min(abs(i-is), minx);
	}

	return minx;
}

int main() {
	cin >> n >> len;

	cin >> s;

	i64 F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		//cs[i].reserve(e);
		//scanf("%s\n", cs + i);
		cin >> cs[i];
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			i64 minx = f(cs[i][j], j);

			if(minx != numeric_limits<int>::max())
				F[i] += minx;
			else
				F[i] += cs[i].length();

			//cout << F[i] << ", ";
		}
	}

	for(int i = 0; i < n; ++i) {
		printf("%d\n", F[i]);
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, e;

int main() {
	cin >> n >> e;

	string s;

	cin >> s;

	s += string(100001 - e, '=');

	string cs[100005];

	int F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		cin >> cs[i];
		//cout << cs[i] << "\n";
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			int right = s.find(cs[i][j], j);
			int left = s.rfind(cs[i][j], j);

			long long diff = 0;

			if(left == string::npos && right == string::npos) {
				F[i] += cs[i].length();
			}
			else if(right == string::npos)
			{
				diff = abs(left - j);
				F[i] += diff;
			}
			else if(left == string::npos) {
				diff = abs(right - j);
				F[i] += diff;
			}
			else {
				diff = min(abs(left - j), abs(right - j));
				F[i] += diff;
			}
			//cout << F[i] << ", ";
		}
	}

	for(int i = 0; i < n; ++i) {
		cout << F[i] << "\n";
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, len;

string s;

string cs[100005];

int f(char c, int is) {
	int minx = 999999;

	for(int i = 0; i < len; ++i) {
		if(s[i] == c && abs(i - is) < minx)
		minx = abs(i - is);
	}

	return minx;
}

int main() {
	cin >> n >> len;

	cin >> s;

	int F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		//cs[i].reserve(e);
		//scanf("%s\n", cs + i);
		cin >> cs[i];
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			/*
			int right = s.find(cs[i][j], j);
			int left = s.rfind(cs[i][j], j);

			if(left == string::npos && right == string::npos) {
				F[i] += cs[i].length();
			}
			else if(right == string::npos)
			{
				F[i] += abs(left - j);
			}
			else if(left == string::npos) {
				F[i] += abs(right - j);
			}
			else {
				F[i] += min(abs(left - j), abs(right - j));
			}
			//*/

			auto minx = f(cs[i][j], j);

			if(minx != 999999)
				F[i] += minx;
			else
				F[i] += cs[i].length();

			//cout << F[i] << ", ";
		}
	}

	for(int i = 0; i < n; ++i) {
		printf("%d\n", F[i]);
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, len;

string s;

string cs[100005];

vector<pair<char, int>> ss;

i64 f(char c, i64 is) {
	auto si = lower_bound(ss.begin(), ss.end(), make_pair(c, 0));
	i64 minx = numeric_limits<i64>::max();

	for(; si->first == c; ++si) {
		minx = min(abs(si->second - is), minx);
	}
	//cout << minx;
	return minx;
}

int main() {
	cin >> n >> len;

	cin >> s;

	ss.resize(len);

	for(int i = 0; i < len; ++i) {
		ss[i] = {s[i], i};
	}

	sort(ss.begin(), ss.end());

	int F[100005] = {0};

	for(int i = 0; i < n; ++i) {
		cin >> cs[i];
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			i64 minx = f(cs[i][j], j);

			if(minx != numeric_limits<i64>::max())
				F[i] += minx;
			else
				F[i] += cs[i].length();
			}
	}

	for(int i = 0; i < n; ++i) {
		printf("%d\n", F[i]);
	}

	return 0;
}


/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/149/C
 */
/*!
 * Noein
 * Problem:
 * http://codeforces.com/problemset/problem/62/B
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <limits>
#include <numeric>

using namespace std;

template<class... T> using uomap = unordered_map<T...>;
template<class... T> using uoset = unordered_set<T...>;
using u32 = unsigned
int;
using i32 = int;
using u64 = unsigned long long;
using i64 = long long;
using real = long double;

const int debug_d_ = 0;

#define DEB if(debug_d_)

int n, len;

string cs[100005];

vector<pair<char, int>> ss;

i64 find2(char c, i64 is) {
	auto si = lower_bound(ss.begin(), ss.end(), make_pair(c, 0));
	auto const se = upper_bound(ss.begin(), ss.end(), make_pair(c, len));

	i64 minx = numeric_limits<i64>::max();

	for(; si != se; ++si) {
		minx = min(abs(si->second - is), minx);
	}

	return minx;
}

int main() {
	cin >> n >> len;

	ss.resize(len);

	char c;
	(void)cin.get();

	for(int i = 0; i < len; ++i) {
	    c = cin.get();
		ss[i] = {c, i};
	}

	sort(ss.begin(), ss.end());

	i64 F[100005] {};

	for(int i = 0; i < n; ++i) {
		cin >> cs[i];
	}

	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < cs[i].length(); ++j) {
			auto minx = find2(cs[i][j], j);

			//*
			if(minx != numeric_limits<i64>::max())
				F[i] += minx;
			else
				F[i] += cs[i].length();
			//*/
			//F[i] += min(minx, (i64)cs[i].length());
		}
	}

	for(int i = 0; i < n; ++i) {
		printf("%I64d\n", F[i]);
		//cout << F[i] << '\n';
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

    vector<pair<string,int> >st;
    int csum[n];

    for(int i=0; i<n; i++){
        csum[i] = (good[(s[i]-'a')] == '0');
    }

    for(int i=1; i<n ; i++) csum[i] += csum[i-1];
    int ans=0;

    for(int i=0; i<n; i++){

        int sub = (i==0? 0 : csum[i-1]);
        string temp;

        for(int j=i; j<n; j++){

            temp += s[j];
            int ok = ((csum[j]-sub)<=k);
            if(ok)st.pb(mp(temp,ok));

        }
    }

    sort(atoz(st));
    string prev = "$";

    for(auto x : st){

        if(prev != x.fi)ans += x.se;
        prev = x.fi;
    }

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}

int dist[27];

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

    int csum[n] , ans=0 , cnt=0;
    queue<string>V;

    for(int i=0; i<n; i++){
        if(good[(s[i]-'a')] == '0'){
            csum[i] = 1;
            if(dist[s[i]-'a']==0)cnt++;

        }
        else if(dist[s[i]-'a']==0)ans++;
        dist[s[i]-'a']=1;
        string c;
        c += s[i];
        V.push(c);

    }

    for(int i=1; i<n ; i++) csum[i] += csum[i-1];

    for(int l=2; l<=n; l++){

        map<string,bool>taken;
       string t;

        for(int i=0 ; (i+l-1) < n ; i++){

            t = V.front();
            t += s[i+l-1];
            //cout<<t<<endl;

            if(taken[t] == false){

                taken[t] = true;
                int sub = (i==0? 0 : csum[i-1]);

                if((csum[i+l-1]-sub)<=k) ans++;
            }

            V.pop();
            V.push(t);
        }
        V.pop();
    }

    ans += cnt*(k>0);

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}

int dist[27];

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

    int csum[n] , ans=0 , cnt=0;
    queue<string>V;

    for(int i=0; i<n; i++){
        if(good[(s[i]-'a')] == '0'){
            csum[i] = 1;
            if(dist[s[i]-'a']==0)cnt++;

        }
        else if(dist[s[i]-'a']==0)ans++;
        dist[s[i]-'a']=1;
        string c;
        c += s[i];
        V.push(c);

    }

    //cout<<ans<<endl;
    for(int i=1; i<n ; i++) csum[i] += csum[i-1];

    for(int l=2; l<=n; l++){

        map<string,bool>taken;
       string t;

        for(int i=0 ; (i+l-1) < n ; i++){

            t = V.front();
            t += s[i+l-1];
            //cout<<t<<endl;

            if(taken[t] == false){

                taken[t] = true;
                int sub = (i==0? 0 : csum[i-1]);

                if((csum[i+l-1]-sub)<=k) ans++;
            }

            V.pop();
            V.push(t);
        }
        if(V.size())V.pop();
    }

    if(k)ans += cnt;

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int ans[1507],len=0;

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

   for(int i=0; i<n; i++){

       for(lli tk=k , j=i , hsh=0 ; j<n; j++){

           if(good[s[j]-'a'] == '0' && !tk--) break;

           else ans[len++] = hsh = (hsh<<7)^s[j];
       }
   }
    sort(ans,ans+len);
    cout<<(unique(ans,ans+len)-ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

    map<string,bool>taken;
    int csum[n];

    for(int i=0; i<n; i++){
        csum[i] = (good[(s[i]-'a')] == '0');
    }

    for(int i=1; i<n ; i++) csum[i] += csum[i-1];
    int ans=0;

    for(int i=0; i<n; i++){

        int sub = (i==0? 0 : csum[i-1]);
        string temp;

        for(int j=i; j<n; j++){

            temp += s[j];

            if(taken[temp] == false){
                taken[temp]  = true;
                if((csum[j]-sub)<=k)ans++;

            }
        }
    }
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int Set(int N,int pos){return N=N | (1<<pos);}
int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

    vector<pair<string,int> >st;
    int csum[n];

    for(int i=0; i<n; i++){
        csum[i] = (good[(s[i]-'a')] == '0');
    }

    for(int i=1; i<n ; i++) csum[i] += csum[i-1];
    int ans=0;

    for(int i=0; i<n; i++){

        int sub = (i==0? 0 : csum[i-1]);
        string temp;

        for(int j=i; j<n; j++){

            temp += s[j];
            int ok = ((csum[j]-sub)<=k);
            st.pb(mp(temp,ok));

        }
    }

    sort(atoz(st));
    string prev = "$";

    for(auto x : st){

        if(prev != x.fi)ans += x.se;
        prev = x.fi;
    }

    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int ans[1507],len=0;

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

   for(int i=0; i<n; i++){

       for(lli tk=k , j=i , hsh=0 ; tk && j<n; j++){

           if(good[s[j]-'a'] == '0' && !tk--) break;

           else ans[len++] = hsh = (hsh<<7)^s[j];
       }
   }
    sort(ans,ans+len);
    cout<<(unique(ans,ans+len)-ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

int ans[4000000],len=0;

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

   for(int i=0; i<n; i++){

       for(lli tk=k , j=i , hsh=0 ; j<n; j++){

           if(good[s[j]-'a'] == '0' && !tk--) break;

           else ans[len++] = hsh = (hsh*131)^s[j];
       }
   }
    sort(ans,ans+len);
    cout<<(unique(ans,ans+len)-ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

lli ans[4000000],len=0;

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

   for(int i=0; i<n; i++){

       for(lli tk=k , j=i , hsh=0 ; j<n; j++){

           if(good[s[j]-'a'] == '0' && !tk--) break;

           else ans[len++] = hsh = (hsh*131)^s[j];
       }
   }
    sort(ans,ans+len);
    cout<<(unique(ans,ans+len)-ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long int lli;
typedef pair<int,int> pii;
typedef vector<int> vec;

#define pb push_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n

lli ans[4000000],len=0;

int main()
{
    ///freopen("debug.txt","w",stdout);
    /*
     ios_base::sync_with_stdio(false);
    cin.tie(0);
    */
    string s;
    cin>>s;

    string good;
    cin>>good;

    int k , n = sz(s);
    scn(k);

   for(int i=0; i<n; i++){

       for(lli tk=k , j=i , hsh=0 ; j<n; j++){

           if(good[s[j]-'a'] == '0' && !tk--) break;

           else ans[len++] = hsh = (hsh<<7)^s[j];
       }
   }
    sort(ans,ans+len);
    cout<<(unique(ans,ans+len)-ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e9+9,sm=101;
ll k,fhash[mx],Exp[mx];
// Map[10000000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	set <ll> S;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		pro=Pow(Exp[i],mod-2);
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			// if(ap[hash]==0)
			// 	ans++;
			// Map[hash]++;
			S.insert(hash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
const ll mod=1e9+7;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	ll n,k,hash[1505],count,curhash;
	string s,good;
	cin>>s>>good>>k;
	hash[0]=1;
	rep(i,1,1505)
		hash[i]=(29*hash[i-1])%mod;
	n=sz(s);
	set<ll>S;
	rep(i,0,n){
		count=curhash=0;
		rep(j,i,n){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			curhash+=(s[j]-'a'+1)*hash[j-i+1];
			S.insert(curhash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=9999991,sm=101;
ll k,fhash[mx],Exp[mx],Map[10000000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			pro=Pow(Exp[i],mod-2);
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			if(Map[hash]==0)
				ans++;
			Map[hash]++;
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e9+7,sm=101;
ll k,fhash[mx],Exp[mx];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[0]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	map<ll,ll>map;
	ll count,pro,hash;
	rep(i,1,sz(s)){
		count=0;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			pro=Pow(Exp[i],mod-2);
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			map[hash]++;
		}
	}
	cout<<sz(map)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e9+7,sm=101;
ll k,fhash[mx],Exp[mx];
// Map[10000000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(29*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	set <ll> S;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		pro=Pow(Exp[i],mod-2);
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			// if(ap[hash]==0)
			// 	ans++;
			// Map[hash]++;
			S.insert(hash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e9+7,sm=101;
ll k,fhash[mx],Exp[mx];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[0]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	unordered_map<ll,ll>map;
	ll count,pro,hash;
	rep(i,1,sz(s)){
		count=0;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			pro=Pow(Exp[i],mod-2);
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			map[hash]++;
		}
	}
	cout<<sz(map)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=2499997,sm=101;
ll k,fhash[mx],Exp[mx],Map[2500000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			pro=Pow(Exp[i],mod-2);
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			if(Map[hash]==0)
				ans++;
			Map[hash]++;
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
const ll mod=1e9+7;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	ll n,k,hash[1505],count,curhash;
	string s,good;
	cin>>s>>good>>k;
	hash[0]=1;
	rep(i,1,1505)
		hash[i]=(31*hash[i-1])%mod;
	n=sz(s);
	set<ll>S;
	rep(i,0,n){
		count=curhash=0;
		rep(j,i,n){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			curhash+=(s[j]-'a'+1)*hash[j-i+1];
			S.insert(curhash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e9+7,sm=101;
ll k,fhash[mx],Exp[mx];
// Map[10000000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	set <ll> S;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		pro=Pow(Exp[i],mod-2);
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			// if(ap[hash]==0)
			// 	ans++;
			// Map[hash]++;
			S.insert(hash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
const ll m=1e18+7;
ll n,k,arr[2000];
string s,good;
// ll Hash(string S){
// 	ll temp=0;
// 	rep(i,0,sz(S))
// 		temp=(temp*257+(ll)S[i])%m;
// 	return temp;
// }
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	// Hash();
	ll count,ans=0;
	map <ll,ll> map;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			t+=s[j];
			hash<string> Hash;
			n=Hash(t);
			map[n]++;
		}
	}
	cout<<sz(map)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
const ll m=1e18+7;
ll n,k,arr[2000];
string s,good;
// ll Hash(string S){
// 	ll temp=0;
// 	rep(i,0,sz(S))
// 		temp=(temp*257+(ll)S[i])%m;
// 	return temp;
// }
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	// Hash();
	ll count,ans=0;
	unordered_map <ll,ll> map;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			t+=s[j];
			hash<string> Hash;
			n=Hash(t);
			map[n]++;
		}
	}
	cout<<sz(map)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
const ll m=1e18+7;
ll n,k,arr[2000];
string s,good;
ll Hash(string S){
	ll temp=0;
	rep(i,0,sz(S))
		temp=(temp*257+(ll)S[i])%m;
	return temp;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	// Hash();
	ll count,ans=0;
	unordered_map <ll,ll> map;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			t+=s[j];
			// hash<string> Hash;
			n=Hash(t);
			map[n]++;
		}
	}
	cout<<sz(map)<<nl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
const ll m=1e18+7;
int n,k,arr[2000];
string s,good;
gp_hash_table<string, ll, hash<string> > table;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	int count,ans=0;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			t+=s[j];
			table[t]=1;
		}
	}
	cout<<sz(table)<<nl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
ll n,k,arr[2000];
const ll m=1e18+7;
ll Hash(string S){
	ll temp=0;
	rep(i,0,sz(S))
		temp=(temp*29+(ll)S[i])%m;
	return temp;
}
string s,good;
// const ll RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
//     ll operator()(ll x) { return hash<ll>{}(x ^ RANDOM); }
// };
gp_hash_table<ll, ll > table;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	// hash<string> Hash;
	ll count,ans=0;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			// cout<<i<<" "<<j<<nl;
			t+=s[j];
			ll n=Hash(t);
			table[n]=1;
		}
	}
	cout<<sz(table)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e9+7,sm=101;
ll k,fhash[mx],Exp[mx];
// Map[10000000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	set <ll> S;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			pro=Pow(Exp[i],mod-2);
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			// if(ap[hash]==0)
			// 	ans++;
			// Map[hash]++;
			S.insert(hash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
const ll m=1e18+7;
ll n,k,arr[2000];
string s,good;
// const ll RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
//     ll operator()(ll x) { return hash<ll>{}(x ^ RANDOM); }
// };
gp_hash_table<ll, ll > table;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	hash<string> Hash;
	ll count,ans=0;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			t+=s[j];
			ll n=Hash(t);
			table[n]=1;
		}
	}
	cout<<sz(table)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=1e18+7,sm=101;
ll k,fhash[mx],Exp[mx];
// Map[10000000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	set <ll> S;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		pro=Pow(Exp[i],mod-2);
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			// if(ap[hash]==0)
			// 	ans++;
			// Map[hash]++;
			S.insert(hash);
		}
	}
	cout<<sz(S)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
ll n,k;
string s,good;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	ll count,ans=0;
	unordered_map <ll,ll> map;
	hash<string> Hash;
	rep(i,0,sz(s)){
		count=0;
		string t;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			t+=s[j];
			n=Hash(t);
			map[n]++;
		}
	}
	cout<<sz(map)<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define rep2(i,r,l) for(int i=r;i>l;i--)
#define sz(a) (ll)a.size()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define nl "\n"
#define pii pair<ll,ll>
#define F first
#define S second
#define debug1(x) cout<<x<<endl;
#define debug2(x,y) cout<<x<<" -> "<<y<<endl;
#define debug3(x,y,z) cout<<x<<" -> "<<y<<" -> "<<z<<endl;
#define trace(c,x) for(auto &x:c)
string s,good;
const ll mx=1505,mod=99991,sm=101;
ll k,fhash[mx],Exp[mx],Map[100000];
ll Pow(ll x,ll y){
	if(y==0)
		return 1;
	ll temp=Pow(x,y/2);
	if(y%2==0)
		return (temp*temp)%mod;
	return (x*((temp*temp)%mod))%mod;
}
void precomp(){
	Exp[0]=1;
	rep(i,1,mx)
		Exp[i]=(101*Exp[i-1])%mod;
	fhash[0]=0;
	fhash[1]=s[0];
	rep(i,2,mx)
		fhash[i]=(fhash[i-1]+((s[i-1]*Exp[i-1])%mod))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL),cout.tie(NULL);
	cin>>s>>good>>k;
	s="#"+s;
	precomp();
	// unordered_map<ll,ll>map;
	ll count,pro,hash,ans=0;
	rep(i,1,sz(s)){
		count=0;
		rep(j,i,sz(s)){
			if(!(good[s[j]-'a']-'0'))
				count++;
			if(count>k)
				break;
			pro=Pow(Exp[i],mod-2);
			hash=(((fhash[j+1]-fhash[i]+mod)%mod)*pro)%mod;
			if(Map[hash]==0)
				ans++;
			Map[hash]++;
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<int,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1610612741
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 300000000077
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll unsigned long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define mod2 1000000033
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll b1 = 29;
ll b2 = 31;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    map<pair<ll,ll> ,bool> M;

    rep(i,0,s.length()){
        ll currhash1=0, currhash2=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash1=(((currhash1*b1)%mod)+(s[j]-'a'+1))%mod;
            currhash2=(((currhash2*b2)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find({currhash1, currhash2})==M.end()){
                M[{currhash1,currhash2}];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 300000000101
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 200099946119
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll unsigned long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define mod2 1000000033
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll b1 = 29;
ll b2 = 31;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    set<pair<ll,ll>> M;

    rep(i,0,s.length()){
        ll currhash1=0, currhash2=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash1=(((currhash1*b1)%mod)+(s[j]-'a'+1))%mod;
            currhash2=(((currhash2*b2)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.count({currhash1, currhash2})==0){
                M.insert({currhash1,currhash2});
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 100002520361
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 31;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<int,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll unsigned long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define mod2 1000000033
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll b1 = 29;
ll b2 = 31;
struct trie{
    trie* next[26];
    bool visited;
    trie(){
        visited=false;
        for(int i=0; i<26; i++){
            next[i]=NULL;
        }
    }
};
void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string str;cin>>str;
    int maxx;cin>>maxx;
    trie* root = new trie();
    for(int i=0; i<s.length(); i++){
        trie* temp = root;
        for(int j=i; j<s.length(); j++){
            if(temp->next[s[j]-'a']==NULL){
                temp->next[s[j]-'a']=new trie();
            }
            temp=temp->next[s[j]-'a'];
        }
    }

    int ans=0;

    for(int i=0; i<s.length(); i++){
        trie* temp = root;
        int k=0;
        for(int j=i; j<s.length(); j++){
            k+=(str[s[j]-'a']=='0');
            temp=temp->next[s[j]-'a'];
            if(k<=maxx && temp->visited==false){
                ans++;
                temp->visited=true;
            }

        }
    }
    cout << ans << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 100002854797
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1125899906842597LL
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 13331;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 100019946863
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1125899906842597LL
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 29;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll unsigned long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 13331;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535897
#define ll long long
#define tr1(n) cout << n << endl
#define tr2(n1,n2) cout << n1 << " " << n2 << endl
#define mem(A,i) memset(A, i, sizeof(A))
#define rep(i, start, end) for(int i=start; i<end; i++)
#define repDown(i, start, end) for(int i=start; i>=end; i--)
#define mod 1000000007
#define MAX 1003
#define INF 1+1e18
#define s second
#define f first
#define pb push_back
#define fast_in std::ios::sync_with_stdio(false);
#define fast_cin fast_in; ios_base::sync_with_stdio(false); cin.tie(NULL);
ll base = 13331;

void solve(){
    //solve the problem. You can and you will :) give your best shot..
    string s;
    cin>>s;
    string arr;
    cin>>arr;
    int k;cin>>k;
    unordered_map<ll,bool> M;

    rep(i,0,s.length()){
        ll currhash=0;
        ll currk=0;
        rep(j,i,s.length()){
            currhash=(((currhash*base)%mod)+(s[j]-'a'+1))%mod;
            currk+=(arr[s[j]-'a']=='0');
            if(currk<=k && M.find(currhash)==M.end()){
                M[currhash];
            }
            if(currk>k){
                break;
            }
        }
    }
    cout << M.size() << endl;

}

int main(){
    fast_cin;
    int t=1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
map<string,int>mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
    ans+=x[i];
    if(m[x[i]-'a']==0) { if(!mp[ans]) {++d; mp[ans]++;} maxx++; }
    else {if (mp[ans]==0){mp[ans]++; d++; } }
    if (maxx==num) k=1;
    else if (maxx>num&&d>0) d--;
         for(int j=i+1; j<x.size(); ++j)
           {    if (k&&m[x[j]-'a']==0) break;
           ans+=x[j];
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                              if (!mp[ans]&&maxx<=num) {d++; mp[ans]++;}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<d<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mod 2000000011ll
#define b 10007
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
 set<ll> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           sum=(sum*b)%mod;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mod 2000000011ll
#define b 53ll
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
 set<ll> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           sum=(sum*b)%mod;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mod 2000000011ll
#define b 53ll
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
 set<ll> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           sum=(sum*b)%mode;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include<unordered_set>
#define ll long long
#define mod 2000000011ll
#define b 53ll
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
 unordered_set<ll> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           ans+=x[j];
           sum=sum*b;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_CHAR = 26;

int main()
{
string x,y;
cin>>x>>y;
int num;
cin>>num;
map<char,int>m; int j=0;
map<string,int>mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[i]=0;
else m[i]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
    ans+=x[i];
    if(m[x[i]]==0) { if(!mp[ans]) {++d; mp[ans]++;} maxx++; }
    else {if (mp[ans]==0){mp[ans]++; d++; } }
    if (maxx==num) k=1;
    else if (maxx>num&&d>0) d--;
         for(int j=i+1; j<x.size(); ++j)
           {    if (k&&m[x[j]]==0) break;
           ans+=x[j];
               if(m[x[j]]==0) {  ++maxx;
                  if (maxx>num) break;

               }
                              if (!mp[ans]) {d++; mp[ans]++;}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<d<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define mod 2000000011ll
#define b 53ll
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
 set<ll> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           ans+=x[j];
           sum=sum*b;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#include<unordered_set>

#define ll long long
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
unordered_set<string> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
         for(int j=i; j<x.size(); ++j)
           {
           ans+=x[j];
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(ans);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_CHAR = 26;

int main()
{
string x,y;
cin>>x>>y;
int num;
cin>>num;
map<char,int>m; int j=0;
map<string,int>mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[i]=0;
else m[i]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
    ans+=x[i];
    if(m[x[i]]==0) { if(!mp[ans]) {++d; mp[ans]++;} maxx++; }
    else {if (mp[ans]==0){mp[ans]++; d++; } }
    if (maxx==num) k=1;
    else if (maxx>num&&d>0) d--;
         for(int j=i+1; j<x.size(); ++j)
           {    if (k&&m[x[j]]==0) break;
           ans+=x[j];
               if(m[x[j]]==0) {  ++maxx;
                  if (maxx>num) break;

               }
                              if (!mp[ans]&&maxx<=num) {d++; mp[ans]++;}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<d<<endl;

    return 0;
}


#include <bits/stdc++.h>

#define ll long long
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
set<ll> mp;
ll b=10,mod=100000000000000007;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           ans+=x[j];
           sum=sum*b;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_CHAR = 26;

int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int num;
cin>>num;
map<char,int>m; int j=0;
map<string,int>mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[i]=0;
else m[i]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
    ans+=x[i];
    if(m[x[i]]==0) { if(!mp[ans]) {++d; mp[ans]++;} maxx++; }
    else {if (mp[ans]==0){mp[ans]++; d++; } }
    if (maxx==num) k=1;
    else if (maxx>num&&d>0) d--;
         for(int j=i+1; j<x.size(); ++j)
           {    if (k&&m[x[j]]==0) break;
           ans+=x[j];
               if(m[x[j]]==0) {  ++maxx;
                  if (maxx>num) break;

               }
                              if (!mp[ans]&&maxx<=num) {d++; mp[ans]++;}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<d<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
//#include<unordered_set>
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
int num;
cin>>num;
 int j=0;
set<string> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
         for(int j=i; j<x.size(); ++j)
           {
           ans+=x[j];
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(ans);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_CHAR = 26;

int main()
{
string x,y;
cin>>x>>y;
int num;
cin>>num;
map<char,int>m; int j=0;
map<string,int>mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[i]=0;
else m[i]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
    ans+=x[i];
    if(m[x[i]]==0) { if(!mp[ans]) {++d; mp[ans]++;} maxx++; }
    else {if (mp[ans]==0){mp[ans]++; d++; } }
    if (maxx==num) k=1;
    else if (maxx>num) d--;
         for(int j=i+1; j<x.size(); ++j)
           {    if (k&&m[x[j]]==0) break;
           ans+=x[j];
               if(m[x[j]]==0) {  ++maxx;
                  if (maxx>num) break;

               }
                              if (!mp[ans]) {d++; mp[ans]++;}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<d<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define b 53ll
using namespace std;
int arr[1000000]={0};

const int MAX_CHAR = 26;
int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie();
string x,y;
cin>>x>>y;
int m[26];
ll mod= 1E16 + 371;
int num;
cin>>num;
 int j=0;
 set<ll> mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[j]=0;
else m[j]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans; ll sum=0;
         for(int j=i; j<x.size(); ++j)
           {
           sum=(sum*b)%mod;
           sum+=x[j];
           sum%=mod;
               if(m[x[j]-'a']==0) {  ++maxx;
                  if (maxx>num) break;

               }
                        {mp.insert(sum);}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<mp.size()<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int MAX_CHAR = 26;

int main()
{
string x,y;
cin>>x>>y;
int num;
cin>>num;
map<char,int>m; int j=0;
map<string,int>mp;
for(char i='a'; i<='z'; ++i)
    {if(y[j]=='0') m[i]=0;
else m[i]=1;
++j;
    }
    ll d=0;
    for(int i=0 ; i<x.size(); ++i)
    {  ll maxx=0; bool k=0; string ans;
    ans+=x[i];
    if(m[x[i]]==0) { if(!mp[ans]) {++d; mp[ans]++;} maxx++; }
    else {if (mp[ans]==0){mp[ans]++; d++; } }
    if (maxx==num) k=1;
         for(int j=i+1; j<x.size(); ++j)
           {    if (k&&m[x[j]]==0) break;
           ans+=x[j];
               if(m[x[j]]==0) {  ++maxx;
                  if (maxx>num) break;

               }
                              if (!mp[ans]) {d++; mp[ans]++;}
//cout<<ans<<" "<<d<<endl;
           }

    }
cout<<d<<endl;

    return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

#if DEBUG
    #include "debug.h"
#else
    #define debug(args...)
#endif

#define int long long

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;

#define M 1000000
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e18;
int mod = 1e9 + 7;

const int N = 2e5 + 10;
inline int add(int x,int y){
  x += y;
  if(x >= mod) x -= mod;
  return x;
}
inline int mul(int x,int y){
  x = (1LL * x * y) % mod;
  return x;
}
clock_t time_p=clock();

int base = 256;

int32_t main(){
  fast;

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  string s,good;
  cin >> s >> good;
  int k;
  cin >> k;
  set<int>st;
  for(int i = 0; i < s.length(); i++) {
    int hashv = 0;
    int powp = 1;
    int cc = 0;
    for(int j = i; j < s.length(); j++) {
      hashv = (hashv + (s[j] - '0') * powp) % mod;
      powp = (powp * base) % mod;
      if(good[s[j]-'a'] == '0')
        cc++;
      if(cc <= k)
        st.insert(hashv);
    }
  }

  cout << sz(st);

  time_p=clock()-time_p;
  cerr<<"\nTime Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

#if DEBUG
    #include "debug.h"
#else
    #define debug(args...)
#endif

#define int long long

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;

#define M 1000000
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e18;
const int mod = 1e9+7;

const int N = 2e5 + 10;
inline int add(int x,int y){
  x += y;
  if(x >= mod) x -= mod;
  return x;
}
inline int mul(int x,int y){
  x = (1LL * x * y) % mod;
  return x;
}
clock_t time_p=clock();

int base = rng() % (int)1e8 + 1e8;

int32_t main(){
  fast;

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  string s,good;
  cin >> s >> good;
  int k;
  cin >> k;
  set<int>st;
  for(int i = 0; i < s.length(); i++) {
    int hashv = 0;
    int powp = 1;
    int cc = 0;
    for(int j = i; j < s.length(); j++) {
      hashv = (hashv + (s[j] - '0') * powp) % mod;
      powp = (powp * base) % mod;
      if(good[s[j]-'a'] == '0')
        cc++;
      if(cc <= k)
        st.insert(hashv);
    }
  }

  cout << sz(st);

  time_p=clock()-time_p;
  cerr<<"\nTime Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

#if DEBUG
    #include "debug.h"
#else
    #define debug(args...)
#endif

#define int long long

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;

#define M 1000000
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e18;
int mod = 1e9 + 7;

const int N = 2e5 + 10;
inline int add(int x,int y){
  x += y;
  if(x >= mod) x -= mod;
  return x;
}
inline int mul(int x,int y){
  x = (1LL * x * y) % mod;
  return x;
}
clock_t time_p=clock();

int base = rng() % (int)1e8 + 1e8;

int32_t main(){
  fast;

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  string s,good;
  cin >> s >> good;
  int k;
  cin >> k;
  set<int>st;
  for(int i = 0; i < s.length(); i++) {
    int hashv = 0;
    int powp = 1;
    int cc = 0;
    for(int j = i; j < s.length(); j++) {
      hashv = (hashv + (s[j] - '0') * powp) % mod;
      powp = (powp * base) % mod;
      if(good[s[j]-'a'] == '0')
        cc++;
      if(cc <= k)
        st.insert(hashv);
    }
  }

  cout << sz(st);

  time_p=clock()-time_p;
  cerr<<"\nTime Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";

  return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

#if DEBUG
    #include "debug.h"
#else
    #define debug(args...)
#endif

#define int long long

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;
typedef long long ll;

#define M 1000000
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define F first
#define S second
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e18;
int mod = rng() % (int)1e8 + 1e9;

const int N = 2e5 + 10;
inline int add(int x,int y){
  x += y;
  if(x >= mod) x -= mod;
  return x;
}
inline int mul(int x,int y){
  x = (1LL * x * y) % mod;
  return x;
}
clock_t time_p=clock();

int base = rng() % (int)1e8 + 1e8;

int32_t main(){
  fast;

  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  string s,good;
  cin >> s >> good;
  int k;
  cin >> k;
  set<int>st;
  for(int i = 0; i < s.length(); i++) {
    int hashv = 0;
    int powp = 1;
    int cc = 0;
    for(int j = i; j < s.length(); j++) {
      hashv = (hashv + (s[j] - '0') * powp) % mod;
      powp = (powp * base) % mod;
      if(good[s[j]-'a'] == '0')
        cc++;
      if(cc <= k)
        st.insert(hashv);
    }
  }

  cout << sz(st);

  time_p=clock()-time_p;
  cerr<<"\nTime Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";

  return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;

using namespace std;

#if DEBUG && !ONLINE_JUDGE
    #include "debug.h"
#else
    #define debug(...)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<vector<int> > vvi;
typedef vector<vector<ii> > vvii;

#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define ff first
#define ss second
#define trav(a, x) for(auto& a : x)
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
const ll INF = 1e18;
const int mod = 1e9+7;
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

//*X.find_by_order(2) element at index=2
//X.order_of_key(1) how many elements strictly less than 1

#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

inline int pow_(ll x, ll y,ll p){
  int r = 1; while(y){if(y & 1) r = r * x % p; y >>= 1; x = x * x % p;} return r;
}
inline int inv_(int x){return pow_(x, mod - 2, mod);}
inline int add(int a, int b){a += b; if(a >= mod) a -= mod; return a;}
inline int mul(int a, int b){return a * 1LL * b % mod;}
inline int sub(int a, int b){a -= b; if(a < 0) a += mod; return a;}

mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
ll m = 801258244502321LL, p = 256;

int main()
{
    #ifdef LOCAL_TEST
    freopen("in.txt","r",stdin);
    //freopen("out.txt", "w", stdout);
    #else
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #endif
    string s,t;
    cin>>s>>t;
    ll k;
    cin>>k;
    set<ll> w;
    for(int i=0;i<s.size();i++){
      int cnt = 0;
      ll hash_value = 0;
      ll p_pow = 1;
      string q;
      for(int j=i;j<s.size();j++){
        hash_value = (hash_value + (s[j]-'a'+1)*p_pow) % m;
        p_pow = (p_pow*p)%m;
        if(!(t[s[j]-'a']-'0')){
          cnt++;
        }
        if(cnt<=k){
          w.insert(hash_value);
        } else{
          break;
        }
      }
    }

    cout << w.size() << "\n";
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;

using namespace std;

#if DEBUG && !ONLINE_JUDGE
    #include "debug.h"
#else
    #define debug(...)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<vector<int> > vvi;
typedef vector<vector<ii> > vvii;

#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define fill(a,x) memset(a,x,sizeof(a))
#define ff first
#define ss second
#define trav(a, x) for(auto& a : x)
#define FOR(i,a,b) for(int i = a; i<=b; ++i)
#define NFOR(i,a,b) for(int i = a; i>=b; --i)
const ll INF = 1e18;
const int mod = 1e9+7;
ll gcd(ll a,ll b){if (a==0) return b;return gcd(b%a,a);}
ll lcm(ll a,ll b){return a*(b/gcd(a,b));}
ll Abs(ll a){if(a>0)return a;return -a;}
ll Ceil(ll a,ll b){if(a%b==0)return a/b;else return a/b+1;}
double Abs(double a){if(a>0)return a;return -a;}

//*X.find_by_order(2) element at index=2
//X.order_of_key(1) how many elements strictly less than 1

#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

inline int pow_(ll x, ll y,ll p){
  int r = 1; while(y){if(y & 1) r = r * x % p; y >>= 1; x = x * x % p;} return r;
}
inline int inv_(int x){return pow_(x, mod - 2, mod);}
inline int add(int a, int b){a += b; if(a >= mod) a -= mod; return a;}
inline int mul(int a, int b){return a * 1LL * b % mod;}
inline int sub(int a, int b){a -= b; if(a < 0) a += mod; return a;}

mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
ll m = (1ll<<48) - 1, p = 256;

int main()
{
    #ifdef LOCAL_TEST
    freopen("in.txt","r",stdin);
    //freopen("out.txt", "w", stdout);
    #else
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #endif
    string s,t;
    cin>>s>>t;
    ll k;
    cin>>k;
    set<ll> w;
    for(int i=0;i<s.size();i++){
      int cnt = 0;
      ll hash_value = 0;
      ll p_pow = 1;
      string q;
      for(int j=i;j<s.size();j++){
        hash_value = (hash_value + (s[j]-'a'+1)*p_pow) % m;
        p_pow = (p_pow*p)%m;
        if(!(t[s[j]-'a']-'0')){
          cnt++;
        }
        if(cnt<=k){
          w.insert(hash_value);
        } else{
          break;
        }
      }
    }

    cout << w.size() << "\n";
}