#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 * 2 + 5;
struct node {
  map<char, int> next;
  int link, len;
  bool cl, ed;
};
int sz, last;
int n;
char str[MAXN];
char a[MAXN];
node s[2 * MAXN];
int cnt[2 * MAXN];
void init() {
  sz = last = 0;
  s[sz].link = -1;
  sz++;
}
void add_char(char c, bool pr) {
  int cur = sz++;
  int p = last;
  s[cur].len = s[last].len + 1;
  while (p != -1 && !s[p].next.count(c)) {
    s[p].next[c] = cur;
    p = s[p].link;
  }
  if (p == -1) {
    s[cur].link = 0;
  } else {
    int q = s[p].next[c];
    if (s[p].len + 1 == s[q].len) {
      s[cur].link = q;
    } else {
      int clone = sz++;
      s[clone].next = s[q].next;
      s[clone].len = s[p].len + 1;
      s[clone].link = s[q].link;
      s[clone].cl = 1;
      s[clone].ed = 0;
      while (p != -1 && s[p].next[c] == q) {
        s[p].next[c] = clone;
        p = s[p].link;
      }
      s[cur].link = s[q].link = clone;
    }
  }
  s[cur].cl = 0;
  s[cur].ed = pr;
  last = cur;
}
void read_input() {
  scanf("%d", &n);
  scanf("%s", str);
  scanf("%s", a);
  for (int i = 0; i < n; i++) {
    add_char(str[i], a[i] == '1');
  }
}
struct state {
  int v, depth;
  state(int _v, int _depth) {
    v = _v;
    depth = _depth;
  }
  bool operator<(state other) const { return depth > other.depth; }
};
vector<state> vr;
bool vis[2 * MAXN];
void dfs(int v, int dpt) {
  vis[v] = true;
  if (v != 0 && !s[v].cl && !s[v].ed) {
    cnt[v]++;
  }
  if (v != 0) {
    vr.push_back(state(v, dpt));
  }
  for (auto x : s[v].next) {
    if (vis[x.second]) continue;
    dfs(x.second, dpt + 1);
  }
}
long long answer;
bool vis2[2 * MAXN];
void rec(int v, int dpt) {
  if (v != 0) {
    answer = max(answer, 1LL * cnt[v] * dpt);
  }
  for (auto x : s[v].next) {
    if (s[v].len == s[x.second].len - 1) rec(x.second, dpt + 1);
  }
}
void solve() {
  dfs(0, 0);
  sort(vr.begin(), vr.end());
  for (auto x : vr) {
    cnt[s[x.v].link] += cnt[x.v];
  }
  rec(0, 0);
  printf("%lld\n", answer);
}
int main() {
  init();
  read_input();
  solve();
  return 0;
}
