#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 20;
const int MAXL = 20;
int n, stp, mv, suffix[MAXN], tmp[MAXN];
int sum[MAXN], cnt[MAXN], Rank[MAXL][MAXN];
char str[MAXN];
inline bool Equal(const int &u, const int &v) {
  if (!stp) return str[u] == str[v];
  if (Rank[stp - 1][u] != Rank[stp - 1][v]) return false;
  int a = u + mv < n ? Rank[stp - 1][u + mv] : -1;
  int b = v + mv < n ? Rank[stp - 1][v + mv] : -1;
  return a == b;
}
void update() {
  int i, rnk;
  for (i = 0; i < n; i++) sum[i] = 0;
  for (i = rnk = 0; i < n; i++) {
    suffix[i] = tmp[i];
    if (i && !Equal(suffix[i], suffix[i - 1])) {
      Rank[stp][suffix[i]] = ++rnk;
      sum[rnk + 1] = sum[rnk];
    } else
      Rank[stp][suffix[i]] = rnk;
    sum[rnk + 1]++;
  }
}
void Sort() {
  int i;
  for (i = 0; i < n; i++) cnt[i] = 0;
  memset(tmp, -1, sizeof tmp);
  for (i = 0; i < mv; i++) {
    int idx = Rank[stp - 1][n - i - 1];
    int x = sum[idx];
    tmp[x + cnt[idx]] = n - i - 1;
    cnt[idx]++;
  }
  for (i = 0; i < n; i++) {
    int idx = suffix[i] - mv;
    if (idx < 0) continue;
    idx = Rank[stp - 1][idx];
    int x = sum[idx];
    tmp[x + cnt[idx]] = suffix[i] - mv;
    cnt[idx]++;
  }
  update();
  return;
}
inline bool cmp(const int &a, const int &b) {
  if (str[a] != str[b]) return str[a] < str[b];
  return false;
}
void SortSuffix() {
  int i;
  n = strlen(str);
  for (i = 0; i < n; i++) tmp[i] = i;
  sort(tmp, tmp + n, cmp);
  stp = 0;
  update();
  ++stp;
  for (mv = 1; mv < n; mv <<= 1) {
    Sort();
    stp++;
  }
  stp--;
  for (i = 0; i <= stp; i++) Rank[i][n] = -1;
}
inline int lcp(int u, int v) {
  if (u == v) return n - u;
  int ret, i;
  for (ret = 0, i = stp; i >= 0; i--) {
    if (Rank[i][u] == Rank[i][v]) {
      ret += 1 << i;
      u += 1 << i;
      v += 1 << i;
    }
  }
  return ret;
}
long long st[400000], top;
long long histogram(int a[], int n) {
  top = 0;
  long long i = 1, res = 0;
  a[++n] = 0, st[top] = 0;
  while (i <= n) {
    if (a[st[top]] <= a[i])
      st[++top] = i++;
    else {
      long long bar = a[st[top]];
      --top;
      res = max(res, (i - st[top] - 1) * bar + bar);
    }
  }
  return res;
}
int a[MAXN];
char valid[MAXN];
int main() {
  cin >> n;
  cin >> str;
  cin >> valid;
  reverse(str, str + n);
  reverse(valid, valid + n);
  SortSuffix();
  vector<int> ans;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (valid[suffix[i]] == '0') {
      ans.push_back(suffix[i]);
      res = max(res, 0ll + n - suffix[i]);
    }
  }
  a[0] = 0;
  for (int i = 1; i < ans.size(); i++) {
    a[i] = lcp(ans[i], ans[i - 1]);
  }
  cout << max(res, 0ll + histogram(a, ans.size())) << endl;
  return 0;
}
