#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
const int M = 2 * 100005;
struct state {
  int len, link, fpos;
  bool isclone;
  map<char, int> next;
  vector<int> inv_link;
} st[2 * M];
int sz, last;
int cnt[2 * M];
bool terminal[2 * M];
void initialize() {
  for (int i = 0; i < (sz + 1); i++) {
    st[i].next.clear();
    st[i].inv_link.clear();
  }
  sz = last = 0;
  st[0].len = 0;
  st[0].link = -1;
}
char vis[M];
void build_automata(char c) {
  int cur = ++sz;
  cnt[cur] = 1;
  st[cur].len = st[last].len + 1;
  if (vis[st[cur].len - 1] == '1') cnt[cur] = 0;
  st[cur].isclone = 0;
  st[cur].fpos = st[cur].len;
  int p;
  for (p = last; p != -1 && !st[p].next[c]; p = st[p].link) {
    st[p].next[c] = cur;
  }
  if (p == -1) {
    st[cur].link = 0;
  } else {
    int q = st[p].next[c];
    if (st[p].len + 1 == st[q].len) {
      st[cur].link = q;
    } else {
      int clone = ++sz;
      cnt[clone] = 0;
      st[clone] = st[q];
      st[clone].len = st[p].len + 1;
      st[clone].isclone = 1;
      for (; p != -1 && st[p].next[c] == q; p = st[p].link) {
        st[p].next[c] = clone;
      }
      st[q].link = st[cur].link = clone;
    }
  }
  last = cur;
}
void cal_terminal() {
  int now = last;
  while (now != -1) {
    terminal[now] = 1;
    now = st[now].link;
  }
}
char s[M];
vector<int> v[M];
void cal_occuarence(int len) {
  long long mx = 0;
  for (int i = sz; i >= 1; i--) {
    v[st[i].len].push_back(i);
  }
  for (int i = len; i >= 1; i--) {
    for (auto x : v[i]) {
      if (st[x].link == -1) continue;
      cnt[st[x].link] += cnt[x];
      mx = max(mx, (long long)st[x].len * (long long)cnt[x]);
    }
    v[i].clear();
  }
  printf("%lld\n", (mx));
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  scanf("%s", vis);
  initialize();
  int len = n;
  for (int i = 0; i < (len); i++) build_automata(s[i] - 'a');
  cal_occuarence(len);
  return 0;
}
