#include <bits/stdc++.h>
using namespace std;
char s[200005], t[200005];
int f[200005], siz[200005], p[200005], q[200005], ht[200005], sm[200005],
    rk[200005 * 2], sa[200005];
struct node {
  int w, id;
} a[200005];
int find(int);
bool cmp(node a, node b) { return a.w > b.w; }
int main() {
  long long ans = 0;
  int i, k, n, cnt, u, v;
  scanf("%d %s %s", &n, s + 1, t + 1);
  reverse(s + 1, s + n + 1);
  reverse(t + 1, t + n + 1);
  for (i = 1; i <= n; ++i) ++sm[s[i] - 'a'];
  for (i = 1; i < 26; ++i) sm[i] += sm[i - 1];
  for (i = 1; i <= n; ++i) p[i] = sm[s[i] - 'a']--;
  for (i = 1; i <= n; ++i) q[p[i]] = i;
  for (i = 1, cnt = 0; i <= n; ++i) {
    if (s[q[i]] != s[q[i - 1]]) ++cnt;
    rk[q[i]] = cnt;
  }
  for (k = 1; k < n; k <<= 1) {
    memset(sm, 0, sizeof(sm));
    for (i = 1; i <= n; ++i) ++sm[rk[i + k]];
    for (i = 1; i <= n; ++i) sm[i] += sm[i - 1];
    for (i = n; i; --i) p[i] = sm[rk[i + k]]--;
    for (i = 1; i <= n; ++i) q[p[i]] = i;
    memset(sm, 0, sizeof(sm));
    for (i = 1; i <= n; ++i) ++sm[rk[i]];
    for (i = 1; i <= n; ++i) sm[i] += sm[i - 1];
    for (i = n; i; --i) p[q[i]] = sm[rk[q[i]]]--;
    for (i = 1; i <= n; ++i) q[p[i]] = i;
    for (i = 1, cnt = 0; i <= n; ++i) {
      cnt += (rk[q[i]] != rk[q[i - 1]] || rk[q[i] + k] != rk[q[i - 1] + k]);
      p[q[i]] = cnt;
    }
    memcpy(rk, p, sizeof(p));
  }
  for (i = 1; i <= n; ++i) sa[rk[i]] = i;
  for (i = 1, k = 0; i <= n; ++i) {
    k -= (k != 0);
    while (s[sa[rk[i] - 1] + k] == s[i + k]) ++k;
    ht[rk[i]] = k;
  }
  for (i = 1; i <= n; ++i) {
    siz[rk[i]] = t[i] == '0';
    a[a[i].id = f[i] = i].w = ht[i];
    if (t[i] == '0' && !ans) ans = n - i + 1;
  }
  sort(a + 1, a + n + 1, cmp);
  for (i = 1; i <= n; ++i) {
    u = find(a[i].id);
    v = find(a[i].id - 1);
    ans = max(ans, 1ll * (siz[u] + siz[v]) * a[i].w);
    f[v] = u;
    siz[u] += siz[v];
  }
  printf("%lld\n", ans);
  return 0;
}
int find(int u) {
  int v = u, p;
  while (v != f[v]) v = f[v];
  while (u != f[u]) {
    p = u;
    u = f[u];
    f[p] = v;
  }
  return u;
}
