#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
char s[maxn], c[maxn];
struct sam {
  int len, fa, val;
  int go[26];
  sam(int len = 0, int val = 0) : len(len), val(val) {
    memset(go, 0, sizeof(go));
  }
} node[maxn];
int root, tot, last, cc[maxn], p[maxn];
void init() {
  root = tot = last = 0;
  node[root] = sam(0, 0);
  node[root].fa = -1;
}
void extend(int c, int v) {
  int p = last, np = ++tot;
  last = np;
  node[np] = sam(node[p].len + 1, v);
  while (~p && !node[p].go[c]) {
    node[p].go[c] = np;
    p = node[p].fa;
  }
  if (p == -1) {
    node[np].fa = root;
    return;
  }
  int q = node[p].go[c];
  if (node[q].len == node[p].len + 1) {
    node[np].fa = q;
    return;
  }
  int nq = ++tot;
  node[nq] = node[q];
  node[nq].len = node[p].len + 1;
  node[nq].val = 0;
  node[q].fa = node[np].fa = nq;
  while (~p && node[p].go[c] == q) {
    node[p].go[c] = nq;
    p = node[p].fa;
  }
}
void build(char *s, char *c) {
  init();
  for (int i = 0; s[i]; ++i) extend(s[i] - 'a', 1 ^ (c[i] - '0'));
}
int main() {
  int n;
  scanf("%d%s%s", &n, s, c);
  build(s, c);
  for (int i = 0; i <= tot; ++i) ++cc[node[i].len];
  for (int i = 1; i <= tot; ++i) cc[i] += cc[i - 1];
  for (int i = tot; i >= 0; --i) p[--cc[node[i].len]] = i;
  long long ans = 0;
  for (int i = tot; i > 0; --i) {
    ans = max(ans, (long long)node[p[i]].val * node[p[i]].len);
    node[node[p[i]].fa].val += node[p[i]].val;
  }
  printf("%I64d\n", ans);
  return 0;
}
