#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
const int N = 2e5 + 2;
const int L = 20;
int p[N][L];
int ord[N];
int acp[N];
int bcp[N];
int ocp[N];
int cnt[N];
int opt[N];
int a[N];
int b[N];
int s[N];
int lgx;
int n;
long long ans;
string T;
string F;
vector<int> h;
int lcp(int i, int j) {
  int res = 0;
  for (int k = lgx; k >= 0 && i < n && j < n; --k) {
    if (p[i][k] == p[j][k]) {
      res += 1 << k;
      i += 1 << k;
      j += 1 << k;
    }
  }
  return res;
}
void counting_sort(int a[], int b[]) {
  fill(cnt, cnt + N, 0);
  for (int i = 0; i < n; ++i) {
    cnt[a[i] + 1]++;
  }
  for (int i = 1; i < N; ++i) {
    cnt[i] += cnt[i - 1];
  }
  for (int i = n - 1; i >= 0; --i) {
    cnt[a[i] + 1]--;
    ocp[cnt[a[i] + 1]] = ord[i];
    acp[cnt[a[i] + 1]] = a[i];
    bcp[cnt[a[i] + 1]] = b[i];
  }
  copy(ocp, ocp + n, ord);
  copy(acp, acp + n, a);
  copy(bcp, bcp + n, b);
}
void radix_sort() {
  copy(opt, opt + n, ord);
  counting_sort(b, a);
  counting_sort(a, b);
}
void input() {
  cin >> n;
  cin >> T;
  cin >> F;
  reverse(T.begin(), T.end());
  reverse(F.begin(), F.end());
  for (int i = 0; i < n; ++i) {
    p[i][0] = T[i];
    opt[i] = i;
  }
}
void max_rectangle() {
  stack<int> s;
  s.push(0);
  for (int i = 1; i < h.size(); ++i) {
    while (h[i] < h[s.top()]) {
      int x = s.top();
      s.pop();
      int y = s.top();
      ans = max(ans, 1ll * (i - y) * h[x]);
    }
    s.push(i);
  }
}
void solve() {
  for (int i = 1, j = 1; j <= n; ++i, j <<= 1) {
    for (int k = 0; k < n; ++k) {
      a[k] = p[k][i - 1];
      b[k] = (k + j < n ? p[k + j][i - 1] : -1);
    }
    radix_sort();
    for (int k = 1; k < n; ++k) {
      p[ord[k]][i] =
          (pair<int, int>(a[k], b[k]) == pair<int, int>(a[k - 1], b[k - 1])
               ? p[ord[k - 1]][i]
               : k);
    }
    lgx = i;
  }
  for (int i = 0; i < n; ++i) {
    s[p[i][lgx]] = i;
    if (F[i] == '0') {
      ans = max(ans, 1ll * n - i);
    }
  }
  h.push_back(0);
  for (int i = 0, j = 0, cur = 0; i < n; ++i, cur++) {
    if (F[s[i]] == '0') {
      if (F[s[j]] == '0' && i != j) {
        h.push_back(lcp(s[j], s[i]));
      }
      j = i;
    }
  }
  h.push_back(0);
  max_rectangle();
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  input();
  solve();
}
