#include <bits/stdc++.h>
using namespace std;
struct tup {
  long f, s, id;
};
void range(long arr[], long res[], long suf[], long n, string z) {
  long pref[n + 1];
  pref[0] = 0;
  for (long i = 1; i <= n; i++) pref[i] = pref[i - 1] + (z[suf[i - 1]] - '0');
  stack<pair<long, long>> st;
  long id;
  for (long i = 1; i < n; i++) {
    while (!st.empty() && st.top().first > arr[i]) {
      id = st.top().second;
      res[id] = i - id - pref[i] + pref[id];
      st.pop();
    }
    st.push({arr[i], i});
  }
  while (!st.empty()) {
    id = st.top().second;
    res[id] = n - id - pref[n] + pref[id];
    st.pop();
  }
  for (long i = n - 1; i >= 1; i--) {
    while (!st.empty() && st.top().first > arr[i]) {
      id = st.top().second;
      res[id] += id - i - pref[id] + pref[i];
      st.pop();
    }
    st.push({arr[i], i});
  }
  while (!st.empty()) {
    id = st.top().second;
    res[id] += id - pref[id];
    st.pop();
  }
}
bool cmp1(tup a, tup b) { return a.f < b.f; }
long cmp2(tup a, tup b) { return a.s < b.s; }
void sufArr(string s, long suf[], long lcp[]) {
  long n = s.size(), l = log2(n);
  if (n == 1) {
    suf[0] = 0;
    lcp[0] = -1;
    return;
  }
  long p[l + 2][n];
  long pow[l + 2];
  pow[0] = 1;
  for (long i = 1; i < l + 2; i++) pow[i] = pow[i - 1] * 2;
  for (long i = 0; i < n; i++) p[0][i] = s[i] - 'a';
  tup t[n];
  for (long i = 1; pow[i - 1] < n; i++) {
    for (long j = 0; j < n; j++) {
      t[j].id = j;
      t[j].f = p[i - 1][j];
      t[j].s = (j + pow[i - 1] < n ? p[i - 1][j + pow[i - 1]] : -1);
    }
    stable_sort(t, t + n, cmp2);
    stable_sort(t, t + n, cmp1);
    for (long j = 0; j < n; j++)
      p[i][t[j].id] = ((j > 0 && t[j].f == t[j - 1].f && t[j].s == t[j - 1].s)
                           ? p[i][t[j - 1].id]
                           : j);
  }
  for (long i = 0; i < n; i++) suf[p[l + 1][i]] = i;
  for (long m = 1; m < n; m++) {
    long j = suf[m - 1], i = suf[m];
    lcp[m] = 0;
    for (long k = l + 1; k >= 0 && i < n && j < n; k--) {
      if (p[k][i] == p[k][j] && p[k][i] != -1) {
        lcp[m] += pow[k];
        i += pow[k];
        j += pow[k];
      }
    }
  }
}
int main() {
  long n;
  cin >> n;
  string s, z;
  cin >> s >> z;
  reverse(s.begin(), s.end());
  reverse(z.begin(), z.end());
  long* suf = new long[n];
  long* lcp = new long[n];
  lcp[0] = -1;
  sufArr(s, suf, lcp);
  long* len = new long[n];
  range(lcp, len, suf, n, z);
  long i = 0;
  long long r = 0;
  while (i < n && z[i] == '1') i++;
  r = n - i;
  for (long i = 1; i < n; i++) r = max((lcp[i] * 1ll * len[i]), r);
  cout << r << "\n";
}
