#include <bits/stdc++.h>
using namespace std;
const int NN = 5e5;
const int MM = 20;
char s[NN], t[NN];
int rk[NN], sa[NN], ht[NN];
int mn[NN][MM];
namespace SA {
int head[NN << 1];
pair<pair<int, int>, int> pos[NN << 1], buf[NN << 1], tbuf[NN << 1];
int nxt[NN << 1];
int N;
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 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 LCP(int n) {
  for (int i = 1; i <= n; i++) mn[i][0] = ht[i];
  for (int j = 1; j < MM; 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 buildSA(int n) {
  N = n;
  int i, j, cnt, u;
  for (i = 0; i < n; i++) rk[i] = s[i];
  rk[n] = 0;
  for (i = 0; i < MM; i++) {
    for (j = 0; j < n; j++)
      pos[j] = make_pair(make_pair(rk[j], rk[min(n, j + (1 << i))]), j);
    bucket_sort(0, n);
    bucket_sort(1, n);
    pos[n] = make_pair(make_pair(-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 N - u;
  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
int c[NN], d[NN];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s %s", s, t);
  reverse(s, s + n);
  reverse(t, t + n);
  SA::buildSA(n);
  for (int i = 0; i < n; i++)
    if (t[i] == '0') c[rk[i]] = 1;
  for (int i = 1; i <= n; i++) d[i] = d[i - 1] + c[i];
  long long mx = 0;
  for (int i = 1; i <= n; i++)
    if (c[i]) {
      mx = max(1ll * (n - sa[i]), mx);
      int st, ed, md;
      int L, R;
      st = 0, L = i;
      while (st + 1 < L) {
        md = st + L >> 1;
        if (SA::calc(sa[i], sa[md]) >= ht[i])
          L = md;
        else
          st = md;
      }
      R = i, ed = n + 1;
      while (R + 1 < ed) {
        md = R + ed >> 1;
        if (SA::calc(sa[i], sa[md]) >= ht[i])
          R = md;
        else
          ed = md;
      }
      if ((long long)(d[R] - d[L - 1]) * ht[i] > mx) {
        mx = (long long)(d[R] - d[L - 1]) * ht[i];
      }
    }
  cout << mx << endl;
  return 0;
}
