#include <bits/stdc++.h>
using namespace std;
using INT = long long;
using pii = pair<int, int>;
char s[400100];
int I[400100], H[400100], rk[400100], sa[400100];
int head[400100], buf[400100], nxt[400100], mn[400100][20];
pii pos[400100];
void bucket_sort(int d, int n) {
  int E = 0, m = max(n + 10, 256);
  for (int i = 0; i <= m; i++) head[i] = 0;
  for (int i = 0, u; i < n; i++) {
    if (d == 1)
      u = pos[I[i]].first;
    else
      u = pos[I[i]].second;
    buf[++E] = I[i];
    nxt[E] = head[u];
    head[u] = E;
  }
  int st = m, ed = -1;
  if (d == 1) st = 0, ed = m + 1;
  for (int i = st, K = 0; i != ed; i += d) {
    for (int j = head[i]; j; j = nxt[j]) I[K++] = buf[j];
  }
}
void buildSA(int n) {
  for (int i = 0; i < n; i++) rk[i] = s[i];
  rk[n] = 0;
  for (int i = 0; i < 20; i++) {
    for (int j = 0; j < n; j++)
      pos[j] = pii(rk[j], rk[min(n, j + (1 << i))]), I[j] = j;
    bucket_sort(-1, n);
    bucket_sort(1, n);
    int cnt = 0;
    pos[n] = pii(-1, -1);
    I[n] = n;
    for (int j = 0; j < n; j++) {
      rk[I[j]] = cnt + 1;
      cnt += pos[I[j]] != pos[I[j + 1]];
    }
    if (cnt == n) break;
  }
  for (int i = 0; i < n; i++) sa[rk[i]] = i;
  for (int i = 0, h = 0; i < n; i++) {
    if (rk[i] == 1) continue;
    h = max(h - 1, 0);
    for (int j = sa[rk[i] - 1]; s[i + h] == s[j + h]; h++)
      ;
    H[rk[i]] = h;
  }
  for (int i = 0; i < 20; i++) {
    for (int j = 1; j + (1 << i) <= n + 1; j++) {
      if (i)
        mn[j][i] = min(mn[j][i - 1], mn[j + (1 << i - 1)][i - 1]);
      else
        mn[j][i] = H[j];
    }
  }
}
char st[400100];
int A[400100];
int p[400100];
int prv[400100], nt[400100];
int main() {
  int n;
  cin >> n;
  scanf("%s", s);
  reverse(s, s + n);
  scanf("%s", st);
  INT ans = 0;
  for (int i = 0; i < n; i++) {
    if (st[i] == '0') {
      ans = i + 1;
    }
  }
  buildSA(n);
  int nn = 0;
  int pre = 0;
  for (int i = 1; i <= n; i++)
    if (st[n - sa[i] - 1] == '0') {
      if (pre == 0) {
        pre = i;
        continue;
      }
      int mn = n + 1;
      for (int j = pre + 1; j <= i; j++) {
        mn = min(mn, H[j]);
      }
      p[++nn] = mn;
      pre = i;
    }
  n = nn;
  for (int i = 1; i <= n; i++) {
    prv[i] = i - 1;
    while (prv[i] and p[prv[i]] >= p[i]) prv[i] = prv[prv[i]];
  }
  for (int i = n; i; i--) {
    nt[i] = i + 1;
    while (nt[i] <= n and p[nt[i]] >= p[i]) nt[i] = nt[nt[i]];
  }
  int len = n + 1, pos = 1;
  for (int i = 1; i <= n; i++) {
    if ((INT)(nt[i] - prv[i]) * p[i] > ans) {
      ans = (INT)(nt[i] - prv[i]) * p[i];
    }
  }
  cout << ans << endl;
}
