#include <bits/stdc++.h>
using namespace std;
const int INF = ~(1 << 31);
const int N = 200000;
int n;
char s[N + 1];
char t[N + 1];
int suffix_array[N + 1];
int *eq;
int lcp[N + 1];
vector<pair<int, int> > lcp_;
inline int add(int a, int b, int md) {
  a += b;
  if (a >= md) a -= md;
  return a;
}
inline int sub(int a, int b, int md) {
  a -= b;
  if (a < 0) a += md;
  return a;
}
int cnt[N + 2];
pair<int, int> res[N + 1];
void count_sort(pair<int, int> *a, int n) {
  memset(cnt, 0, 4 * n);
  for (int i = 0; i < n; ++i) ++cnt[a[i].first + 1];
  for (int i = 1; i <= n; ++i) cnt[i] += cnt[i - 1];
  for (int i = 0; i < n; ++i) {
    res[cnt[a[i].first]] = a[i];
    ++cnt[a[i].first];
  }
  for (int i = 0; i < n; ++i) a[i] = res[i];
}
pair<int, int> substr[N + 1];
int eq_c1[N + 1], eq_c2[N + 1];
int *suffix_array_build(char *s, int n, int *suffix_array) {
  for (int i = 0; i < n; ++i) substr[i] = make_pair((int)s[i], i);
  sort(substr, substr + n);
  int *eq = eq_c1;
  int *eqnw = eq_c2;
  eq[substr[0].second] = 0;
  for (int i = 1; i < n; ++i)
    eq[substr[i].second] =
        eq[substr[i - 1].second] + (substr[i].first != substr[i - 1].first);
  for (int i = 1; i < n; i <<= 1) {
    for (int j = 0; j < n; ++j)
      substr[j] = make_pair(eq[sub(substr[j].second, i, n)],
                            sub(substr[j].second, i, n));
    count_sort(substr, n);
    eqnw[substr[0].second] = 0;
    for (int j = 1; j < n; ++j)
      eqnw[substr[j].second] = eqnw[substr[j - 1].second] +
                               (substr[j].first != substr[j - 1].first ||
                                eq[add(substr[j].second, i, n)] !=
                                    eq[add(substr[j - 1].second, i, n)]);
    swap(eq, eqnw);
  }
  for (int i = 0; i < n; ++i) suffix_array[i] = substr[i].second;
  return eq;
}
void lcp_build(char *s, int n, int *suffix_array, int *eq, int *lcp) {
  for (int i = 0; i < n - 1; ++i) {
    while (s[i + lcp[eq[i]]] == s[suffix_array[eq[i] - 1] + lcp[eq[i]]])
      ++lcp[eq[i]];
    lcp[eq[i + 1]] = max(lcp[eq[i]] - 1, 0);
  }
}
void erase_forb() {
  int mn = INF;
  for (int i = 0; i < n; ++i) {
    mn = min(mn, lcp[i]);
    if (t[suffix_array[i]] == '0') {
      lcp_.push_back(make_pair(suffix_array[i], mn));
      mn = INF;
    }
  }
}
long long calc() {
  vector<pair<int, int> > st;
  st.push_back(make_pair(0, 0));
  long long ans = 0;
  for (int i = 0; i < lcp_.size(); ++i) {
    ans = max(ans, (long long)n - lcp_[i].first - 1);
    while (!st.empty() && lcp_[i].second <= st.back().second) st.pop_back();
    ans = max(ans, (long long)(lcp_[i].second * (i - st.back().first + 1)));
    st.push_back(make_pair(i, lcp_[i].second));
  }
  for (int i = 0; i < st.size() - 1; ++i)
    ans = max(ans, (long long)(lcp_.size() - st[i].first) * (st[i + 1].second));
  return ans;
}
int main() {
  scanf("%d%s%s", &n, s, t);
  reverse(s, s + n);
  reverse(t, t + n);
  ++n;
  eq = suffix_array_build(s, n, suffix_array);
  lcp_build(s, n, suffix_array, eq, lcp);
  erase_forb();
  printf("%lld\n", calc());
  return 0;
}
