#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int n, m, k, sa[MAXN], rk[MAXN], height[MAXN];
int x[MAXN], y[MAXN], c[MAXN];
char s[MAXN], t[MAXN];
int pre[MAXN];
void DA(char *s, int *sa, int *rk, int *height) {
  for (int i = 1; i <= n; i++) x[i] = s[i];
  for (int i = 1; i <= m; i++) c[i] = 0;
  for (int i = 1; i <= n; i++) c[x[i]]++;
  for (int i = 1; i <= m; i++) c[i] += c[i - 1];
  for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
  for (int p = 1; p <= n; p <<= 1) {
    int num = 0;
    for (int i = n - p + 1; i <= n; i++) y[++num] = i;
    for (int i = 1; i <= n; i++)
      if (sa[i] > p) y[++num] = sa[i] - p;
    for (int i = 1; i <= m; i++) c[i] = 0;
    for (int i = 1; i <= n; i++) c[x[i]]++;
    for (int i = 1; i <= m; i++) c[i] += c[i - 1];
    for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
    swap(x, y);
    x[sa[1]] = 1, num = 1;
    for (int i = 2; i <= n; i++)
      x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + p] == y[sa[i - 1] + p])
                     ? num
                     : ++num;
    if (num == n) break;
    m = num;
  }
  for (int i = 1; i <= n; i++) rk[sa[i]] = i;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    if (rk[i] == 1) continue;
    if (k) k--;
    int j = sa[rk[i] - 1];
    while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++;
    height[rk[i]] = k;
  }
}
int sta[MAXN], top;
int L[MAXN], R[MAXN];
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  reverse(s + 1, s + n + 1);
  scanf("%s", t + 1);
  reverse(t + 1, t + n + 1);
  m = 122;
  DA(s, sa, rk, height);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (t[i] == '0')
      ans = max(ans, 1LL + n - i);
    else
      pre[rk[i]] = 1;
  }
  for (int i = 1; i <= n; i++) pre[i] += pre[i - 1];
  for (int i = 2; i <= n; i++) {
    while (top && height[sta[top]] >= height[i]) top--;
    L[i] = top ? sta[top] + 1 : 2;
    sta[++top] = i;
  }
  top = 0;
  for (int i = n; i >= 2; i--) {
    while (top && height[sta[top]] >= height[i]) top--;
    R[i] = top ? sta[top] - 1 : n;
    sta[++top] = i;
  }
  for (int i = 2; i <= n; i++)
    ans = max(
        ans, 1LL * height[i] * (R[i] - L[i] + 2 - (pre[R[i]] - pre[L[i] - 2])));
  cout << ans;
  return 0;
}
