#include <bits/stdc++.h>
static constexpr int N = 200200;
int n;
int T[N];
int SA[N], rank[N], height[N];
int c[N], h[N];
inline bool leq(int a1, int a2, int b1, int b2) {
  return a1 < b1 || a1 == b1 && a2 <= b2;
}
inline bool leq(int a1, int a2, int a3, int b1, int b2, int b3) {
  return a1 < b1 || a1 == b1 && leq(a2, a3, b2, b3);
}
void radixPass(int* a, int* b, int* r, int n, int K) {
  for (int i = 0; i <= K; i++) c[i] = 0;
  for (int i = 0; i < n; i++) c[r[a[i]]]++;
  for (int i = 0, sum = 0; i <= K; i++) {
    int t = c[i];
    c[i] = sum;
    sum += t;
  }
  for (int i = 0; i < n; i++) b[c[r[a[i]]]++] = a[i];
}
int R0[N];
int SA0[N];
void suffixArray(int* T, int* SA, int n, int K) {
  int n0 = (n + 2) / 3, n1 = (n + 1) / 3, n2 = n / 3, n02 = n0 + n2;
  int* R = new int[n02 + 3];
  R[n02] = R[n02 + 1] = R[n02 + 2] = 0;
  int* SA12 = new int[n02 + 3];
  SA12[n02] = SA12[n02 + 1] = SA12[n02 + 2] = 0;
  for (int i = 0, j = 0; i < n + (n0 - n1); i++)
    if (i % 3 != 0) R[j++] = i;
  radixPass(R, SA12, T + 2, n02, K);
  radixPass(SA12, R, T + 1, n02, K);
  radixPass(R, SA12, T, n02, K);
  int name = 0, c0 = -1, c1 = -1, c2 = -1;
  for (int i = 0; i < n02; i++) {
    if (T[SA12[i] + 0] != c0 || T[SA12[i] + 1] != c1 || T[SA12[i] + 2] != c2) {
      name++;
      c0 = T[SA12[i] + 0];
      c1 = T[SA12[i] + 1];
      c2 = T[SA12[i] + 2];
    }
    if (SA12[i] % 3 == 1) {
      R[SA12[i] / 3] = name;
    } else {
      R[SA12[i] / 3 + n0] = name;
    }
  }
  if (name < n02) {
    suffixArray(R, SA12, n02, name);
    for (int i = 0; i < n02; i++) R[SA12[i]] = i + 1;
  } else {
    for (int i = 0; i < n02; i++) SA12[R[i] - 1] = i;
  }
  for (int i = 0, j = 0; i < n02; i++)
    if (SA12[i] < n0) R0[j++] = 3 * SA12[i];
  radixPass(R0, SA0, T, n0, K);
  for (int p = 0, t = n0 - n1, k = 0; k < n; k++) {
    int i = (SA12[t] < n0 ? SA12[t] * 3 + 1 : (SA12[t] - n0) * 3 + 2);
    int j = SA0[p];
    if (SA12[t] < n0 ? leq(T[i], R[SA12[t] + n0], T[j], R[j / 3])
                     : leq(T[i], T[i + 1], R[SA12[t] - n0 + 1], T[j], T[j + 1],
                           R[j / 3 + n0])) {
      SA[k] = i;
      t++;
      if (t == n02)
        for (k++; p < n0; p++, k++) SA[k] = SA0[p];
    } else {
      SA[k] = j;
      p++;
      if (p == n0)
        for (k++; t < n02; t++, k++)
          SA[k] = (SA12[t] < n0 ? SA12[t] * 3 + 1 : (SA12[t] - n0) * 3 + 2);
    }
  }
  delete[] R;
  delete[] SA12;
}
void make_height(int* data, int* SA, int* rank, int* height, int n) {
  int i;
  for (i = 0; i < n; i++) rank[SA[i]] = i;
  for (i = 0; i < n; i++) {
    if (rank[i] == 0) {
      h[i] = 0;
    } else {
      if (i > 0 && h[i - 1] > 1)
        h[i] = h[i - 1] - 1;
      else
        h[i] = 0;
      while (data[i + h[i]] == data[SA[rank[i] - 1] + h[i]]) h[i]++;
    }
  }
  for (i = 0; i < n; i++) {
    if (rank[i])
      height[rank[i] - 1] = h[i];
    else
      height[n - 1] = 0;
  }
}
void init() {
  T[n] = 255;
  T[n + 1] = T[n + 2] = T[n + 3] = T[n + 4] = 0;
  suffixArray(T, SA, n + 1, 255);
  make_height(T, SA, rank, height, n + 1);
}
int L[N], R[N];
int st[N], top;
long long int ans;
int lef, rig;
char u[N], v[N];
int cnt[N];
void process() {
  for (int i = 0; i < n; i++) {
    cnt[i + 1] = cnt[i] + (v[SA[i]] == '0');
  }
  top = 0;
  for (int i = 0; i < n; i++) {
    while (top > 0 && height[st[top - 1]] >= height[i]) top--;
    if (top == 0)
      L[i] = -1;
    else
      L[i] = st[top - 1];
    st[top++] = i;
  }
  top = 0;
  for (int i = n - 1; i >= 0; i--) {
    while (top > 0 && height[st[top - 1]] >= height[i]) top--;
    if (top == 0)
      R[i] = n;
    else
      R[i] = st[top - 1];
    st[top++] = i;
  }
  long long int cur_sum;
  for (int i = 0; i < n; i++) {
    cur_sum = height[i] * 1LL * (cnt[R[i] + 1] - cnt[L[i] + 1]);
    ans = ::std::max(ans, cur_sum);
  }
}
int main() {
  scanf("%d%s%s", &n, u, v);
  for (int i = 0; i < n; i++) {
    if (v[i] == '0') ans = (i + 1) * 1ll;
  }
  ::std::reverse(u, u + n);
  ::std::reverse(v, v + n);
  for (int i = 0; i < n; i++) {
    T[i] = u[i];
  }
  init();
  process();
  printf("%lld\n", ans);
  return 0;
}
