#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T maxs(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T mins(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
struct suffix_array {
  string s;
  vector<int> p, c;
  vector<int> lcp, len;
  int n;
  suffix_array(string str) {
    s = str;
    n = s.size();
    p.resize(n);
    c.resize(n);
    lcp.resize(n);
    len.resize(n);
  }
  void count_sort(vector<int>& p, vector<int>& c) {
    int n = p.size();
    vector<int> cnt(n);
    for (auto first : c) cnt[first]++;
    vector<int> p_new(n), pos(n);
    pos[0] = 0;
    for (int i = 1; i < n; i++) pos[i] = pos[i - 1] + cnt[i - 1];
    for (auto first : p) {
      int i = c[first];
      p_new[pos[i]] = first;
      pos[i]++;
    }
    p = p_new;
  }
  void build() {
    vector<pair<char, int>> a(n);
    for (int i = 0; i < n; i++) a[i] = {s[i], i};
    sort(a.begin(), a.end());
    for (int i = 0; i < n; i++) p[i] = a[i].second;
    c[p[0]] = 0;
    for (int i = 1; i < n; i++)
      c[p[i]] = (a[i].first == a[i - 1].first) ? c[p[i - 1]] : c[p[i - 1]] + 1;
    int k = 0;
    while ((1 << k) < n) {
      for (int i = 0; i < n; i++) p[i] = (p[i] - (1 << k) + n) % n;
      count_sort(p, c);
      vector<int> c_new(n);
      c_new[p[0]] = 0;
      for (int i = 1; i < n; i++) {
        pair<int, int> prev = {c[p[i - 1]], c[(p[i - 1] + (1 << k)) % n]};
        pair<int, int> now = {c[p[i]], c[(p[i] + (1 << k)) % n]};
        if (now == prev) {
          c_new[p[i]] = c_new[p[i - 1]];
        } else {
          c_new[p[i]] = c_new[p[i - 1]] + 1;
        }
      }
      c = c_new;
      k++;
    }
    for (int i = 0; i < n; i++) {
      len[i] = n - p[i];
    }
    k = 0;
    for (int i = 0; i < n - 1; i++) {
      int pi = c[i];
      int j = p[pi - 1];
      while (s[i + k] == s[j + k]) k++;
      lcp[pi] = k;
      k = max(0, k - 1);
    }
  }
};
const int maxn = 2000005;
int p[maxn];
int sz[maxn];
int dp[maxn];
void clear(int n = maxn) {
  for (int i = 0; i < maxn; i++) p[i] = i, sz[i] = 1;
}
int root(int first) {
  while (first != p[first]) {
    p[first] = p[p[first]];
    first = p[first];
  }
  return first;
}
void merge(int first, int second) {
  int p1 = root(first);
  int p2 = root(second);
  if (p1 == p2) return;
  if (sz[p1] < sz[p2]) swap(p1, p2);
  sz[p1] += sz[p2];
  dp[p1] += dp[p2];
  p[p2] = p1;
}
int solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  vector<bool> bad(n + 1);
  int cnt = 0;
  bad[n] = 1;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    int first = c - 48;
    if (first == 0) ans = i + 1;
    bad[n - 1 - i] = first;
  }
  if (cnt == n) {
    cout << 0 << "\n";
    return 0;
  }
  clear();
  for (int i = 0; i <= n; i++) {
    dp[i] = bad[i];
  }
  s = s + '$';
  suffix_array sa(s);
  sa.build();
  vector<pair<int, int>> edges;
  for (int i = 1; i <= n; i++) edges.emplace_back(sa.lcp[i], i);
  sort(edges.begin(), edges.end(), greater<pair<int, int>>());
  for (auto i : edges) {
    int j = i.second;
    int u = sa.p[j];
    int v = sa.p[j - 1];
    merge(u, v);
    int k = sz[root(u)] - dp[root(u)];
    maxs(ans, 1LL * i.first * k);
  }
  cout << ans << "\n";
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
