#include <bits/stdc++.h>
using namespace std;
long long ans;
char st[400005], ban[400005];
int n, m, last, sz[400005], f[400005], len[400005], nxt[400005][26], a[400005];
bool cmp(int x, int y) { return len[x] > len[y]; }
void add(int ch) {
  int i, p, x;
  len[++m] = len[last] + 1;
  for (i = last; i && !nxt[i][ch]; i = f[i]) nxt[i][ch] = m;
  x = i, p = nxt[i][ch], last = m;
  if (!i) {
    f[m] = 1;
    return;
  }
  if (len[p] == len[x] + 1) {
    f[m] = p;
    return;
  }
  len[++m] = len[x] + 1, f[m] = f[p], f[p] = f[m - 1] = m;
  for (i = 0; i < 26; i++) nxt[m][i] = nxt[p][i];
  for (i = x; i && nxt[i][ch] == p; i = f[i]) nxt[i][ch] = m;
}
int main() {
  int i, p = 1;
  scanf("%d\n", &n);
  scanf("%s", st + 1);
  scanf("%s", ban + 1);
  last = m = 1;
  for (i = 1; i <= n; i++) add(st[i] - 'a');
  for (i = 1; i <= m; i++) a[i] = i;
  sort(a + 1, a + m + 1, cmp);
  for (i = 1; i <= n; i++) {
    p = nxt[p][st[i] - 'a'];
    if (ban[i] == '0') sz[p]++;
  }
  for (i = 1; i <= m; i++) sz[f[a[i]]] += sz[a[i]];
  for (i = 1; i <= m; i++) ans = max(ans, 1LL * sz[i] * len[i]);
  cout << ans;
  return 0;
}
