#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 25;
const int M = 10;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	if (x == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 22;
const int M = 7;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	if (x == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
		cout << "\n";
		return;
	}
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 20;
const int M = 10;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	if (x == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 22;
const int M = 7;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	if (x == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 4e5;
const int Log = 30;
const int M = 10;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	if (x == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		ll sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 20;
const int M = 10;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		int sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		int sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	if (k == 0) {
		forn(i, 1, m)
			cout << "0 " ;
		cout << "\n";
	}
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 20;
const int M = 10;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		int sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		int sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;   /*
	if (k == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}                   */
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for (int i = int(x); i <= int(n); ++i)
#define for1(i, n, x) for (int i = int(n); i >= int(x); --i)
#define file ""
#define x first
#define y second
#define pb push_back

typedef long long ll;
typedef pair <int, int> PII;
typedef const PII & pnt;

const int N = 3e5;
const int Log = 20;
const int M = 10;
const int INF = 1e9 + 9;

int n, m, k;
int a[M][N];

struct SparseTable {
	int n, t[Log][N], lg[N];

	void init(int _n, int *a) {
		n = _n;
		lg[1] = 0;
		forn(i, 2, n)
			lg[i] = lg[i / 2] + 1;
		forn(i, 1, n)
			t[0][i] = a[i];
		forn(lvl, 1, lg[n])
			forn(i, 1, n)
				t[lvl][i] = max(t[lvl - 1][i], t[lvl - 1][i + (1 << (lvl - 1))]);
	}

	int g_max(int l, int r) {
		int d = (r - l + 1);
		return max(t[lg[d]][l], t[lg[d]][r - (1 << lg[d]) + 1]);
	}
} sp_t[M];

bool can(int x) {
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		int sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum <= k) return 1;
	}
	return 0;
}

void restore(int x) {
	if (x == 0) {
		forn(i, 1, m)
			cout << "0 ";
		cout << "\n";
	}
	forn(l, 1, n - x + 1) {
		int r = l + x - 1;
		int sum = 0;
		forn(i, 1, m)
			sum += sp_t[i].g_max(l, r);
		if (sum > k) continue;
		forn(i, 1, m)
			cout << sp_t[i].g_max(l, r) << " ";
	}
	cout << "\n";
}

int main () {
#ifdef machine42
	freopen (file"in", "r", stdin);
	freopen (file"out", "w", stdout);
#endif
	ios_base :: sync_with_stdio (0);
	cin.tie(0);

	cin >> n >> m >> k;
	forn(i, 1, n)
		forn(j, 1, m)
			cin >> a[j][i];

	forn(i, 1, m)
		sp_t[i].init(n, a[i]);

	int l = 0, r = n, mid = 0;
	while (l < r) {
		mid = (l + r + 1) >> 1;
		if (can(mid)) l = mid;
		else r = mid - 1;
	}

    restore(l);

	return 0;
}


#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <queue>
#include <bitset>
using namespace std;
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define pb push_back
#define fst first
#define snd second
#define SZ(x) ((int)x.size())
typedef long long ll;
#define dforn(i, n) for(int i = n - 1; i >= 0; i--)
const int MAXN=100100;

int n,m,k,A[MAXN][5],ans[5];

bool P(int sz){
  ll cand; bool ok=false;
  multiset<int> S[5];
  forn(i,sz){
    forn(j,m) S[j].insert(A[i][j]); // agrega elemento i
  }
  cand=0;forn(j,m)cand+=*S[j].rbegin();
  if(cand<=k){ forn(j,m)ans[j]=*S[j].rbegin(); ok=true; }
  forr(i,sz,n){
    forn(j,m) S[j].insert(A[i][j]);
    forn(j,m) S[j].erase(S[j].find(A[i-sz][j]));

    cand=0;forn(j,m)cand+=*S[j].rbegin();
    if(cand<=k){ forn(j,m)ans[j]=*S[j].rbegin(); ok=true; }
  }
  return ok;
}

int main() {
  //~ freopen("input.in","r",stdin);
  while(scanf("%d%d%d",&n,&m,&k)>=1){
    forn(i,n){
      forn(j,m){
        scanf("%d",&A[i][j]);
      }
    }
    int a=0;
    int b=n+1;
    while(b-a>1){
      int c=(a+b)/2;
      if(P(c)){
        a=c;
      } else {
        b=c;
      }
    }
    //~ printf("kills %d\n",a);
    if(a){
      P(a);
    } else {
      memset(ans,0,sizeof(ans));
    }
    forn(i,m){
      if(i)printf(" ");
      printf("%d",ans[i]);
    }
    puts("");
  }
  return 0;
}


#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <queue>
#include <bitset>
using namespace std;
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define pb push_back
#define fst first
#define snd second
#define SZ(x) ((int)x.size())
typedef long long ll;
#define dforn(i, n) for(int i = n - 1; i >= 0; i--)
const int MAXN=100100;

int n,m,k,A[MAXN][5],ans[5];

struct MaxQueue {
  deque<int> d; queue<int> q;
  void push(int v){while(SZ(d)&&d.back()<v)d.pop_back();d.pb(v);q.push(v);}
  void pop(){if(SZ(d)&&d.front()==q.front())d.pop_front();q.pop();}
  int getMax(){return d.front();}
};

bool P(int sz){
  ll cand; bool ok=false;
  MaxQueue S[5];
  forn(i,sz){
    forn(j,m) S[j].push(A[i][j]); // agrega elemento i
  }
  cand=0;forn(j,m)cand+=S[j].getMax();
  if(cand<=k){ forn(j,m)ans[j]=S[j].getMax(); ok=true; }
  forr(i,sz,n){
    forn(j,m) S[j].push(A[i][j]);
    forn(j,m) S[j].pop();

    cand=0;forn(j,m)cand+=S[j].getMax();
    if(cand<=k){ forn(j,m)ans[j]=S[j].getMax(); ok=true; }
  }
  return ok;
}

int main() {
  //~ freopen("input.in","r",stdin);
  while(scanf("%d%d%d",&n,&m,&k)>=1){
    forn(i,n){
      forn(j,m){
        scanf("%d",&A[i][j]);
      }
    }
    int a=0;
    int b=n+1;
    while(b-a>1){
      int c=(a+b)/2;
      if(P(c)){
        a=c;
      } else {
        b=c;
      }
    }
    //~ printf("kills %d\n",a);
    if(a){
      P(a);
    } else {
      memset(ans,0,sizeof(ans));
    }
    forn(i,m){
      if(i)printf(" ");
      printf("%d",ans[i]);
    }
    puts("");
  }
  return 0;
}


#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <queue>
#include <bitset>
using namespace std;
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define pb push_back
#define fst first
#define snd second
#define SZ(x) ((int)x.size())
typedef long long ll;
#define dforn(i, n) for(int i = n - 1; i >= 0; i--)
const int MAXN=100100;

int n,m,k,A[MAXN][5],ans[5];

void add(deque<int> &d, int v){
  while(SZ(d) && d.back() < v)
    d.pop_back();
  d.push_back(v);
}
void remove(deque<int> &d, int v){
  if(SZ(d) && d.front()==v)
    d.pop_front();
}
int getMax(deque<int> &d){
  return d.front();
}

bool P(int sz){
  ll cand; bool ok=false;
  deque<int> S[5];
  forn(i,sz){
    forn(j,m) add(S[j], A[i][j]); // agrega elemento i
  }
  cand=0;forn(j,m)cand+=getMax(S[j]);
  if(cand<=k){ forn(j,m)ans[j]=getMax(S[j]); ok=true; }
  forr(i,sz,n){
    forn(j,m) add(S[j], A[i][j]);
    forn(j,m) remove(S[j], A[i-sz][j]);

    cand=0;forn(j,m)cand+=getMax(S[j]);
    if(cand<=k){ forn(j,m)ans[j]=getMax(S[j]); ok=true; }
  }
  return ok;
}

int main() {
  //~ freopen("input.in","r",stdin);
  while(scanf("%d%d%d",&n,&m,&k)>=1){
    forn(i,n){
      forn(j,m){
        scanf("%d",&A[i][j]);
      }
    }
    int a=0;
    int b=n+1;
    while(b-a>1){
      int c=(a+b)/2;
      if(P(c)){
        a=c;
      } else {
        b=c;
      }
    }
    //~ printf("kills %d\n",a);
    if(a){
      P(a);
    } else {
      memset(ans,0,sizeof(ans));
    }
    forn(i,m){
      if(i)printf(" ");
      printf("%d",ans[i]);
    }
    puts("");
  }
  return 0;
}


#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <queue>
#include <bitset>
using namespace std;
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define pb push_back
#define fst first
#define snd second
#define SZ(x) ((int)x.size())
typedef long long ll;
#define dforn(i, n) for(int i = n - 1; i >= 0; i--)
const int MAXN=100100;

int n,m,k,A[MAXN][5],ans[5];

bool P(int sz){
  ll cand; bool ok=false;
  multiset<int> S[5];
  forn(i,sz){
    forn(j,m) S[j].insert(A[i][j]); // agrega elemento i
  }
  cand=0;forn(j,m)cand+=*S[j].rbegin();
  if(cand<=k){ forn(j,m)ans[j]=*S[j].rbegin(); ok=true; }
  forr(i,sz,n){
    forn(j,m) S[j].insert(A[i][j]);
    forn(j,m) S[j].erase(S[j].find(A[i-sz][j]));

    cand=0;forn(j,m)cand+=*S[j].rbegin();
    if(cand<=k){ forn(j,m)ans[j]=*S[j].rbegin(); ok=true; }
  }
  return ok;
}

int main() {
  //~ freopen("input.in","r",stdin);
  while(scanf("%d%d%d",&n,&m,&k)>=1){
    forn(i,n){
      forn(j,m){
        scanf("%d",&A[i][j]);
      }
    }
    int a=0;
    int b=n+1;
    while(b-a>1){
      int c=(a+b)/2;
      if(P(c)){
        a=c;
      } else {
        b=c;
      }
    }
    //~ printf("kills %d\n",a);
    P(a);
    forn(i,m){
      if(i)printf(" ");
      printf("%d",ans[i]);
    }
    puts("");
  }
  return 0;
}


#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <queue>
#include <bitset>
using namespace std;
#define forr(i, a, b) for(int i = (a); i < (int) (b); i++)
#define forn(i, n) forr(i, 0, n)
#define pb push_back
#define fst first
#define snd second
#define SZ(x) ((int)x.size())
typedef long long ll;
#define dforn(i, n) for(int i = n - 1; i >= 0; i--)
const int MAXN=100100;

int n,m,k,A[MAXN][5],ans[5];

struct MaxQueue {
  deque<int> q;
  void push(int v){while(SZ(q)&&q.back()<v)q.pop_back();q.pb(v);}
  void pop(int v){if(SZ(q)&&q.front()==v)q.pop_front();}
  int getMax(){return q.front();}
};

bool P(int sz){
  ll cand; bool ok=false;
  MaxQueue S[5];
  forn(i,sz){
    forn(j,m) S[j].push(A[i][j]); // agrega elemento i
  }
  cand=0;forn(j,m)cand+=S[j].getMax();
  if(cand<=k){ forn(j,m)ans[j]=S[j].getMax(); ok=true; }
  forr(i,sz,n){
    forn(j,m) S[j].push(A[i][j]);
    forn(j,m) S[j].pop(A[i-sz][j]);

    cand=0;forn(j,m)cand+=S[j].getMax();
    if(cand<=k){ forn(j,m)ans[j]=S[j].getMax(); ok=true; }
  }
  return ok;
}

int main() {
  //~ freopen("input.in","r",stdin);
  while(scanf("%d%d%d",&n,&m,&k)>=1){
    forn(i,n){
      forn(j,m){
        scanf("%d",&A[i][j]);
      }
    }
    int a=0;
    int b=n+1;
    while(b-a>1){
      int c=(a+b)/2;
      if(P(c)){
        a=c;
      } else {
        b=c;
      }
    }
    //~ printf("kills %d\n",a);
    if(a){
      P(a);
    } else {
      memset(ans,0,sizeof(ans));
    }
    forn(i,m){
      if(i)printf(" ");
      printf("%d",ans[i]);
    }
    puts("");
  }
  return 0;
}


#include <cstdio>
#include <cstring>
#define INF 2000000000
#define fin stdin
#define fout stdout
int n, m, v[100001][5], sol[5], st[5], dr[5], dq[100001][5];
inline void dreapta(int l){
    int i;
    for(i=0; i<m; i++){
        while((st[i]<dr[i])&&(v[dq[dr[i]-1][i]][i]<=v[l][i])){
            dr[i]--;
        }
        dq[dr[i]][i]=l;
        dr[i]++;
    }
}
inline void stanga(int l){
    int i;
    for(i=0; i<m; i++){
        if(dq[st[i]][i]==l){
            st[i]++;
        }
    }
}
inline int cat(int l){
    int min, i, j;
    long long acum;
    memset(dq, 0, sizeof dq);
    memset(st, 0, sizeof st);
    memset(dr, 0, sizeof dr);
    min=INF;
    for(i=1; i<l; i++){
        dreapta(i);
    }
    for(i=l; i<=n; i++){
        acum=0;
        dreapta(i);
        for(j=0; j<m; j++){
            acum+=v[dq[st[j]][j]][j];
        }
        if(acum<min){
            min=acum;
            for(j=0; j<m; j++){
                sol[j]=v[dq[st[j]][j]][j];
            }
        }
        stanga(i-l+1);
    }
    return min;
}
int main(){
    int k, i, j, rez, pas;
    //FILE *fin, *fout;
    //fin=fopen("a.in", "r");
    //fout=fopen("a.out", "w");
    fscanf(fin, "%d%d%d", &n, &m, &k);
    for(i=1; i<=n; i++){
        for(j=0; j<m; j++){
            fscanf(fin, "%d", &v[i][j]);
        }
    }
    rez=0;
    for(pas=1<<16; pas; pas>>=1){
        if((rez+pas<=n)&&(cat(rez+pas)<=k)){
            rez+=pas;
        }
    }
    i=cat(rez);
    for(i=0; i<m; i++){
        fprintf(fout, "%d ", sol[i]);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}


#include <cstdio>
#include <cstring>
#define INF 2000000000
#define fin stdin
#define fout stdout
int n, m, v[100001][5], sol[5], st[5], dr[5], dq[100001][5];
inline void dreapta(int l){
    int i;
    for(i=0; i<m; i++){
        while((st[i]<dr[i])&&(v[dq[dr[i]-1][i]][i]<=v[l][i])){
            dr[i]--;
        }
        dq[dr[i]][i]=l;
        dr[i]++;
    }
}
inline void stanga(int l){
    int i;
    for(i=0; i<m; i++){
        if(dq[st[i]][i]==l){
            st[i]++;
        }
    }
}
inline int cat(int l){
    int min, i, j;
    long long acum;
    memset(dq, 0, sizeof dq);
    memset(st, 0, sizeof st);
    memset(dr, 0, sizeof dr);
    min=INF;
    for(i=1; i<l; i++){
        dreapta(i);
    }
    for(i=l; i<=n; i++){
        acum=0;
        stanga(i-l);
        dreapta(i);
        for(j=0; j<m; j++){
            acum+=v[dq[st[j]][j]][j];
        }
        if(acum<min){
            min=acum;
            for(j=0; j<m; j++){
                sol[j]=v[dq[st[j]][j]][j];
            }
        }
    }
    return min;
}
int main(){
    int k, i, j, rez, pas;
    //FILE *fin, *fout;
    //fin=fopen("a.in", "r");
    //fout=fopen("a.out", "w");
    fscanf(fin, "%d%d%d", &n, &m, &k);
    for(i=1; i<=n; i++){
        for(j=0; j<m; j++){
            fscanf(fin, "%d", &v[i][j]);
        }
    }
    rez=0;
    for(pas=1<<16; pas; pas>>=1){
        if((rez+pas<=n)&&(cat(rez+pas)<=k)){
            rez+=pas;
        }
    }
    i=cat(rez);
    for(i=0; i<m; i++){
        fprintf(fout, "%d ", sol[i]);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}


#include <cstdio>
#include <cstring>
#define INF 2000000000
#define fin stdin
#define fout stdout
int n, m, v[100001][5], sol[5], st[5], dr[5], dq[100001][5];
inline void dreapta(int l){
    int i;
    for(i=0; i<m; i++){
        while((st[i]<dr[i])&&(v[dq[dr[i]-1][i]][i]<=v[l][i])){
            dr[i]--;
        }
        dq[dr[i]][i]=l;
        dr[i]++;
    }
}
inline void stanga(int l){
    int i;
    for(i=0; i<m; i++){
        if(dq[st[i]][i]==l){
            st[i]++;
        }
    }
}
inline int cat(int l){
    int min, acum, i, j;
    memset(dq, 0, sizeof dq);
    memset(st, 0, sizeof st);
    memset(dr, 0, sizeof dr);
    min=INF;
    for(i=1; i<l; i++){
        dreapta(i);
    }
    for(i=l; i<=n; i++){
        acum=0;
        stanga(i-l);
        dreapta(i);
        for(j=0; j<m; j++){
            acum+=v[dq[st[j]][j]][j];
        }
        if(acum<min){
            min=acum;
            for(j=0; j<m; j++){
                sol[j]=v[dq[st[j]][j]][j];
            }
        }
    }
    return min;
}
int main(){
    int k, i, j, rez, pas;
    //FILE *fin, *fout;
    //fin=fopen("a.in", "r");
    //fout=fopen("a.out", "w");
    fscanf(fin, "%d%d%d", &n, &m, &k);
    for(i=1; i<=n; i++){
        for(j=0; j<m; j++){
            fscanf(fin, "%d", &v[i][j]);
        }
    }
    rez=0;
    for(pas=1<<16; pas; pas>>=1){
        if((rez+pas<=n)&&(cat(rez+pas)<=k)){
            rez+=pas;
        }
    }
    i=cat(rez);
    for(i=0; i<m; i++){
        fprintf(fout, "%d ", sol[i]);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}


#include <cstdio>
#include <cstring>
#define INF 2000000000
#define fin stdin
#define fout stdout
int n, m, v[100001][5], sol[5], st[5], dr[5], dq[100001][5];
inline void dreapta(int l){
    int i;
    for(i=0; i<m; i++){
        while((st[i]<dr[i])&&(v[dq[dr[i]-1][i]][i]<=v[l][i])){
            dr[i]--;
        }
        dq[dr[i]][i]=l;
        dr[i]++;
    }
}
inline void stanga(int l){
    int i;
    for(i=0; i<m; i++){
        if(dq[st[i]][i]==l){
            st[i]++;
        }
    }
}
inline int cat(int l){
    int min, acum, i, j;
    memset(dq, 0, sizeof dq);
    memset(st, 0, sizeof st);
    memset(dr, 0, sizeof dr);
    min=INF;
    for(i=1; i<l; i++){
        dreapta(i);
    }
    for(i=l; i<=n; i++){
        acum=0;
        stanga(i-l);
        dreapta(i);
        for(j=0; j<m; j++){
            acum+=v[dq[st[j]][j]][j];
        }
        if(acum<min){
            min=acum;
            for(j=0; j<m; j++){
                sol[j]=v[dq[st[j]][j]][j];
            }
        }
    }
    return min;
}
int main(){
    int k, i, j, rez, pas;
    //FILE *fin, *fout;
    //fin=fopen("a.in", "r");
    //fout=fopen("a.out", "w");
    fscanf(fin, "%d%d%d", &n, &m, &k);
    for(i=1; i<=n; i++){
        for(j=0; j<m; j++){
            fscanf(fin, "%d", &v[i][j]);
        }
    }
    printf("%d\n", cat(3));
    rez=0;
    for(pas=1<<16; pas; pas>>=1){
        if((rez+pas<=n)&&(cat(rez+pas)<=k)){
            rez+=pas;
        }
    }
    i=cat(rez);
    for(i=0; i<m; i++){
        fprintf(fout, "%d ", sol[i]);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}


#include <cstdio>
#include <cstring>
#define INF 2000000000
//#define fin stdin
//#define fout stdout
int n, m, v[100001][5], sol[5], st[5], dr[5], dq[100001][5];
inline void dreapta(int l){
    int i;
    for(i=0; i<m; i++){
        while((st[i]<dr[i])&&(v[dq[dr[i]-1][i]][i]<=v[l][i])){
            dr[i]--;
        }
        dq[dr[i]][i]=l;
        dr[i]++;
    }
}
inline void stanga(int l){
    int i;
    for(i=0; i<m; i++){
        if(dq[st[i]][i]==l){
            st[i]++;
        }
    }
}
inline int cat(int l){
    int min, i, j;
    long long acum;
    memset(dq, 0, sizeof dq);
    memset(st, 0, sizeof st);
    memset(dr, 0, sizeof dr);
    min=INF;
    for(i=1; i<l; i++){
        dreapta(i);
    }
    for(i=l; i<=n; i++){
        acum=0;
        dreapta(i);
        for(j=0; j<m; j++){
            acum+=v[dq[st[j]][j]][j];
        }
        if(acum<min){
            min=acum;
            for(j=0; j<m; j++){
                sol[j]=v[dq[st[j]][j]][j];
            }
        }
        stanga(i-l+1);
    }
    return min;
}
int main(){
    int k, i, j, rez, pas;
    FILE *fin, *fout;
    fin=fopen("a.in", "r");
    fout=fopen("a.out", "w");
    fscanf(fin, "%d%d%d", &n, &m, &k);
    for(i=1; i<=n; i++){
        for(j=0; j<m; j++){
            fscanf(fin, "%d", &v[i][j]);
        }
    }
    rez=0;
    for(pas=1<<16; pas; pas>>=1){
        if((rez+pas<=n)&&(cat(rez+pas)<=k)){
            rez+=pas;
        }
    }
    i=cat(rez);
    for(i=0; i<m; i++){
        fprintf(fout, "%d ", sol[i]);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}


#include<bits/stdc++.h>
#define modd
#define INF int(1e9)
#define ll long long int
#define fr first
#define se second
#define pb(a) push_back(a)
#define rep(i,n)  for(int i=0;i<n;i++)
long long int n,m,ans=0;

using namespace std;

int main()
{
	int n,m,k;
	cin>>n>>m>>k;
	vector<int> v(m);
	vector<vector<int> > in(n,v);
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>in[i][j];
	//		cout<<in[i][j]<<" ";
		}
	//	cout<<endl;
	}
	vector<set<pair<int,int> > > s(m);
	vector<set<pair<int,int> > > sdum;
	int st=-1,en=0;
	int ans=0;
	for(int i=0;i<n;i++)
	 {
	 	if(s[0].size())
	 	{
 		en=i+1;
 		int sum=0;
 		for(int j=0;j<m;j++)
 			{
 				s[j].insert(make_pair(in[i][j],i));
 				sum+=(*s[j].rbegin()).first;
 			}
 		if(sum>k)
 		{
 			while(sum>k&&s[0].size())
 			{
				sum=0;
				for(int j=0;j<m;j++)
				{
					s[j].erase(make_pair(in[st][j],st));
					sum+=(*s[j].rbegin()).first;
				}
				st++;
 			}
 		}
 		else
 		{
 			if(int(s[0].size())>ans)
 			{
 				ans=int(s[0].size());
 				sdum=s;
 			}
 		}

	 	}
	 	else
	 	{
	 		st=i;
	 		for(int j=0;j<m;j++)
	 			{
	 				s[j].insert(make_pair(in[i][j],i));
	 		    }

 			if(int(s[0].size())>ans)
 			{
 				ans=int(s[0].size());
 				sdum=s;
 			}

	 	}
	 //   cout<<" ans "<<ans<<endl;
		// for(int j=0;j<m;j++)
		// 	{
		// 	   if(sdum[j].size())
		// 			cout<<(*sdum[j].rbegin()).first<<" ";
		// 	}
		// cout<<endl;

	 }

	 for(int j=0;j<m;j++)
				{
				   if(sdum[j].size())
						cout<<(*sdum[j].rbegin()).first<<" ";
				}
	return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=2000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*37+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
vector <int> v;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
            }
            if(pl<=k)
                v.pb(H);

        }
    }

    if(v.size()==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(v.begin(),v.end());
    forn(i,v.size()-1)
        if(v[i]!=v[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*73+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
vector <int> v;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
            }
            if(pl<=k)
                v.pb(H);

        }
    }

    if(v.size()==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(v.begin(),v.end());
    forn(i,v.size()-1)
        if(v[i]!=v[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*13+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
pair<i64,int> a[2000000];
i64 H,D,MOD=373;
int H2,D2,MOD2=173;
int L=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        D=1;
        H=0;
        D2=1;
        H2=0;

        pl=0;
        forn(i,l)
        {
            D*=MOD;
            D2*=MOD2;
            if(b[s[i]-'a']==0)
                pl++;
            H=H*MOD+s[i];
            H2=H2*MOD2+s[i];
        }
        D/=MOD;
        D2/=MOD2;

        if(pl<=k)
            a[L++]=mp(H,H2);

        for(int i=0; i<s.size()-l; i++)
        {
            H-=D*s[i];
            H2=H2-D2*s[i];

            if(b[s[i]-'a']==0)
                pl--;
            H=H*MOD+s[i+l];
            H2=H2*MOD2+s[i+l];

            if(b[s[i+l]-'a']==0)
                pl++;
            if(pl<=k)
                a[L++]=mp(H,H2);
        }
    }

    if(L==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+L);
    forn(i,L-1)
    {
        if(a[i]!=a[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
                H2=H2*73+s[p];
            }
            pos=(H*H2)%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;

// Constants
const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

// Types
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < i64, i64 > PII;

string s;
char c;
bool b[26];
int k;

i64 H,x=37,step;
int pl;
set <i64> st;
int ans;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        H=0;
        pl=0;
        st.clear();

        forn(i,l)
        {
            H=H*x+s[i];
            if(b[s[i]-'a']==0)
                pl++;
        }
        step=1;
        forn(i,l-1)
            step*=x;
        if(pl<=k)
            st.insert(H);
        for(int i=l; i<s.size(); i++)
        {
            H-=s[i-l]*step;
            H=H*x+s[i];
            if(b[s[i-l]-'a']==0)
                pl--;
            if(b[s[i]-'a']==0)
                pl++;
            if(pl<=k)
                st.insert(H);
        }
        ans+=st.size();
    }
    cout <<ans;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=100000;
string s;
char c;
int k,n;
bool b[30];
int bb[MOD];

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*37+s[p];
                H2=H2*73+s[p];
            }
            if(pl<=k)
                bb[(H*H2)%MOD]=1;
        }
        forn(i,MOD)
        {
            if(bb[i]>0)
                ans++;
            bb[i]=0;
        }
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            u64 H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    unsigned int H=0,H2=0;

    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            H=0;
            H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
                H2=H2+s[p];
            }
            pos=(H+H2)%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
//int a[1000000][2],lpp=0;
pair <PII, int> a[2000000];
int lpp;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0,H2=0,H3=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*137+s[p];
                H2=H2*13+s[p];
                H3=H3*73+s[p];
            }

            if(pl<=k)
                a[lpp++]=mp(mp(H,H2),H3);

        }
    }

    if(lpp==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+lpp);
    forn(i,lpp-1)
        if(a[i]!=a[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
pair<i64,i64> a[2000000];
i64 H,D,MOD=37,H2,D2,MOD2=173;
int L=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        D=1;
        H=0;
        D2=1;
        H2=0;

        pl=0;
        forn(i,l)
        {
            D*=MOD;
            D2*=MOD2;
            if(b[s[i]-'a']==0)
                pl++;
            H=H*MOD+s[i];
            H2=H2*MOD2+s[i];
        }
        D/=MOD;
        D2/=MOD2;

        if(pl<=k)
            a[L++]=mp(H,H2);

        for(int i=0; i<s.size()-l; i++)
        {
            H-=D*s[i];
            H2-=D2*s[i];

            if(b[s[i]-'a']==0)
                pl--;
            H=H*MOD+s[i+l];
            H2=H2*MOD2+s[i+l];

            if(b[s[i+l]-'a']==0)
                pl++;
            if(pl<=k)
                a[L++]=mp(H,H2);
        }
    }

    if(L==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+L);
    forn(i,L-1)
    {
        if(a[i]!=a[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
                //H2=H2*73+s[p];
            }
            pos=(H)%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=400000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*113+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
pair<i64,i64> a[2000000];
i64 H,D,MOD=73;
i64 H2,D2,MOD2=173;
int L=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        D=1;
        H=0;
        D2=1;
        H2=0;

        pl=0;
        forn(i,l-1)
        {
            D*=MOD;
            D2*=MOD2;
        }

        forn(i,l)
        {
            if(b[s[i]-'a']==0)
                pl++;
            H=H*MOD+s[i];
            H2=H2*MOD2+s[i];
        }

        if(pl<=k)
            a[L++]=mp(H,H2);

        for(int i=0; i<s.size()-l; i++)
        {
            H-=D*s[i];
            H2-=D2*s[i];

            if(b[s[i]-'a']==0)
                pl--;

            H=H*MOD+s[i+l];
            H2=H2*MOD2+s[i+l];

            if(b[s[i+l]-'a']==0)
                pl++;
            if(pl<=k)
                a[L++]=mp(H,H2);
        }
    }

    if(L==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+L);
    forn(i,L-1)
    {
        if(a[i]!=a[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n,pl,p;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;
int ans=0;
int pos=0;
unsigned int H,H2;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            pl=0;
            H=0;
            H2=0;
            for(p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
                H2=H2+s[p];
            }
            pos=(H*H2)%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;

// Constants
const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

// Types
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < i64, i64 > PII;

string s;
char c;
bool b[26];
int k;

i64 H,x=173,step;
i64 H2,x2=73,step2;
int pl;
set <PII> st;
int ans;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        H=0; H2=0;
        pl=0;
        st.clear();

        forn(i,l)
        {
            H=H*x+s[i];
            H2=H2*x2+s[i];
            if(b[s[i]-'a']==0)
                pl++;
        }
        step=1;
        step2=1;
        forn(i,l-1)
        {
            step*=x;
            step2*=x2;
        }
        if(pl<=k)
            st.insert(mp(H,H2));
        for(int i=l; i<s.size(); i++)
        {
            H-=s[i-l]*step;
            H=H*x+s[i];
            H2-=s[i-l]*step2;
            H2=H2*x2+s[i];
            if(b[s[i-l]-'a']==0)
                pl--;
            if(b[s[i]-'a']==0)
                pl++;
            if(pl<=k)
                st.insert(mp(H,H2));
        }
        ans+=st.size();
    }
    cout <<ans;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned i64 H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;

// Constants
const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

// Types
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < i64, i64 > PII;

string s;
char c;
bool b[26];
int k;

i64 H,x=173,step;
//i64 H2,x2=73,step2;
int pl;
set <i64> st;
int ans;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        H=0; //H2=0;
        pl=0;
        st.clear();

        forn(i,l)
        {
            H=H*x+s[i];
        //  H2=H2*x2+s[i];
            if(b[s[i]-'a']==0)
                pl++;
        }
        step=1;
        //step2=1;
        forn(i,l-1)
        {
            step*=x;
            //step2*=x2;
        }
        if(pl<=k)
            st.insert(H);
        for(int i=l; i<s.size(); i++)
        {
            H-=s[i-l]*step;
            H=H*x+s[i];
            //H2-=s[i-l]*step2;
            //H2=H2*x2+s[i];
            if(b[s[i-l]-'a']==0)
                pl--;
            if(b[s[i]-'a']==0)
                pl++;
            if(pl<=k)
                st.insert(H);
        }
        ans+=st.size();
    }
    cout <<ans;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=100000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
set <int> st;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*111+s[p];
            }
            while(H<0)
                H*=-1;
            H%=MOD;
            if(pl<=k)
                bb[H]=1;
        //  if(pl<=k)
        //      st.insert(H);
        }
        forn(i,MOD)
        {
            if(bb[i])
                ans++;
            bb[i]=0;
        }
        //ans+=st.size();
        //st.clear();
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*6666+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=2000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*37+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                bb[pos]=1;
                pp[lpp++]=pos;
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
//int a[1000000][2],lpp=0;
PII a[2000000];
int lpp;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*137+s[p];
                H2=H2*13+s[p];
            }

            if(pl<=k)
                a[lpp++]=mp(H,H2);

        }
    }

    if(lpp==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+lpp);
    forn(i,lpp-1)
        if(a[i]!=a[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
vector <pair<ui,ui> > v;
ui H,H2;

int main()
{
    freopen("z.in", "rt", stdin);
    freopen("z.out", "wt", stdout);

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            pl=0;
            H=0;
            H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*173+s[p];
                H2=H2*37+s[p];
            }
            if(pl<=k)
                v.pb(mp(H,H2));

        }
    }

    if(v.size()==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(v.begin(),v.end());
    forn(i,v.size()-1)
    {
        if(v[i]!=v[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;

// Constants
const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

// Types
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

string s;
char c;
bool b[26];
int k;

int H,x=173,step;
int H2,x2=73,step2;
int pl;
set <PII> st;
int ans;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        H=0; H2=0;
        pl=0;
        st.clear();

        forn(i,l)
        {
            H=H*x+s[i];
            H2=H2*x2+s[i];
            if(b[s[i]-'a']==0)
                pl++;
        }
        step=1;
        step2=1;
        forn(i,l-1)
        {
            step*=x;
            step2*=x2;
        }
        if(pl<=k)
            st.insert(mp(H,H2));
        for(int i=l; i<s.size(); i++)
        {
            H-=s[i-l]*step;
            H=H*x+s[i];
            H2-=s[i-l]*step2;
            H2=H2*x2+s[i];
            if(b[s[i-l]-'a']==0)
                pl--;
            if(b[s[i]-'a']==0)
                pl++;
            if(pl<=k)
                st.insert(mp(H,H2));
        }
        ans+=st.size();
    }
    cout <<ans;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=200000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*37+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=400000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*37+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
ui a[100000000];
int L=0;
ui H;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            pl=0;
            H=0;
            //H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*173+s[p];
            //    H2=H2*37+s[p];
            }
            if(pl<=k)
                a[L++]=H;
                //v.pb(mp(H,H2));

        }
    }

    if(L==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+L);
    forn(i,L-1)
    {
        if(a[i]!=a[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=100000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
set <int> st;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*73+s[p];
            }
            while(H<0)
                H*=-1;
            H%=MOD;
            if(pl<=k)
                bb[H]=1;
        //  if(pl<=k)
        //      st.insert(H);
        }
        forn(i,MOD)
        {
            if(bb[i])
                ans++;
            bb[i]=0;
        }
        //ans+=st.size();
        //st.clear();
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n,pl,p;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;
int ans=0;
int pos=0;
unsigned int H,H2;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            pl=0;
            H=0;
            H2=0;
            for(p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
                //H2=H2+s[p];
            }
            pos=(H)%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
//int a[1000000][2],lpp=0;
pair <PII, int> a[2000000];
int lpp;
int H=0,H2=0,H3=0;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            pl=0;
            int H=0,H2=0,H3=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*137+s[p];
                H2=H2+s[p]+s[p];
                H3+=s[p];
            }

            if(pl<=k)
                a[lpp++]=mp(mp(H,H2),H3);

        }
    }

    if(lpp==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+lpp);
    forn(i,lpp-1)
        if(a[i]!=a[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=100000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            unsigned int H=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*37+s[p];
            }
            pos=H%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                bb[pos]=1;
                pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
//int a[1000000][2],lpp=0;
PII a[2000000];
int lpp;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*137+s[p];
                H2=H2*s[p];
            }

            if(pl<=k)
                a[lpp++]=mp(H,H2);

        }
    }

    if(lpp==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+lpp);
    forn(i,lpp-1)
        if(a[i]!=a[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
vector <pair<ui,ui> > v;
ui H,H2;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            pl=0;
            H=0;
            H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*173+s[p];
                H2=H2*37+s[p];
            }
            if(pl<=k)
                v.pb(mp(H,H2));

        }
    }

    if(v.size()==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(v.begin(),v.end());
    forn(i,v.size()-1)
    {
        if(v[i]!=v[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
//int a[1000000][2],lpp=0;
PII a[2000000];
int lpp;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*137+s[p];
                H2+=s[p];

            }

            if(pl<=k)
            {
                a[lpp++]=mp(H,H2);
                //a[lpp][0]=H;
                //a[lpp++][1]=H2;
            }

        }
    }

    if(lpp==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+lpp);
    forn(i,lpp-1)
        //if((a[i][0]!=v[i+1][0])&&(a[i][1]!=v[i+1][1]))
        if(a[i]!=a[i+1])
            ans++;
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <hash_set>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

const int MOD=500000000;
string s;
char c;
int k,n;
bool b[30];
bool bb[MOD];
int pp[2000],lpp=0;

int main()
{
/*  freopen("z.in","wt",stdout);
    forn(i,1500)
        cout <<char('a'+rand()%26);
    cout <<endl;
    forn(i,26)
        cout <<'1';
    cout <<endl;
    cout <<1000;
    */

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;
    n=s.size();

    int ans=0;
    int pos=0;
    unsigned int H=0,H2=0;

    for(int l=1; l<=n; l++)
    {
        for(int i=0; i<n-l+1; i++)
        {
            int pl=0;
            H=0;
            H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*666+s[p];
                H2=H2*73+s[p];
            }
            pos=(H+H2)%MOD;

            if(pl<=k)
            {
                if(bb[pos]==0)
                {
                    bb[pos]=1;
                    pp[lpp++]=pos;
                }
            }
        }
        forn(i,lpp)
        {
            ans++;
            bb[pp[i]]=0;
        }
        lpp=0;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;
typedef unsigned int ui;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n,pl;
bool b[30];
pair<i64,int> a[2000000];
i64 H,D,MOD=73;
int H2,D2,MOD2=173;
int L=0;

int main()
{
    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        D=1;
        H=0;
        D2=1;
        H2=0;

        pl=0;
        forn(i,l-1)
        {
            D*=MOD;
            D2*=MOD2;
        }

        forn(i,l)
        {
            if(b[s[i]-'a']==0)
                pl++;
            H=H*MOD+s[i];
            H2=H2*MOD2+s[i];
        }

        if(pl<=k)
            a[L++]=mp(H,H2);

        for(int i=0; i<s.size()-l; i++)
        {
            H-=D*s[i];
            H2-=D2*s[i];

            if(b[s[i]-'a']==0)
                pl--;

            H=H*MOD+s[i+l];
            H2=H2*MOD2+s[i+l];

            if(b[s[i+l]-'a']==0)
                pl++;
            if(pl<=k)
                a[L++]=mp(H,H2);
        }
    }

    if(L==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+L);
    forn(i,L-1)
    {
        if(a[i]!=a[i+1])
            ans++;
    }
    cout <<ans;
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define sz size()
#define x first
#define y second
#define forn(i, n) for(int i=0; i<(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)

typedef long double ld;
typedef signed   long long i64;
typedef unsigned long long u64;
typedef pair < int, int > PII;

const ld PI = 3.1415926535897932384626433832795;
const ld EPS = 1e-11;

string s;
char c;
int k,n;
bool b[30];
//int a[1000000][2],lpp=0;
PII a[2000000];
int lpp;

int main()
{

    cin >>s;
    forn(i,26)
    {
        cin >>c;
        if(c=='1')
            b[i]=1;
    }
    cin >>k;

    for(int l=1; l<=s.size(); l++)
    {
        for(int i=0; i<s.size()-l+1; i++)
        {
            int pl=0;
            int H=0,H2=0;
            for(int p=i; p<i+l; p++)
            {
                if(b[s[p]-'a']==0)
                    pl++;
                H=H*137+s[p];
                H2=H2*37+s[p];

            }

            if(pl<=k)
            {
                a[lpp++]=mp(H,H2);
                //a[lpp][0]=H;
                //a[lpp++][1]=H2;
            }

        }
    }

    if(lpp==0)
    {
        cout <<0;
        return 0;
    }

    int ans=1;
    sort(a,a+lpp);
    forn(i,lpp-1)
        //if((a[i][0]!=v[i+1][0])&&(a[i][1]!=v[i+1][1]))
        if(a[i]!=a[i+1])
            ans++;
    cout <<ans;
    return 0;
}