#include <bits/stdc++.h>
using namespace std;
char s[200005];
long long Ans;
int n, cnt;
int head[400005], to[400005], Next[400005];
struct SAM {
  int tot, lst;
  struct Trans {
    int Next[26];
    int len, link, size;
  } s[400005];
  void Initialize() {
    s[0].link = -1;
    s[0].len = tot = lst = 0;
  }
  void Insert(char c, int val) {
    int p = lst, Now = ++tot;
    s[Now].size = val;
    s[Now].len = s[p].len + 1;
    while (p != -1 && !s[p].Next[c - 'a']) {
      s[p].Next[c - 'a'] = Now;
      p = s[p].link;
    }
    if (p == -1)
      s[Now].link = 0;
    else {
      int q = s[p].Next[c - 'a'];
      if (s[q].len == s[p].len + 1)
        s[Now].link = q;
      else {
        int New = ++tot;
        s[New] = s[q];
        s[New].size = 0;
        s[New].len = s[p].len + 1;
        while (p != -1 && s[p].Next[c - 'a'] == q) {
          s[p].Next[c - 'a'] = New;
          p = s[p].link;
        }
        s[Now].link = s[q].link = New;
      }
    }
    lst = Now;
    return;
  }
} S;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void add(int u, int v) {
  to[++cnt] = v;
  Next[cnt] = head[u];
  head[u] = cnt;
}
void dfs(int u) {
  for (register int i = head[u]; i; i = Next[i]) {
    int v = to[i];
    dfs(v);
    S.s[u].size += S.s[v].size;
  }
  Ans = max(Ans, 1LL * S.s[u].len * S.s[u].size);
  return;
}
int main() {
  n = read();
  scanf("%s", s + 1);
  S.Initialize();
  for (register int i = 1; i <= n; ++i) {
    char op = getchar();
    while (op != '0' && op != '1') op = getchar();
    S.Insert(s[i], op - '0' ^ 1);
  }
  for (register int i = 1; i <= S.tot; ++i) add(S.s[i].link, i);
  dfs(0);
  printf("%lld\n", Ans);
  return 0;
}
