#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 5;
const int oo = 0x3f3f3f3f;
int N;
long long ans = 0;
char s[MAXN], vaild[MAXN];
namespace SAM {
int son[MAXN][26], pre[MAXN], step[MAXN], p, np, q, nq, now = 1, cnt = 1;
int mark[MAXN], ord[MAXN], val[MAXN];
void extend(int nxt) {
  p = now;
  np = ++cnt;
  now = np;
  step[np] = step[p] + 1;
  for (; p && !son[p][nxt]; p = pre[p]) son[p][nxt] = np;
  if (!p)
    pre[np] = 1;
  else {
    q = son[p][nxt];
    if (step[q] == step[p] + 1)
      pre[np] = q;
    else {
      step[nq = ++cnt] = step[p] + 1;
      memcpy(son[nq], son[q], sizeof(son[q]));
      pre[nq] = pre[q];
      pre[np] = pre[q] = nq;
      for (; son[p][nxt] == q; p = pre[p]) son[p][nxt] = nq;
    }
  }
}
void Topsort() {
  for (int i = 1; i <= cnt; i++) mark[step[i]]++;
  for (int i = 1; i <= N; i++) mark[i] += mark[i - 1];
  for (int i = cnt; i >= 1; i--) ord[mark[step[i]]--] = i;
  for (int i = cnt; i >= 1; i--) {
    int o = ord[i];
    val[pre[o]] += val[o];
    ans = max(ans, 1LL * val[o] * step[o]);
  }
}
}  // namespace SAM
using namespace SAM;
namespace solution {
void Prepare() {
  scanf("%d%s%s", &N, s + 1, vaild + 1);
  for (int i = 1; i <= N; i++) {
    extend(s[i] - 'a');
    if (vaild[i] == '0') val[np] = 1;
  }
}
void Solve() {
  Topsort();
  printf("%lld\n", ans);
}
}  // namespace solution
int main() {
  using namespace solution;
  Prepare();
  Solve();
  return 0;
}
