#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int INF = 2147483647;
struct node {
  node *ch[26], *f;
  int len;
} * root, *last, q[400005];
int cur = 0;
node *newnode(int x) {
  for (int i = 0; i < 26; i++) q[cur].ch[i] = NULL;
  q[cur].f = NULL;
  q[cur].len = x;
  return &q[cur++];
}
void add(char c) {
  node *p = newnode(last->len + 1), *now = last;
  while (now && now->ch[c] == NULL) now->ch[c] = p, now = now->f;
  if (!now)
    p->f = root;
  else {
    node *q = now->ch[c];
    if (q->len == now->len + 1)
      p->f = q;
    else {
      node *nq = newnode(now->len + 1);
      for (int i = 0; i < 26; i++) nq->ch[i] = q->ch[i];
      while (now && now->ch[c] == q) now->ch[c] = nq, now = now->f;
      nq->f = q->f, q->f = nq, p->f = nq;
    }
  }
  last = p;
}
char s[200005], t[200005];
bool good[400005];
vector<int> G[400005];
int sz[400005];
long long ans = 0;
void dfs(int u) {
  for (int v : G[u]) {
    dfs(v);
    sz[u] += sz[v];
  }
  if (good[u]) sz[u]++;
  ans = max(ans, 1ll * q[u].len * sz[u]);
}
int main() {
  int n;
  scanf("%d%s%s", &n, s, t);
  root = last = newnode(0);
  for (int i = 0; i < n; i++) add(s[i] - 'a');
  node *now = root;
  for (int i = 0; i < n; i++) {
    now = now->ch[s[i] - 'a'];
    if (t[i] == '0') good[now - q] = 1;
  }
  for (int i = 1; i < cur; i++) {
    G[q[i].f - q].emplace_back(i);
  }
  dfs(0);
  printf("%lld\n", ans);
  return 0;
}
