#include <bits/stdc++.h>
using namespace std;
namespace IO {
long long len = 0;
char ibuf[(1 << 20) + 1], *iS, *iT, out[(1 << 23) + 1];
inline long long read() {
  register char ch =
      (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin),
       (iS == iT ? EOF : *iS++) : *iS++);
  register long long x = 0;
  register char t = 0;
  while (ch < '0' || ch > '9')
    t |= ch == '-', ch = (iS == iT ? iT = (iS = ibuf) +
                                          fread(ibuf, 1, (1 << 20) + 1, stdin),
                          (iS == iT ? EOF : *iS++) : *iS++);
  while (ch >= '0' && ch <= '9')
    x = x * 10 + (ch ^ 48),
    ch = (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin),
          (iS == iT ? EOF : *iS++) : *iS++);
  return t ? -x : x;
}
inline void putc(char ch) { out[len++] = ch; }
template <class T>
inline void write(T x) {
  if (x < 0) putc('-'), x = -x;
  if (x > 9) write(x / 10);
  out[len++] = x % 10 + 48;
}
inline void readstr(char *m, char l = 'a', char r = 'z') {
  register char ch =
      (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin),
       (iS == iT ? EOF : *iS++) : *iS++);
  register long long x = 0;
  while (ch < l || ch > r)
    ch = (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin),
          (iS == iT ? EOF : *iS++) : *iS++);
  while (ch >= l && ch <= r)
    m[++x] = ch,
    ch = (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin),
          (iS == iT ? EOF : *iS++) : *iS++);
}
inline void flush() {
  fwrite(out, 1, len, stdout);
  len = 0;
}
}  // namespace IO
using IO::flush;
using IO::putc;
using IO::read;
using IO::readstr;
using IO::write;
const long long N = 4e5 + 10;
struct SAM {
  struct statu {
    long long len, link;
    long long ch[26];
  } a[N];
  long long c[N], d[N], sz[N];
  long long siz, last;
  inline void init() {
    a[0].len = 0;
    a[0].link = -1;
    last = 0;
  }
  inline void insert(long long c, long long v) {
    long long cur = ++siz;
    a[cur].len = a[last].len + 1;
    long long p = last;
    while (p != -1 && !a[p].ch[c]) {
      a[p].ch[c] = cur;
      p = a[p].link;
    }
    if (~p) {
      long long q = a[p].ch[c];
      if (a[p].len + 1 == a[q].len) {
        a[cur].link = q;
      } else {
        long long clone = ++siz;
        a[clone] = a[q];
        a[clone].len = a[p].len + 1;
        while (p != -1 && a[p].ch[c] == q) {
          a[p].ch[c] = clone;
          p = a[p].link;
        }
        a[q].link = a[cur].link = clone;
      }
    } else {
      a[cur].link = 0;
    }
    sz[cur] = v ^ 1;
    last = cur;
  }
  inline void insert() {
    static char str[N], st[N];
    long long len = read();
    readstr(str);
    readstr(st, '0', '1');
    for (long long i = 1; i <= len; i++) {
      insert(str[i] - 'a', st[i] - '0');
    }
  }
  inline long long calc() {
    long long ans = 0;
    for (long long i = 1; i <= siz; i++) {
      c[a[i].len]++;
    }
    for (long long i = 1; i <= siz; i++) {
      c[i] += c[i - 1];
    }
    for (long long i = 1; i <= siz; i++) {
      d[c[a[i].len]--] = i;
    }
    for (long long i = siz; i >= 1; i--) {
      long long p = d[i];
      sz[a[p].link] += sz[p];
    }
    for (long long i = 1; i <= siz; i++) {
      ans = max(ans, 1ll * sz[i] * a[i].len);
    }
    return ans;
  }
} sam;
signed main() {
  sam.init();
  sam.insert();
  write(sam.calc());
  flush();
  return 0;
}
