#include <bits/stdc++.h>
using namespace std;
const int INFTY = 20000000;
const int MAX = 200100;
const int MOD = 10000000;
void coutTab(int* tab, int n) {
  for (int i = 0; i < n; i++) {
    cout << tab[i] << " ";
  }
  cout << "\n";
}
template <class T>
void coutVec(vector<T> tab) {
  for (T t : tab) {
    cout << t << " ";
  }
  cout << "\n";
}
vector<vector<int>> kmr(const string& s) {
  int S = s.size();
  vector<vector<int>> kmr(1, vector<int>(S + 1));
  int k = 0;
  for (int i = 0; i < S; i++) kmr[k][i] = s[i] - 'a' + 1;
  kmr[k][s.size()] = 0;
  while ((1 << k) < S) {
    kmr.push_back(vector<int>(S + 1));
    k++;
    vector<pair<int, int>> c;
    int K = 1 << (k - 1);
    for (int i = 0; i < S; i++)
      c.push_back({kmr[k - 1][i], ((i + K > S) ? 0 : kmr[k - 1][i + K])});
    sort(c.begin(), c.end());
    c.erase(unique(c.begin(), c.end()), c.end());
    map<pair<int, int>, int> cc;
    for (int i = 0; i < (int)c.size(); i++) cc[c[i]] = i + 1;
    for (int i = 0; i < S; i++)
      kmr[k][i] = cc[{kmr[k - 1][i], ((i + K > S) ? 0 : kmr[k - 1][i + K])}];
    kmr[k][S] = 0;
  }
  return kmr;
}
vector<int> suffix_array(const string& s) {
  auto k = kmr(s);
  int ks = k.size() - 1;
  vector<int> sarr(s.size());
  for (int i = 0; i < (int)s.size(); i++) sarr[k[ks][i] - 1] = i;
  return sarr;
}
vector<int> LCP(const string& s, const vector<int>& suffix_array) {
  int n = suffix_array.size();
  vector<int> lcp(n, 0);
  vector<int> R(n + 1);
  for (int i = 0; i < n; i++) R[suffix_array[i]] = i;
  int l = 0;
  for (int i = 0; i < n; i++) {
    if (R[i] == n - 1) {
      l = 0;
      continue;
    }
    int j = suffix_array[R[i] + 1];
    while (l + i < n && l + j < n && s[l + i] == s[l + j]) l++;
    lcp[R[i]] = l;
    l = max(0, l - 1);
  }
  return lcp;
}
int T[MAX];
int R[MAX];
long long B[MAX];
void init_fu(int n, string block, const vector<int>& sa) {
  for (int i = 0; i < n; i++) {
    R[i] = 0;
    T[i] = i;
    B[i] = 1 - (block[n - sa[i] - 1] - '0');
  }
}
int fu_find(int v) {
  if (T[v] == v) return v;
  T[v] = fu_find(T[v]);
  return T[v];
}
void fu_union(int u, int v) {
  int tu = fu_find(u);
  int tv = fu_find(v);
  if (R[tu] < R[tv]) {
    T[tu] = tv;
    B[tv] += B[tu];
  } else {
    T[tv] = tu;
    B[tu] += B[tv];
    if (R[tu] == R[tv]) {
      R[tu]++;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  string s, a;
  int n;
  cin >> n;
  cin >> s;
  cin >> a;
  reverse((s).begin(), (s).end());
  auto sa = suffix_array(s);
  auto lcp = LCP(s, sa);
  init_fu(n, a, sa);
  vector<pair<long long, long long>> lp;
  for (int i = 0; i < n - 1; i++) {
    lp.push_back({lcp[i], i});
  }
  sort((lp).begin(), (lp).end(), std::greater<pair<long long, long long>>());
  long long maxi = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != '1') {
      maxi = max(maxi, (long long)i + 1);
    }
  }
  for (int i = 0; i < n - 1; i++) {
    auto c = lp[i];
    if (c.first > 0) {
      fu_union(c.second, c.second + 1);
      maxi = max(maxi, ((long long)c.first) * ((long long)B[T[c.second]]));
    }
  }
  cout << maxi << "\n";
}
