#include <bits/stdc++.h>
using namespace std;
vector<int> V[1500 * 1500 + 200];
char x[1500 * 1500 + 200];
int cnt = 1, now;
void check(char s) {
  bool flag = true;
  for (int i = 0; i < (int)V[now].size(); i++) {
    if (x[V[now][i]] == s) {
      flag = false;
      now = V[now][i];
      break;
    }
  }
  if (flag) {
    cnt++;
    x[cnt] = s;
    V[now].push_back(cnt);
    now = cnt;
  }
}
int main() {
  char s[1510];
  char a[27];
  int y;
  cin >> s;
  cin >> a;
  scanf("%d", &y);
  int len = strlen(s);
  cnt = 1;
  for (int i = 0; i < len; i++) {
    int num = 0;
    now = 1;
    for (int j = i; j < len; j++) {
      if (a[s[j] - 'a'] == '0') num++;
      if (num > y) break;
      check(s[j]);
    }
  }
  printf("%d\n", cnt - 1);
}
