#include <bits/stdc++.h>
using namespace std;
const int N = 1048576;
int sa[N], h[N], ra[N];
void init_sa(const char s[], int n, int m) {
  static int r1[N], r2[N], sum[N];
  int *x = r1, *y = r2, i, j, p;
  r1[n] = r2[n] = -1;
  for (i = 0; i < n; i++) sa[i] = i;
  for (i = 0; i < n; i++) x[i] = s[i];
  for (j = p = 0; p < n; j = j + j + !j, m = p) {
    p = 0;
    for (i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < m; i++) sum[i] = 0;
    for (i = 0; i < n; i++) ++sum[x[i]];
    for (i = 1; i < m; i++) sum[i] += sum[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--sum[x[y[i]]]] = y[i];
    swap(x, y), x[sa[0]] = 0, p = 1;
    for (i = 1; i < n; i++)
      x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + j] == y[sa[i - 1] + j]
                     ? p - 1
                     : p++;
  }
  for (i = 0; i < n; i++) ra[i] = x[i];
}
void init_h(const char s[], int n) {
  int top = h[0] = 0;
  for (int i = 0; i < n; i++) {
    if (ra[i]) {
      for (int j = sa[ra[i] - 1]; s[i + top] == s[j + top]; ++top) {
      }
      h[ra[i]] = top;
    }
    if (top) --top;
  }
}
char s[N], t[N];
int n, lef[N], rig[N], sumt[N];
deque<int> q;
long long max_rect() {
  long long mx = 0;
  q.push_back(0);
  for (int i = 1; i < n; i++) {
    while (q.size() > 1 && h[q.back()] >= h[i]) q.pop_back();
    lef[i] = q.back();
    q.push_back(i);
  }
  while (!q.empty()) q.pop_back();
  q.push_back(n);
  for (int i = n - 1; i; i--) {
    while (q.size() > 1 && h[q.back()] >= h[i]) q.pop_back();
    rig[i] = q.back();
    q.push_back(i);
  }
  for (int i = 0; i < n; i++)
    if (t[i] == '0') mx = max(mx, (long long)(n - i));
  for (int i = 1; i <= n; i++) sumt[i] = sumt[i - 1] + (t[sa[i - 1]] == '1');
  for (int i = 1; i < n; i++)
    if (t[sa[i]] == '0') {
      int l = lef[i];
      int r = rig[i];
      mx = max(mx, (long long)h[i] * (r - l - (sumt[r] - sumt[l])));
    }
  return mx;
}
int main() {
  scanf("%d %s %s", &n, s, t);
  reverse(s, s + n);
  reverse(t, t + n);
  init_sa(s, n, 128);
  init_h(s, n);
  cout << max_rect() << endl;
  return 0;
}
