#include <bits/stdc++.h>
using namespace std;
const int mxn = 200002;
const int mxlg = 22;
long long n;
int suffix[mxlg][mxn], Rank[mxn], lcp[mxn];
struct entry {
  int nr[2], p;
} L[mxn];
bool cmp(entry const a, entry const b) {
  return a.nr[0] == b.nr[0]  ? (a.nr[1] < b.nr[1] ? true : false)
         : a.nr[0] < b.nr[0] ? true
                             : false;
}
string str;
int lg, ln, stp;
void build_suffix_array() {
  ln = str.size();
  for (int i = 0; i < ln; i++) {
    suffix[0][i] = str[i] - 'a';
  }
  for (stp = 1, lg = 1; (lg >> 1) < ln; stp++, lg <<= 1) {
    for (int i = 0; i < ln; i++) {
      L[i].nr[0] = suffix[stp - 1][i];
      L[i].nr[1] = i + lg < ln ? suffix[stp - 1][i + lg] : -1;
      L[i].p = i;
    }
    sort(L, L + ln, cmp);
    for (int i = 0; i < ln; i++) {
      suffix[stp][L[i].p] =
          i > 0 && L[i].nr[0] == L[i - 1].nr[0] && L[i].nr[1] == L[i - 1].nr[1]
              ? suffix[stp][L[i - 1].p]
              : i;
    }
  }
  stp -= 1;
  for (int i = 0; i < ln; i++) {
    Rank[suffix[stp][i]] = i;
  }
}
void big_o_LCP() {
  for (int i = 0; i < ln; i++) {
    Rank[suffix[stp][i]] = i;
  }
  int now = 0;
  for (int i = 0; i < ln; i++) {
    now = max(now - 1, 0);
    if (suffix[stp][i] == ln - 1) {
      now = 0;
      continue;
    }
    int j = Rank[suffix[stp][i] + 1];
    while (i + now < ln && j + now < ln && str[i + now] == str[j + now]) now++;
    lcp[suffix[stp][i]] = now;
  }
}
int nlogn_lcp(int x, int y) {
  int ret = 0;
  if (x == y) return ln - x;
  for (int k = stp; k >= 0 && x < ln && y < ln; k--) {
    if (suffix[k][x] == suffix[k][y]) {
      x += (1 << k);
      y += (1 << k);
      ret += (1 << k);
    }
  }
  return ret;
}
int ar[mxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  cin >> str;
  string s;
  cin >> s;
  reverse(str.begin(), str.end());
  reverse(s.begin(), s.end());
  ln = str.size();
  build_suffix_array();
  for (int i = 0; i < n; i++) {
    ar[i] = s[Rank[i]] - '0';
  }
  for (int i = 1; i < n; i++) ar[i] += ar[i - 1];
  for (int i = 1; i < ln; i++) {
    lcp[i - 1] = nlogn_lcp(Rank[i], Rank[i - 1]);
  }
  long long left[mxn], right[mxn];
  stack<pair<int, int> > st;
  st.push({-1e9, -1});
  for (int i = 0; i < n - 1; i++) {
    while (st.top().first >= lcp[i]) st.pop();
    left[i] = st.top().second;
    st.push({lcp[i], i});
  }
  st = stack<pair<int, int> >();
  st.push({-1e9, n - 1});
  for (int i = n - 2; i >= 0; i--) {
    while (st.top().first >= lcp[i]) st.pop();
    right[i] = st.top().second;
    st.push({lcp[i], i});
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    long long ok = right[i] - left[i];
    long long tmp = ar[right[i]];
    if (left[i] > 0) tmp -= ar[left[i]];
    ok -= tmp;
    ok = max(ok, 0LL);
    ans = max(ans, 1LL * ok * lcp[i]);
  }
  for (int i = 0; i < ln; i++) {
    if (s[Rank[i]] != '1') {
      ans = max(ans, (long long)n - Rank[i]);
    }
  }
  cout << ans << endl;
  str.clear();
  s.clear();
  return 0;
}
