#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int num = 0, f = 1;
  for (; !isdigit(c); c = getchar()) f = c == '-' ? -1 : f;
  for (; isdigit(c); c = getchar()) num = num * 10 + c - '0';
  return num * f;
}
const int N = 4e5 + 5;
int n;
char s[N >> 1], t[N >> 1];
int ch[N][26], len[N], fa[N];
int siz[N];
int tot = 1, lst = 1;
void insert(int x, bool flag) {
  int p = lst;
  lst = ++tot;
  len[lst] = len[p] + 1, siz[lst] = flag;
  for (; p && !ch[p][x]; p = fa[p]) ch[p][x] = lst;
  if (!p)
    fa[lst] = 1;
  else {
    int q = ch[p][x];
    if (len[p] + 1 == len[q])
      fa[lst] = q;
    else {
      int nq = ++tot;
      len[nq] = len[p] + 1, fa[nq] = fa[q];
      fa[lst] = fa[q] = nq;
      memmove(ch[nq], ch[q], sizeof(ch[q]));
      for (; ch[p][x] == q; p = fa[p]) ch[p][x] = nq;
    }
  }
}
int cnt[N], sa[N];
int main() {
  n = read();
  scanf("%s%s", s + 1, t + 1);
  for (int i = 1; i <= n; ++i) insert(s[i] - 'a', (t[i] != '1'));
  for (int i = 1; i <= tot; ++i) ++cnt[len[i]];
  for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1];
  for (int i = tot; i; --i) sa[cnt[len[i]]--] = i;
  for (int i = tot; i; --i) siz[fa[sa[i]]] += siz[sa[i]];
  long long ans = 0;
  for (int i = 1; i <= tot; ++i) {
    ans = max(ans, 1ll * len[i] * siz[i]);
  }
  cout << ans;
  return 0;
}
