#include <bits/stdc++.h>
using namespace std;
template <class T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { scanf("%lld", &x); }
void _R(unsigned long long &x) { scanf("%llu", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <class T, class... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <class T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const long long &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) {
  _W(x.first);
  putchar(' ');
  _W(x.second);
}
template <class T>
void _W(const vector<T> &x) {
  for (auto i = x.begin(); i != x.end(); _W(*i++))
    if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int MOD = 1e9 + 7, mod = 998244353;
long long qpow(long long a, long long b) {
  long long res = 1;
  a %= MOD;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
const int MAXN = 4e5 + 10, MAXM = 2e6 + 10;
const int INF = INT_MAX, SINF = 0x3f3f3f3f;
const long long llINF = LLONG_MAX;
const int inv2 = (MOD + 1) / 2;
const int Lim = 1 << 20;
struct SAM {
  int len[MAXN], fail[MAXN], val[MAXN], ch[MAXN][30];
  int sz, last;
  void init() {
    sz = last = len[0] = (fail[0] = -1) + 1;
    memset(ch[0], 0, sizeof(ch[0]));
  }
  void extend(char c, int ttt) {
    int cur = ++sz;
    memset(ch[cur], 0, sizeof(ch[cur]));
    len[cur] = len[last] + 1;
    int p = last;
    while (~p && !ch[p][c - 'a']) {
      ch[p][c - 'a'] = cur;
      p = fail[p];
    }
    if (~p) {
      int q = ch[p][c - 'a'];
      if (len[p] + 1 == len[q])
        fail[cur] = q;
      else {
        int clone = ++sz;
        fail[clone] = fail[q];
        for (int i = 0; i < 26; i++) ch[clone][i] = ch[q][i];
        len[clone] = len[p] + 1;
        val[clone] = 0;
        while (~p && ch[p][c - 'a'] == q) {
          ch[p][c - 'a'] = clone;
          p = fail[p];
        }
        fail[q] = fail[cur] = clone;
      }
    } else
      fail[cur] = 0;
    last = cur;
    val[cur] += ttt;
  }
  int ord[MAXN], cnt[MAXN];
  void _sort() {
    memset(cnt, 0, (sz + 5) * sizeof(int));
    for (int i = 1; i <= sz; i++) cnt[len[i]]++;
    for (int i = 1; i <= sz; i++) cnt[i] += cnt[i - 1];
    for (int i = sz; i >= 1; i--) ord[cnt[len[i]]--] = i;
  }
} G;
char s[MAXN], t[MAXN];
long long ans = 0;
void solve() {
  int n;
  R(n);
  R(s, t);
  G.init();
  for (int i = 0; i < n; i++) G.extend(s[i], 1 ^ (t[i] - '0'));
  G._sort();
  for (int i = G.sz; i; i--) G.val[G.fail[G.ord[i]]] += G.val[G.ord[i]];
  for (int i = 1; i <= G.sz; i++) {
    ans = max(1ll * G.val[i] * G.len[i], ans);
  }
  W(ans);
}
int main() {
  srand(time(0));
  int T = 1;
  while (T--) solve();
  return 0;
}
