#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int inf = 2e9;
const double pi = acos(-1.0);
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int l[400500], val[400500], fa[400500], head[400500], c[400500][26];
int n, last, tot, idx;
long long ans;
char s[400500], gg[400500];
struct data {
  int obj, pre;
} e[400500 * 2];
void add(int x, int gg) {
  int p = last, np, q, nq;
  np = ++tot;
  l[np] = l[p] + 1;
  val[np] = gg ^ 1;
  while (p && !c[p][x]) c[p][x] = np, p = fa[p];
  if (!p)
    fa[np] = 1;
  else {
    q = c[p][x];
    if (l[q] == l[p] + 1)
      fa[np] = q;
    else {
      nq = ++tot;
      l[nq] = l[p] + 1;
      for (int j = 0; j <= 25; j++) c[nq][j] = c[q][j];
      fa[nq] = fa[q];
      fa[q] = fa[np] = nq;
      for (; c[p][x] == q; p = fa[p]) c[p][x] = nq;
    }
  }
  last = np;
}
void insert(int x, int y) {
  e[++idx].obj = y;
  e[idx].pre = head[x];
  head[x] = idx;
}
void dfs(int u) {
  for (int j = head[u]; j; j = e[j].pre) {
    int v = e[j].obj;
    dfs(v);
    val[u] += val[v];
  }
  ans = max(ans, 1ll * l[u] * val[u]);
}
int main() {
  n = read();
  scanf("%s", s + 1);
  scanf("%s", gg + 1);
  tot = last = 1;
  for (int i = 1; i <= n; i++) add(s[i] - 'a', gg[i] - '0');
  for (int i = 1; i <= tot; i++) insert(fa[i], i);
  dfs(1);
  printf("%lld\n", ans);
  return 0;
}
