#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 100;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const long long linf = 2e18;
const long long mod = 1e9 + 7;
const double eps = 1e-7;
template <class T = int>
T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}
long long n;
char s[maxn], a[maxn];
struct SAM {
  int tr[maxn][26], len[maxn], fail[maxn], cnt[maxn], last, tot;
  void init() {
    memset(tr, 0, sizeof(tr)), memset(cnt, 0, sizeof(cnt));
    len[0] = 0;
    last = tot = 1;
  }
  void insert(int c, int add) {
    len[++tot] = len[last] + 1;
    int p = last;
    int u = last = tot;
    cnt[tot] = add;
    for (; p && !tr[p][c]; p = fail[p]) tr[p][c] = u;
    if (!p) return (void)(fail[u] = 1);
    int q = tr[p][c];
    if (len[q] == len[p] + 1) return (void)(fail[u] = q);
    len[++tot] = len[p] + 1;
    fail[tot] = fail[q], fail[q] = fail[u] = tot;
    memcpy(tr[tot], tr[q], sizeof(tr[tot]));
    for (; p && tr[p][c] == q; p = fail[p]) tr[p][c] = tot;
  }
  int a[maxn], c[maxn];
  long long ans = 0;
  void rsort() {
    for (register int i = (1); i <= (tot); ++i) c[i] = 0;
    for (register int i = (1); i <= (tot); ++i) c[len[i]]++;
    for (register int i = (1); i <= (tot); ++i) c[i] += c[i - 1];
    for (register int i = (1); i <= (tot); ++i) a[c[len[i]]--] = i;
    for (register int i = (tot); i >= (1); --i)
      cnt[fail[a[i]]] += cnt[a[i]], ans = max(ans, 1ll * cnt[a[i]] * len[a[i]]);
    printf("%lld\n", ans);
  }
} sam;
signed main() {
  scanf("%lld", &n);
  scanf("%s %s", s + 1, a + 1);
  sam.init();
  for (register int i = (1); i <= (n); ++i)
    sam.insert(s[i] - 'a', !(a[i] - '0'));
  sam.rsort();
  return 0;
}
