#include <bits/stdc++.h>
using namespace std;
const long long MX = 2e5 + 10;
struct suffixArray {
  string s;
  long long n, alphabet;
  vector<long long> p, c, cnt, rank, lcp;
  vector<vector<long long> > st;
  suffixArray() {}
  suffixArray(string s) : s(s) {
    s += '$';
    n = s.size();
    alphabet = 256;
    p.resize(n, 0);
    c.resize(n, 0);
    cnt.resize(max(alphabet, n), 0);
    rank.resize(n - 1, 0);
    lcp.resize(n - 2, 0);
  }
  void build() {
    for (long long i = 0; i < n; i++) cnt[s[i]]++;
    for (long long i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];
    for (long long i = 0; i < n; i++) p[--cnt[s[i]]] = i;
    c[p[0]] = 0;
    long long classes = 1;
    for (long long i = 1; i < n; i++) {
      if (s[p[i]] != s[p[i - 1]]) classes++;
      c[p[i]] = classes - 1;
    }
    vector<long long> pn(n), cn(n);
    for (long long h = 0; (1ll << h) < n; h++) {
      for (long long i = 0; i < n; i++) {
        pn[i] = p[i] - (1ll << h);
        if (pn[i] < 0) pn[i] += n;
      }
      fill(cnt.begin(), cnt.begin() + classes, 0);
      for (long long i = 0; i < n; i++) cnt[c[pn[i]]]++;
      for (long long i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
      for (long long i = n - 1; i >= 0; i--) p[--cnt[c[pn[i]]]] = pn[i];
      cn[p[0]] = 0;
      classes = 1;
      for (long long i = 1; i < n; i++) {
        if (c[p[i]] != c[p[i - 1]] ||
            c[p[i] + (1 << h) - (p[i] + (1 << h) < n ? 0 : n)] !=
                c[p[i - 1] + (1 << h) - (p[i - 1] + (1 << h) < n ? 0 : n)])
          classes++;
        cn[p[i]] = classes - 1;
      }
      swap(c, cn);
    }
    n--;
    p.erase(p.begin());
    return;
  }
  long long lcp2(long long i, long long j) {
    long long l = rank[i];
    long long r = rank[j];
    if (l > r) swap(l, r);
    long long T = r - l + 1;
    long long lg = 63 - (__builtin_clzll(T));
    return min(st[lg][l], st[lg][r - (1 << lg) + 1]);
  }
  void buildST() {
    st.push_back(vector<long long>(n, -1));
    for (long long i = 0; i < n; i++) st[0][i] = n - p[i];
    for (long long h = 1; (1ll << h) <= n; h++) {
      st.push_back(vector<long long>(n, -1));
      for (long long i = 0; i + (1ll << h) - 1 < n; i++) {
        st[h][i] = min({st[h - 1][i], st[h - 1][i + (1ll << (h - 1))],
                        lcp[i - 1 + (1ll << (h - 1))]});
      }
    }
    return;
  }
  void lcp_construction() {
    for (long long i = 0; i < n; i++) rank[p[i]] = i;
    long long k = 0;
    for (long long i = 0; i < n; i++) {
      if (rank[i] == n - 1) {
        k = 0;
        continue;
      }
      long long j = p[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;
  }
  long long num_different_substrings() {
    long long ans = (n * n + n) / 2;
    for (long long i = 0; i < n - 1; i++) ans -= lcp[i];
    return ans;
  }
};
long long parent[MX];
long long rnk[MX];
long long cnt[MX];
long long findSet(long long i) {
  if (parent[i] == i) return i;
  parent[i] = findSet(parent[i]);
  return parent[i];
}
void unionSet(long long i, long long j) {
  long long x = findSet(i);
  long long y = findSet(j);
  if (y != x) {
    if (rnk[x] <= rnk[y]) swap(x, y);
    cnt[x] += cnt[y];
    parent[y] = x;
    if (rnk[x] == rnk[y]) rnk[y]++;
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(10);
  cout << fixed;
  long long n;
  cin >> n;
  string a, b;
  cin >> a >> b;
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  suffixArray sa = suffixArray(a);
  sa.build();
  sa.lcp_construction();
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    parent[i] = i;
    rnk[i] = 0;
    cnt[i] = (b[i] == '0');
    if (cnt[i] == 1) ans = max(ans, n - i);
  }
  vector<pair<long long, pair<long long, long long> > > order;
  for (long long i = 0; i < sa.lcp.size(); i++)
    order.push_back({sa.lcp[i], {sa.p[i], sa.p[i + 1]}});
  sort(order.rbegin(), order.rend());
  for (long long i = 0; i < n - 1; i++) {
    long long len = order[i].first;
    long long x = order[i].second.first;
    long long y = order[i].second.second;
    unionSet(x, y);
    ans = max(ans, len * cnt[findSet(x)]);
  }
  cout << ans << "\n";
  return 0;
}
