#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, sa[N], lcp[N], r[N], rNew[N], L[N], R[N], cnt[N];
string s, t;
bool suffCmp(int u, int v) {
  return (r[u] != r[v]
              ? r[u] < r[v]
              : (u + k < n && v + k < n ? r[u + k] < r[v + k] : u > v));
}
void makeSA() {
  for (int i = 0; i < n; i++) sa[i] = i, r[i] = s[i];
  for (k = 1; k <= n; k *= 2) {
    sort(sa, sa + n, suffCmp);
    for (int i = 0; i < n - 1; i++)
      rNew[i + 1] = rNew[i] + suffCmp(sa[i], sa[i + 1]);
    for (int i = 0; i < n; i++) r[sa[i]] = rNew[i];
    if (rNew[n - 1] == n - 1) break;
  }
}
void makeLCP() {
  for (int i = 0, k = 0; i < n; i++, k -= (k > 0))
    if (r[i] != n - 1) {
      for (int j = sa[r[i] + 1]; s[i + k] == s[j + k];) k++;
      lcp[r[i]] = k;
    }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> s >> t;
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  makeSA();
  makeLCP();
  for (int i = 0; i < n; i++)
    cnt[i] = (i == 0 ? 0 : cnt[i - 1]) + (t[sa[i]] == '1');
  if (cnt[n - 1] == n) return cout << 0, 0;
  for (int i = 0; i < n; i++) {
    L[i] = i;
    int j = i - 1;
    while (j >= 0 && lcp[i] <= lcp[j]) L[i] = L[j], j = L[j] - 1;
  }
  for (int i = n - 1; i >= 0; i--) {
    R[i] = i;
    int j = i + 1;
    while (j + 1 < n && lcp[i] <= lcp[j]) R[i] = R[j], j = R[j] + 1;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) ans = max(ans, 1ll * (t[i] == '0' ? n - i : 0));
  for (int i = 0; i < n; i++) {
    int u = L[i], v = R[i];
    int bicam = cnt[v + 1] - (u == 0 ? 0 : cnt[u - 1]);
    ans = max(ans, 1ll * lcp[i] * (R[i] - L[i] + 2 - bicam));
  }
  cout << ans;
  return 0;
}
