#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ppi = pair<pii, int>;
char s[200100];
int rk[200100], sa[200100], ht[200100];
int mn[200100][22];
namespace SA {
int head[200100 * 2];
ppi pos[200100 * 2], buf[200100 << 1], tbuf[200100 << 1];
int nxt[200100 * 2];
void bucket_sort(int ok, int n) {
  int i, u, j, deg, E, K;
  for (i = E = K = 0; i < max(222, n + 10); i++) head[i] = -1;
  for (i = 0; i < n; i++) {
    if (ok)
      u = pos[i].first.first + 1;
    else
      u = pos[i].first.second + 1;
    buf[E] = pos[i];
    nxt[E] = head[u];
    head[u] = E++;
  }
  for (int i = 0; i < max(n + 10, 222); i++) {
    for (deg = 0, j = head[i]; ~j; j = nxt[j]) {
      tbuf[deg++] = buf[j];
    }
    while (deg) pos[K++] = tbuf[--deg];
  }
}
void LCP(int n) {
  for (int i = 1; i <= n; i++) mn[i][0] = ht[i];
  for (int j = 1; j < 22; j++) {
    for (int i = 1; i <= n; i++) {
      mn[i][j] = min(mn[i][j - 1], mn[min(n, i + (1 << j - 1))][j - 1]);
    }
  }
}
void height(int n) {
  int h = 0, i;
  for (i = 0; i < n; i++) sa[rk[i]] = i;
  for (i = 0; i < n; i++) {
    if (rk[i] == 1) continue;
    h = max(0, h - 1);
    for (int j = sa[rk[i] - 1]; s[i + h] == s[j + h]; h++)
      ;
    ht[rk[i]] = h;
  }
}
void buildSA(int n) {
  int i, j, cnt, u;
  for (i = 0; i < n; i++) rk[i] = s[i];
  rk[n] = 0;
  for (i = 0; i < 20; i++) {
    for (j = 0; j < n; j++)
      pos[j] = ppi(pii(rk[j], rk[min(n, j + (1 << i))]), j);
    bucket_sort(0, n);
    bucket_sort(1, n);
    pos[n] = ppi(pii(-1, -1), -1);
    for (cnt = j = 0; j < n; j++) {
      rk[pos[j].second] = cnt + 1;
      cnt += (pos[j].first != pos[j + 1].first);
    }
    if (cnt == n) break;
  }
  height(n);
  LCP(n);
}
int calc(int u, int v) {
  if (u == v) return 100000000;
  u = rk[u], v = rk[v];
  if (u > v) swap(u, v);
  u++;
  int l = 31 - __builtin_clz(v - u + 1);
  return min(mn[u][l], mn[v - (1 << l) + 1][l]);
}
}  // namespace SA
using namespace SA;
using INT = long long;
char vst[200100];
int cnt[200100];
int main() {
  int n;
  cin >> n;
  cin >> s;
  cin >> vst;
  reverse(s, s + n);
  buildSA(n);
  INT ans = 0;
  for (int i = 1; i <= n; i++) {
    int u = sa[i];
    cnt[i] = cnt[i - 1];
    u = n - sa[i] - 1;
    if (vst[u] == '0') {
      ans = max(ans, (INT)n - sa[i]);
      cnt[i]++;
    }
  }
  for (int i = 2; i <= n; i++) {
    int st = 0, ed = i;
    while (st + 1 < ed) {
      int md = (st + ed) >> 1;
      if (calc(sa[md], sa[i]) >= ht[i])
        ed = md;
      else
        st = md;
    }
    int l = ed;
    st = i, ed = n + 1;
    while (st + 1 < ed) {
      int md = (st + ed) >> 1;
      if (calc(sa[md], sa[i]) >= ht[i])
        st = md;
      else
        ed = md;
    }
    int r = st;
    ans = max(ans, (INT)ht[i] * (cnt[r] - cnt[l - 1]));
  }
  cout << ans << endl;
  return 0;
}
