#include <bits/stdc++.h>
using namespace std;
namespace Elaina {
template <class T>
inline T Max(const T x, const T y) {
  return x < y ? y : x;
}
template <class T>
inline T Min(const T x, const T y) {
  return x < y ? x : y;
}
template <class T>
inline T fab(const T x) {
  return x < 0 ? -x : x;
}
template <class T>
inline void getMax(T& x, const T y) {
  x = Max(x, y);
}
template <class T>
inline void getMin(T& x, const T y) {
  x = Min(x, y);
}
template <class T>
T gcd(const T x, const T y) {
  return y ? gcd(y, x % y) : x;
}
template <class T>
inline T readin(T x) {
  x = 0;
  int f = 0;
  char c;
  while ((c = getchar()) < '0' || '9' < c)
    if (c == '-') f = 1;
  for (x = (c ^ 48); '0' <= (c = getchar()) && c <= '9';
       x = (x << 1) + (x << 3) + (c ^ 48))
    ;
  return f ? -x : x;
}
template <class T>
void fwrit(const T x) {
  if (x < 0) return putchar('-'), fwrit(-x);
  if (x > 9) fwrit(x / 10);
  putchar(x % 10 ^ 48);
}
}  // namespace Elaina
using namespace Elaina;
const long long maxn = 200000;
char s[maxn + 5];
long long f[maxn + 5];
long long n;
inline void init() {
  n = readin(1);
  scanf("%s", s + 1);
  for (long long i = 1, i_end_ = n; i <= i_end_; ++i) scanf("%1d", &f[i]);
  reverse(s + 1, s + n + 1);
  reverse(f + 1, f + n + 1);
}
long long sa[maxn + 5], x[maxn * 2 + 5], y[maxn * 2 + 5], c[maxn + 5], lev;
inline void getsa() {
  lev = 122;
  for (long long i = 1, i_end_ = n; i <= i_end_; ++i) ++c[x[i] = s[i]];
  for (long long i = 2, i_end_ = lev; i <= i_end_; ++i) c[i] += c[i - 1];
  for (long long i = n, i_end_ = 1; i >= i_end_; --i) sa[c[x[i]]--] = i;
  for (long long k = 1; k <= n; k <<= 1) {
    long long sz = 0;
    for (long long i = n - k + 1, i_end_ = n; i <= i_end_; ++i) y[++sz] = i;
    for (long long i = 1, i_end_ = n; i <= i_end_; ++i)
      if (sa[i] > k) y[++sz] = sa[i] - k;
    for (long long i = 1, i_end_ = lev; i <= i_end_; ++i) c[i] = 0;
    for (long long i = 1, i_end_ = n; i <= i_end_; ++i) ++c[x[i]];
    for (long long i = 2, i_end_ = lev; i <= i_end_; ++i) c[i] += c[i - 1];
    for (long long i = n, i_end_ = 1; i >= i_end_; --i)
      sa[c[x[y[i]]]--] = y[i], y[i] = 0;
    swap(x, y);
    x[sa[1]] = 1;
    long long level = 1;
    for (long long i = 2, i_end_ = n; i <= i_end_; ++i)
      x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])
                     ? level
                     : ++level;
    if (level == n) break;
    lev = level;
  }
}
long long heit[maxn + 5], rk[maxn + 5];
inline void getheit() {
  long long h = 0;
  for (long long i = 1, i_end_ = n; i <= i_end_; ++i) rk[sa[i]] = i;
  for (long long i = 1, i_end_ = n; i <= i_end_; ++i) {
    if (rk[i] == 1) continue;
    if (h) --h;
    long long j = sa[rk[i] - 1];
    while (i + h <= n && j + h <= n && s[i + h] == s[j + h]) ++h;
    heit[rk[i]] = h;
  }
}
long long fa[maxn + 5];
long long tot[maxn + 5];
long long find(const long long x) {
  return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline long long merge(long long x, long long y) {
  x = find(x), y = find(y);
  return (x == y) ? (tot[x]) : (fa[x] = y, tot[y] = tot[y] + tot[x]);
}
struct node {
  long long x, v;
  inline long long operator<(const node rhs) const { return v > rhs.v; }
} v[maxn + 5];
inline void getans() {
  long long now = 1, ans = 0;
  for (long long i = 1, i_end_ = n; i <= i_end_; ++i) {
    fa[i] = i;
    tot[i] = f[i] ^ 1;
    if (f[i] == 0) ans = Max(ans, n - i + 1);
    v[i] = node{sa[i], heit[i]};
  }
  sort(v + 1, v + n + 1);
  for (long long i = n, i_end_ = 1; i >= i_end_; --i) {
    while (now <= n && v[now].v >= i) {
      long long pre = sa[rk[v[now].x] - 1];
      long long ret = merge(pre, v[now].x);
      ans = Max(ans, ret * i);
      ++now;
    }
  }
  fwrit(ans), putchar('\n');
}
signed main() {
  init();
  getsa();
  getheit();
  getans();
  return 0;
}
