#include <bits/stdc++.h>
using namespace std;
long long n, sa[200010], hei[200010], rk[200010], tmp[200010], s[200010],
    l[200010], r[200010], stk[200010], f[200010], ofs;
char str[200010], fb[200010];
void build_sa(char *str, long long sa[]);
void build_hei(char *str, long long sa[], long long hei[]);
int main() {
  scanf("%I64d%s%s", &n, str, fb);
  reverse(str, str + n);
  reverse(fb, fb + n);
  build_sa(str, sa);
  build_hei(str, sa, hei);
  long long i, j;
  for (j = 0, i = 1; i <= n - 1; i++) {
    while (j && hei[stk[j]] >= hei[i]) j--;
    l[i] = (j ? stk[j] + 1 : 1);
    stk[++j] = i;
  }
  for (j = 0, i = n - 1; i >= 1; i--) {
    while (j && hei[stk[j]] >= hei[i]) j--;
    r[i] = (j ? stk[j] - 1 : n - 1);
    stk[++j] = i;
  }
  for (s[0] = 0, i = 1; i <= n; i++)
    s[i] = s[i - 1] + (fb[sa[i] - 1] == '0' ? 1 : 0);
  memset(f, 0, sizeof(f));
  for (i = 1; i <= n - 1; i++)
    f[hei[i]] = max(f[hei[i]], s[r[i] + 1] - s[l[i] - 1]);
  long long ans = 0;
  for (i = 1; i <= n; i++) ans = max(ans, i * f[i]);
  for (i = 1; i <= n; i++)
    if (fb[i - 1] != '1') ans = max(ans, n - i + 1);
  printf("%I64d\n", ans);
  return 0;
}
bool cmp(long long i, long long j) {
  if (rk[i] != rk[j]) return rk[i] < rk[j];
  long long ri = (i + ofs <= n ? rk[i + ofs] : -1),
            rj = (j + ofs <= n ? rk[j + ofs] : -1);
  return ri < rj;
}
void build_sa(char *str, long long sa[]) {
  long long i;
  for (i = 1; i <= n; i++) {
    sa[i] = i;
    rk[i] = str[i - 1];
  }
  for (ofs = 1; ofs <= n; ofs *= 2) {
    sort(sa + 1, sa + n + 1, cmp);
    for (tmp[sa[1]] = 1, i = 2; i <= n; i++)
      tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0);
    for (i = 1; i <= n; i++) rk[i] = tmp[i];
  }
}
void build_hei(char *str, long long sa[], long long hei[]) {
  long long h = 0, i, j;
  for (i = 1; i <= n; i++) {
    if (rk[i] == n) continue;
    j = sa[rk[i] + 1];
    if (h) h--;
    for (; j + h <= n && i + h <= n; h++)
      if (str[j + h - 1] != str[i + h - 1]) break;
    hei[rk[i]] = h;
  }
}
