#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x7f7f7f7f7f7f7f7f;
template <typename _T>
void read(_T &x) {
  _T f = 1;
  x = 0;
  char s = getchar();
  while ('0' > s || '9' < s) {
    if (s == '-') f = -1;
    s = getchar();
  }
  while ('0' <= s && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    s = getchar();
  }
  x *= f;
}
char str[200005];
long long n, x[200005], y[200005], c[200005], z[200005], m, sta[200005], stak,
    minn;
long long ans;
long long sa[200005], rk[200005], hei[200005], ban[200005], L[200005],
    R[200005], hh[200005], cur;
void getSa() {
  for (long long i = 1; i <= n; i++) c[x[i] = str[i]]++;
  for (long long i = 2; i <= m; i++) c[i] += c[i - 1];
  for (long long i = n; i > 0; i--) sa[c[x[i]]--] = i;
  for (long long k = 1; k <= n; k <<= 1) {
    long long num = 0;
    for (long long i = n - k + 1; i <= n; i++) y[++num] = i;
    for (long long i = 1; i <= n; i++)
      if (sa[i] > k) y[++num] = sa[i] - k;
    for (long long i = 1; i <= m; i++) c[i] = 0;
    for (long long i = 1; i <= n; i++) c[z[i] = x[i]]++;
    for (long long i = 2; i <= m; i++) c[i] += c[i - 1];
    for (long long i = n; i > 0; i--) sa[c[x[y[i]]]--] = y[i];
    for (long long i = 1; i <= n; i++) x[i] = y[i] = 0;
    x[sa[1]] = num = 1;
    for (long long i = 2; i <= n; i++)
      x[sa[i]] = (z[sa[i]] == z[sa[i - 1]] && z[sa[i] + k] == z[sa[i - 1] + k])
                     ? num
                     : ++num;
    m = num;
    if (num == n) break;
  }
}
void getHi() {
  long long k = 0;
  for (long long i = 1; i <= n; i++) rk[sa[i]] = i;
  for (long long i = 1; i <= n; i++) {
    if (k) k--;
    long long j = sa[rk[i] - 1];
    while (i + k <= n && j + k <= n && str[i + k] == str[j + k]) k++;
    hei[rk[i]] = k;
  }
}
signed main() {
  read(n);
  scanf("\n%s", str + 1);
  m = 128;
  getSa();
  getHi();
  for (long long i = n; i > 0; i--) scanf("%1lld", &ban[i]);
  ban[0] = 1;
  for (long long i = 1; i <= n; i++) {
    if (!ban[sa[i]]) ans = max(ans, 1ll * (n - sa[i] + 1));
    if (ban[sa[i]])
      minn = min(minn, hei[i]);
    else if (!ban[sa[i]] && ban[sa[i - 1]])
      hh[++cur] = min(minn, hei[i]), minn = INF;
    else
      hh[++cur] = hei[i];
  }
  for (long long i = 1; i <= cur; i++) {
    while (stak && hh[sta[stak]] >= hh[i]) R[sta[stak--]] = i;
    L[i] = stak ? sta[stak] : 1;
    sta[++stak] = i;
  }
  while (stak) R[sta[stak--]] = cur + 1;
  for (long long i = 1; i <= cur; i++)
    ans = max(ans, 1ll * hh[i] * (R[i] - L[i]));
  printf("%lld\n", ans);
  return 0;
}
