#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > c;
vector<int> sort_cyclic_shifts(const string &s) {
  int n = s.size();
  const int alphabet = 256;
  vector<int> p(n), cnt(alphabet, 0);
  c.clear();
  c.emplace_back();
  c[0].resize(n);
  for (int i = 0; i < n; i++) cnt[s[i]]++;
  for (int i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];
  for (int i = 0; i < n; i++) p[--cnt[s[i]]] = i;
  c[0][p[0]] = 0;
  int classes = 1;
  for (int i = 1; i < n; i++) {
    if (s[p[i]] != s[p[i - 1]]) classes++;
    c[0][p[i]] = classes - 1;
  }
  vector<int> pn(n), cn(n);
  cnt.resize(n);
  for (int h = 0; (1 << h) < n; h++) {
    for (int i = 0; i < n; i++) {
      pn[i] = p[i] - (1 << h);
      if (pn[i] < 0) pn[i] += n;
    }
    fill(cnt.begin(), cnt.end(), 0);
    for (int i = 0; i < n; i++) cnt[c[h][pn[i]]]++;
    for (int i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
    for (int i = n - 1; i >= 0; i--) p[--cnt[c[h][pn[i]]]] = pn[i];
    cn[p[0]] = 0;
    classes = 1;
    for (int i = 1; i < n; i++) {
      pair<int, int> cur = {c[h][p[i]], c[h][(p[i] + (1 << h)) % n]};
      pair<int, int> prev = {c[h][p[i - 1]], c[h][(p[i - 1] + (1 << h)) % n]};
      if (cur != prev) ++classes;
      cn[p[i]] = classes - 1;
    }
    c.push_back(cn);
  }
  return p;
}
vector<int> suffix_array_construction(string s) {
  s += "!";
  vector<int> sorted_shifts = sort_cyclic_shifts(s);
  sorted_shifts.erase(sorted_shifts.begin());
  return sorted_shifts;
}
int getlcp(int i, int j) {
  int log_n = c.size() - 1;
  int ans = 0;
  for (int k = log_n; k >= 0; k--) {
    if (c[k][i] == c[k][j]) {
      ans += 1 << k;
      i += 1 << k;
      j += 1 << k;
    }
  }
  return ans;
}
vector<int> lcp_construction(const string &s, const vector<int> &sa) {
  int n = s.size();
  vector<int> rank(n, 0);
  vector<int> lcp(n - 1, 0);
  for (int i = 0; i < n; i++) rank[sa[i]] = i;
  for (int i = 0, k = 0; i < n; i++) {
    if (rank[i] == n - 1) {
      k = 0;
      continue;
    }
    int j = sa[rank[i] + 1];
    while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++;
    lcp[rank[i]] = k;
    if (k) k--;
  }
  return lcp;
}
const int N = 2e5 + 7, K = 18;
int st[N][K];
int lg[N];
void pre() {
  lg[1] = 0;
  for (int i = 2; i < N; i++) lg[i] = lg[i / 2] + 1;
}
void build(const vector<int> &lcp) {
  int n = lcp.size();
  for (int i = 0; i < n; i++) st[i][0] = lcp[i];
  for (int k = 1; k < K; k++)
    for (int i = 0; i < n; i++) {
      st[i][k] = st[i][k - 1];
      int nxt = i + (1 << (k - 1));
      if (nxt >= n) continue;
      st[i][k] = min(st[i][k], st[nxt][k - 1]);
    }
}
int get(int l, int r) {
  int k = lg[r - l + 1];
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
int f(int l, int r, int tar) {
  int lo = l, hi = r;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    int k = get(l, mid);
    if (k >= tar)
      lo = mid;
    else
      hi = mid - 1;
  }
  return lo;
}
int g(int l, int r, int tar) {
  int lo = l, hi = r;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    int k = get(mid, r);
    if (k >= tar)
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}
string s, is;
int cum[N];
int qq(int l, int r) { return cum[r + 1] - cum[l]; }
int main() {
  pre();
  int n;
  cin >> n;
  cin >> s >> is;
  reverse(s.begin(), s.end());
  reverse(is.begin(), is.end());
  vector<int> sa = suffix_array_construction(s);
  vector<int> lcp = lcp_construction(s, sa);
  build(lcp);
  for (int i = 1; i <= n; i++) cum[i] = cum[i - 1] + (is[sa[i - 1]] == '0');
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int len = n - sa[i];
    if (is[sa[i]] == '0') ans = max(ans, 1LL * len);
    int m = (i ? lcp[i - 1] : 0);
    int r = f(i - 1, n - 2, m);
    int l = g(0, i - 1, m);
    int occ = qq(l, r + 1);
    ans = max(ans, 1LL * m * occ);
  }
  cout << ans << endl;
}
