#include <bits/stdc++.h>
using namespace std;
char s[400003], b[400003];
int rnk[400003], sa[400003], tp[400003], buc[400003], n, m, height[400003],
    stk[400003], cnt[400003];
void bucsort() {
  for (int i = 1; i <= m; i++) buc[i] = 0;
  for (int i = 1; i <= n; i++) buc[rnk[i]]++;
  for (int i = 1; i <= m; i++) buc[i] += buc[i - 1];
  for (int i = n; i >= 1; i--) sa[buc[rnk[tp[i]]]--] = tp[i];
}
void sufsort() {
  m = 128;
  for (int i = 1; i <= n; i++) rnk[i] = s[i], tp[i] = i;
  bucsort();
  for (int t = 1, cnt = 0; cnt < n; m = cnt, t <<= 1) {
    cnt = 0;
    for (int i = n - t + 1; i <= n; i++) tp[++cnt] = i;
    for (int i = 1; i <= n; i++) {
      if (sa[i] > t) tp[++cnt] = sa[i] - t;
    }
    bucsort();
    for (int i = 1; i <= n; i++) tp[i] = rnk[i];
    rnk[sa[1]] = cnt = 1;
    for (int i = 2; i <= n; i++) {
      rnk[sa[i]] =
          (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + t] == tp[sa[i - 1] + t]
               ? cnt
               : ++cnt);
    }
  }
}
void geth() {
  int k = 0;
  for (int i = 1; i <= n; i++) rnk[sa[i]] = i;
  for (int i = 1; i <= n; i++) {
    if (rnk[i] == 1) continue;
    if (k) k--;
    int j = sa[rnk[i] - 1];
    while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++;
    height[rnk[i]] = k;
  }
}
int main() {
  scanf("%d%s%s", &n, s + 1, b + 1);
  reverse(s + 1, s + n + 1), reverse(b + 1, b + n + 1);
  sufsort();
  geth();
  long long ans = 0;
  int top = 0;
  for (int i = 1; i <= n; i++) {
    if (b[sa[i - 1]] == '0') cnt[i]++;
    while (top && height[i] <= height[stk[top]]) {
      cnt[i] += cnt[stk[top]];
      ans = max(ans, (long long)cnt[i] * height[stk[top]]);
      top--;
    }
    stk[++top] = i;
  }
  if (b[sa[n]] == '0') cnt[n + 1]++;
  while (top) {
    cnt[n + 1] += cnt[stk[top]];
    ans = max(ans, (long long)cnt[n + 1] * height[stk[top]]);
    top--;
  }
  for (int i = 1; i <= n; i++)
    if (b[i] == '0') {
      ans = max(ans, (long long)n - i + 1);
      break;
    }
  cout << ans << endl;
  return 0;
}
