#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:536870912")
#pragma warning(disable : 4996)
using namespace std;
const int N = 2e5 + 100, X[2] = {997, 449};
char s[N], t[N];
long long int h[2][N], p[2][N];
int w[N], g[N], lb[N], rb[N];
int n;
pair<long long int, long long int> getH(int l, int r) {
  return {h[0][r] - h[0][l - 1] * p[0][r - l + 1],
          h[1][r] - h[1][l - 1] * p[1][r - l + 1]};
}
int lcp(int a, int b) {
  int l = 0, r = n - max(a, b) + 1;
  while (r - l > 1) {
    int p = (r + l) >> 1;
    if (getH(a + 1, a + p) == getH(b + 1, b + p))
      l = p;
    else
      r = p;
  }
  return l;
}
int main() {
  scanf("%d %s %s", &n, s, t);
  reverse(s, s + n);
  reverse(t, t + n);
  for (int i = 0; i < 2; i++) {
    p[i][0] = 1;
    for (int j = 0; j < n; j++) {
      p[i][j + 1] = p[i][j] * X[i];
      h[i][j + 1] = h[i][j] * X[i] + s[j];
    }
  }
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (t[i] == '0') w[c++] = i;
  }
  sort(w, w + c, [](int a, int b) {
    int x = lcp(a, b);
    return s[a + x] < s[b + x];
  });
  for (int i = 0; i < c - 1; i++) {
    g[i] = lcp(w[i], w[i + 1]);
  }
  vector<int> v;
  for (int i = 0; i < c - 1; i++) {
    while (!v.empty() && g[v.back()] >= g[i]) v.pop_back();
    lb[i] = v.empty() ? -1 : v.back();
    v.push_back(i);
  }
  v.clear();
  for (int i = c - 2; i >= 0; i--) {
    while (!v.empty() && g[v.back()] >= g[i]) v.pop_back();
    rb[i] = v.empty() ? c - 1 : v.back();
    v.push_back(i);
  }
  long long int ans = 0;
  for (int i = 0; i < c; i++) ans = max(ans, (long long int)n - w[i]);
  for (int i = 0; i < c - 1; i++) {
    ans = max(ans, g[i] * (long long int)(rb[i] - lb[i]));
  }
  printf("%lld", ans);
  return 0;
}
