#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
char a[N], b[N];
struct node {
  node *ch[26], *pre;
  int len, cnt;
  void init(int L = 0) {
    memset(ch, 0, sizeof(ch));
    pre = 0;
    cnt = 0;
    len = L;
  }
} buff[N << 1], *tmp[N << 1], *p, *np, *q, *nq, *root;
int cnt, n;
node* newnode(int L = 0) {
  buff[++cnt].init(L);
  return buff + cnt;
}
void ins(int x, bool flag) {
  np = newnode(p->len + 1);
  np->cnt = flag;
  for (; p; p = p->pre)
    if (!p->ch[x])
      p->ch[x] = np;
    else
      break;
  if (!p) {
    np->pre = root;
    p = np;
    return;
  }
  q = p->ch[x];
  if (q->len == p->len + 1) {
    np->pre = q;
    p = np;
    return;
  }
  nq = newnode(p->len + 1);
  memcpy(nq->ch, q->ch, sizeof(q->ch));
  nq->pre = q->pre;
  q->pre = np->pre = nq;
  for (; p; p = p->pre)
    if (p->ch[x] == q)
      p->ch[x] = nq;
    else
      break;
  p = np;
}
long long ans;
bool cmp(node* a, node* b) { return a->len < b->len; }
void solve() {
  for (int i = 1; i <= cnt; ++i) tmp[i] = buff + i;
  sort(tmp + 1, tmp + 1 + cnt, cmp);
  for (int i = cnt; i > 1; --i) tmp[i]->pre->cnt += tmp[i]->cnt;
  ans = 0;
  for (int i = cnt; i; --i)
    ans = max(ans, (long long)(tmp[i]->len) * tmp[i]->cnt);
}
int main() {
  p = root = newnode();
  scanf("%d", &n);
  scanf(" %s %s", a, b);
  for (int i = 0; i < n; ++i) ins(a[i] - 'a', b[i] == '0');
  solve();
  printf("%I64d\n", ans);
  return 0;
}
