#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
inline void Put(long long x) { printf("%lld\n", x); }
const long long Maxn = 500000 * 2 + 1;
long long N;
char str[Maxn];
char fob[Maxn];
struct ss {
  long long len[Maxn];
  long long link[Maxn];
  long long ar[Maxn];
  long long c[Maxn];
  long long num[Maxn];
  long long head[Maxn];
  long long last;
  long long siz;
  long long ans;
  map<long long, long long> next[Maxn];
  void init() {
    len[0] = 0;
    link[0] = -1;
    siz = 0;
    last = 0;
    ans = -INT_MAX;
  }
  void extend(long long c, long long opt) {
    long long now = ++siz;
    len[now] = len[last] + 1;
    long long pre = last;
    num[now] = opt;
    while (pre != -1 && !next[pre][c]) {
      next[pre][c] = now;
      pre = link[pre];
    }
    if (pre == -1)
      link[now] = 0;
    else {
      long long s = next[pre][c];
      if (len[pre] + 1 == len[s])
        link[now] = s;
      else {
        long long clone = ++siz;
        len[clone] = len[pre] + 1;
        link[clone] = link[s];
        next[clone] = next[s];
        link[s] = clone;
        link[now] = clone;
        while (pre != -1 && next[pre][c] == s) {
          next[pre][c] = clone;
          pre = link[pre];
        }
      }
    }
    last = now;
  }
  void Sort() {
    for (long long i = 1; i <= siz; i++) c[len[i]]++;
    for (long long i = 1; i <= siz; i++) c[i] += c[i - 1];
    for (long long i = 1; i <= siz; i++) ar[c[len[i]]--] = i;
    for (long long i = siz; i >= 1; i--) {
      num[link[ar[i]]] += num[ar[i]];
    }
    for (long long i = 1; i <= siz; i++) {
      ans = max(ans, num[i] * len[i]);
    }
  }
} SAM;
signed main() {
  N = read();
  char ch;
  scanf("%s", str);
  scanf("%s", fob);
  SAM.init();
  for (long long i = 0; i < N; i++) {
    SAM.extend(str[i] - 'a', (fob[i] - '0') == 0);
  }
  SAM.Sort();
  Put(SAM.ans);
  return 0;
}
