#include <bits/stdc++.h>
using namespace std;
struct SA {
  int p[262192];
  int cnt[262192];
  int c[19][262192];
  int logs[262192];
  const int alphabet = 256;
  int n;
  void init() {
    memset(p, 0, sizeof(p));
    memset(cnt, 0, sizeof(cnt));
    memset(c, 0, sizeof(c));
  }
  void sort_cyclic_shifts(string &s) {
    int h, i;
    n = (int)s.size();
    for (i = 2; i < 262192; ++i) logs[i] = logs[i / 2] + 1;
    for (i = 0; i < n; i++) cnt[(int)s[i]]++;
    for (i = 1; i < alphabet; i++) cnt[i] += cnt[i - 1];
    for (i = 0; i < n; i++) p[--cnt[(int)s[i]]] = i;
    c[0][p[0]] = 0;
    int classes = 1;
    for (i = 1; i < n; i++) {
      if (s[p[i]] != s[p[i - 1]]) classes++;
      c[0][p[i]] = classes - 1;
    }
    vector<int> pn(n), cn(n);
    for (h = 0; (1 << h) < n; ++h) {
      for (i = 0; i < n; i++) {
        pn[i] = p[i] - (1 << h);
        if (pn[i] < 0) pn[i] += n;
      }
      fill(cnt, cnt + classes, 0);
      for (i = 0; i < n; i++) cnt[c[h][pn[i]]]++;
      for (i = 1; i < classes; i++) cnt[i] += cnt[i - 1];
      for (i = n - 1; i >= 0; i--) p[--cnt[c[h][pn[i]]]] = pn[i];
      cn[p[0]] = 0;
      classes = 1;
      for (i = 1; i < n; i++) {
        pair<int, int> cur = {c[h][p[i]], c[h][(p[i] + (1 << h)) % n]};
        pair<int, int> prev = {c[h][p[i - 1]], c[h][(p[i - 1] + (1 << h)) % n]};
        if (cur != prev) ++classes;
        cn[p[i]] = classes - 1;
      }
      for (i = 0; i < n; ++i) {
        c[h + 1][i] = cn[i];
      }
    }
  }
  int lcp(int i, int j) {
    int ans = 0, k;
    for (k = logs[n]; k >= 0; k--) {
      if (c[k][i] == c[k][j]) {
        ans += 1 << k;
        i += 1 << k;
        j += 1 << k;
      }
    }
    return ans;
  }
};
struct segt {
  int L, R, ans, len;
};
segt tree[262192 << 1];
segt pullup(segt t1, segt t2) {
  segt ret;
  ret.len = t1.len + t2.len;
  ret.ans = max(max(t1.ans, t2.ans), t1.R + t2.L);
  ret.L = t1.L + (t1.L == t1.len) * t2.L;
  ret.R = t2.R + (t2.R == t2.len) * t1.R;
  return ret;
}
void build(int l, int r, int index) {
  if (l > r) return;
  if (l == r) {
    tree[index].len = 1;
    return;
  }
  build(l, ((l + r) >> 1), (index << 1));
  build(((l + r) >> 1) + 1, r, (index << 1 | 1));
  tree[index].len = tree[(index << 1)].len + tree[(index << 1 | 1)].len;
}
void update(int pt, int l, int r, int index, int val) {
  if (l > r || pt < l || pt > r) return;
  if (l == r) {
    tree[index].ans += val;
    tree[index].L += val;
    tree[index].R += val;
    return;
  }
  if (pt <= ((l + r) >> 1)) {
    update(pt, l, ((l + r) >> 1), (index << 1), val);
  } else {
    update(pt, ((l + r) >> 1) + 1, r, (index << 1 | 1), val);
  }
  tree[index] = pullup(tree[(index << 1)], tree[(index << 1 | 1)]);
}
SA sa;
int N;
char st[262192];
char pos[262192];
string s;
vector<int> valid;
vector<pair<int, int>> height;
int main() {
  int i, j;
  long long ans = 0;
  scanf("%d", &N);
  scanf("%s", st);
  scanf("%s", pos);
  for (i = 0; i < N; ++i) {
    if (pos[i] == '0') {
      ans = max(ans, 1ll * (i + 1));
    }
  }
  reverse(st, st + N);
  reverse(pos, pos + N);
  s = string(st);
  s += '$';
  sa.sort_cyclic_shifts(s);
  for (i = 0; i < sa.n; ++i) {
    if (sa.p[i] < N && pos[sa.p[i]] == '0') {
      valid.push_back(sa.p[i]);
    }
  }
  for (i = 0; i < (int)valid.size() - 1; ++i) {
    height.emplace_back(sa.lcp(valid[i], valid[i + 1]), i);
    if (height.back().first == 0) height.pop_back();
  }
  int sz = height.size();
  build(0, sz - 1, 1);
  for (auto h : height) {
    update(h.second, 0, sz - 1, 1, 1);
  }
  sort(height.begin(), height.end());
  int mx = height.empty() ? 0 : height.back().first;
  for (i = 1, j = 0; i <= mx; ++i) {
    ans = max(ans, 1ll * (tree[1].ans + 1) * i);
    while (j < sz && height[j].first == i) {
      update(height[j].second, 0, sz - 1, 1, -1);
      ++j;
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
