#include <bits/stdc++.h>
using namespace std;
int const N = 5e5, oo = 1e9;
int mod = oo + 7;
long long const OO = 1e18;
int rnk[N][20], p[N], counting[N], lcp[N], rev[N];
void suff_arr(string s) {
  s += char('a' - 1);
  int n = s.size();
  for (int i = 0; i < n; i++) counting[s[i] - 'a' + 1]++;
  for (int i = 1; i < 27; i++) counting[i] += counting[i - 1];
  for (int i = n - 1; i > -1; i--) p[--counting[s[i] - 'a' + 1]] = i;
  int r = 0;
  rnk[p[0]][0] = 0;
  for (int i = 1; i < n; i++) {
    if (s[p[i - 1]] != s[p[i]]) r++;
    rnk[p[i]][0] = r;
  }
  for (int k = 0; (1 << k) < n; k++) {
    fill(counting, counting + r + 1, 0);
    vector<int> np(n);
    for (int i = 0; i < n; i++) {
      np[i] = p[i] - (1 << k);
      if (np[i] < 0) np[i] += n;
    }
    for (int i = 0; i < n; i++) counting[rnk[np[i]][k]]++;
    for (int i = 1; i <= r; i++) counting[i] += counting[i - 1];
    for (int i = n - 1; i > -1; i--) p[--counting[rnk[np[i]][k]]] = np[i];
    r = 0;
    rnk[p[0]][k + 1] = r;
    for (int i = 1; i < n; i++) {
      pair<int, int> cur = {rnk[p[i]][k], rnk[(p[i] + (1 << k)) % n][k]};
      pair<int, int> pre = {rnk[p[i - 1]][k],
                            rnk[(p[i - 1] + (1 << k)) % n][k]};
      if (cur != pre) r++;
      rnk[p[i]][k + 1] = r;
    }
  }
  for (int i = 0; i < n; i++) p[i] = p[i + 1];
}
void build(string &s) {
  int k = 0;
  int n = s.size();
  for (int i = 0; i < n; i++) rev[p[i]] = i;
  for (int i = 0; i < n; i++) {
    if (rev[i] == n - 1) {
      k = 0;
      continue;
    }
    int r = p[rev[i] + 1];
    while (i + k < n && r + k < n && s[i + k] == s[r + k]) k++;
    lcp[rev[i]] = k;
    if (k) k--;
  }
}
int dp2[N][19], lg[N], lft[N], rgt[N], n;
string s, f;
int get(int l, int r) {
  int s = 0;
  for (int i = lg[n]; i > -1; i--) {
    if (l + (1 << i) <= r + 1) {
      s += dp2[l][i];
      l += (1 << i);
    }
  }
  return s;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  lg[1] = 0;
  for (int i = 2; i < N; i++) lg[i] = lg[i / 2] + 1;
  cin >> n;
  cin >> s >> f;
  reverse(s.begin(), s.end());
  reverse(f.begin(), f.end());
  suff_arr(s);
  build(s);
  for (int i = 0; i < n; i++) dp2[rev[i]][0] = f[i] - '0';
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 0; i + (1 << (j - 1)) < n; i++)
      dp2[i][j] = dp2[i][j - 1] + dp2[i + (1 << (j - 1))][j - 1];
  stack<pair<int, int> > st;
  for (int i = 0; i < n - 1; i++) {
    if (st.empty()) {
      st.push({i, lcp[i]});
      lft[i] = 0;
      continue;
    }
    while (!st.empty() && st.top().second >= lcp[i]) st.pop();
    if (st.empty())
      lft[i] = 0;
    else
      lft[i] = st.top().first + 1;
    st.push({i, lcp[i]});
  }
  while (!st.empty()) st.pop();
  for (int i = n - 2; i > -1; i--) {
    if (st.empty()) {
      st.push({i, lcp[i]});
      rgt[i] = n - 1;
      continue;
    }
    while (!st.empty() && st.top().second >= lcp[i]) st.pop();
    if (st.empty())
      rgt[i] = n - 1;
    else
      rgt[i] = st.top().first;
    st.push({i, lcp[i]});
  }
  long long mx = 0;
  for (int i = 0; i < n - 1; i++) {
    int val = lcp[i];
    int msl = lft[i], msr = rgt[i];
    mx = max(mx, val * 1LL * (msr - msl + 1 - get(msl, msr)));
  }
  for (int i = 0; i < n; i++) mx = max(mx, (1 - f[i] + '0') * 1LL * (n - i));
  cout << mx << endl;
  return 0;
}
