#include <bits/stdc++.h>
using namespace std;
const int mxn = (1 << (20)) + 5;
const int mxl = 20;
int n, stp, mv, suffix[mxn], tmp[mxn];
int sum[mxn], cnt[mxn], rnk[mxl][mxn];
char str[mxn], ss[mxn];
inline bool eql(const int &u, const int &v) {
  if (!stp) return (str[u] == str[v]);
  if (rnk[stp - 1][u] != rnk[stp - 1][v]) return false;
  int a = u + mv < n ? rnk[stp - 1][u + mv] : -1;
  int b = v + mv < n ? rnk[stp - 1][v + mv] : -1;
  return a == b;
}
void update() {
  for (int i = 0; i < n; i++) sum[i] = 0;
  for (int i = 0, r = 0; i < n; i++) {
    suffix[i] = tmp[i];
    if (i && !eql(suffix[i], suffix[i - 1])) {
      rnk[stp][suffix[i]] = ++r;
      sum[r + 1] = sum[r];
    } else
      rnk[stp][suffix[i]] = r;
    sum[r + 1]++;
  }
  return;
}
void Sort() {
  for (int i = 0; i < n; i++) cnt[i] = 0;
  memset(tmp, -1, sizeof tmp);
  for (int i = 0; i < mv; i++) {
    int idx = rnk[stp - 1][n - i - 1];
    int x = sum[idx];
    tmp[x + cnt[idx]] = n - i - 1;
    cnt[idx]++;
  }
  for (int i = 0; i < n; i++) {
    int idx = suffix[i] - mv;
    if (idx < 0) continue;
    idx = rnk[stp - 1][idx];
    int x = sum[idx];
    tmp[x + cnt[idx]] = suffix[i] - mv;
    cnt[idx]++;
  }
  update();
  return;
}
inline bool cmp(const int &a, const int &b) {
  if (str[a] != str[b]) return str[a] < str[b];
  return false;
}
void SortSuffix() {
  stp = mv = 0;
  for (int i = 0; i < n; i++) tmp[i] = i;
  sort(tmp, tmp + n, cmp);
  stp = 0;
  update();
  ++stp;
  for (mv = 1; mv < n; mv <<= 1) Sort(), stp++;
  stp--;
  for (int i = 0; i <= stp; i++) rnk[i][n] = -1;
}
inline int lcp(int u, int v) {
  if (v == n) return -1;
  if (u == v) return n - u;
  int ret = 0;
  for (int i = stp; i >= 0; i--)
    if (rnk[i][u] == rnk[i][v]) {
      ret += 1 << i;
      u += 1 << i;
      v += 1 << i;
    }
  return ret;
}
long long cumu[mxn];
stack<pair<long long, long long> > stck;
int main() {
  scanf("%d %s %s", &n, str, ss);
  long long res = 0;
  reverse(str, str + n);
  reverse(ss, ss + n);
  for (int i = 0; i < n && !res; i++)
    if (ss[i] == '0') res = (n - i);
  SortSuffix();
  suffix[n] = n;
  for (int i = 0; i < n; i++) {
    if (i) cumu[i] = cumu[i - 1];
    cumu[i] += (ss[suffix[i]] == '0');
    int x = lcp(suffix[i], suffix[i + 1]);
    int prev = i;
    while (!stck.empty() && x < stck.top().first) {
      long long t = cumu[i];
      if (stck.top().second) t -= cumu[stck.top().second - 1];
      res = max(res, stck.top().first * t);
      prev = stck.top().second;
      stck.pop();
    }
    stck.push({x, prev});
  }
  printf("%lld\n", res);
  return 0;
}
