#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int inf = INT_MAX;
const long long inff = 1e18;
const long long mod = 1e9 + 7;
int n;
string s, forbid;
int tmp[maxn], sa[maxn], ranks[maxn], height[maxn];
int l[maxn], r[maxn];
int p[maxn];
int a[maxn];
void Build() {
  vector<int> v;
  for (int i = 0; i < n; i++) v.push_back(s[i]);
  sort((v).begin(), (v).end());
  for (int i = 0; i < n; i++)
    tmp[i] = lower_bound((v).begin(), (v).end(), s[i]) - v.begin();
  vector<pair<int, int> > V;
  for (int k = 1; k <= n + 2; k <<= 1) {
    for (int i = 0; i < n; i++) {
      if (i + k < n)
        V.push_back(make_pair(tmp[i], tmp[i + k]));
      else
        V.push_back(make_pair(tmp[i], -1));
    }
    sort((V).begin(), (V).end());
    for (int i = 0; i < n; i++) {
      tmp[i] = lower_bound((V).begin(), (V).end(),
                           make_pair(tmp[i], (i + k >= n ? -1 : tmp[i + k]))) -
               V.begin();
    }
    V.clear();
  }
  for (int i = 0; i < n; i++) sa[tmp[i]] = i;
}
void Build_Lcp() {
  for (int i = 0; i < n; i++) ranks[sa[i]] = i;
  int k = 0;
  for (int i = 0; i < n; i++) {
    if (k) k--;
    if (ranks[i] == 0) continue;
    int j = sa[ranks[i] - 1];
    while (s[i + k] == s[j + k]) k++;
    height[ranks[i]] = k;
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> s >> forbid;
  reverse((s).begin(), (s).end());
  reverse((forbid).begin(), (forbid).end());
  Build();
  Build_Lcp();
  for (int i = 0; i < n; i++) {
    if (forbid[i] == '1') a[ranks[i]]++;
  }
  for (int i = 0; i < n; i++) {
    if (i > 0) p[i] = p[i - 1];
    if (a[i] == 1) p[i]++;
  }
  height[0] = -1;
  height[n] = -1;
  vector<int> v;
  v.push_back(0);
  for (int i = 1; i <= n - 1; i++) {
    while (height[i] <= height[v.back()]) v.pop_back();
    l[i] = v.back();
    v.push_back(i);
  }
  v.clear();
  v.push_back(n);
  for (int i = n - 1; i >= 1; i--) {
    while (height[i] <= height[v.back()]) v.pop_back();
    r[i] = v.back() - 1;
    v.push_back(i);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (forbid[i] == '0') ans = max(ans, n * 1ll - i);
  }
  for (int i = 1; i <= n - 1; i++) {
    ans = max(ans,
              height[i] * 1ll *
                  (r[i] - l[i] + 1 - p[r[i]] + (l[i] == 0 ? 0 : p[l[i] - 1])));
  }
  cout << ans << endl;
}
