#include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= 1000000007;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
  }
  return res;
}
int gcd(int x, int y) {
  while (y) {
    int t = x % y;
    x = y;
    y = t;
  }
  return x;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
namespace FastIO {
bool IOError = 0;
inline char NextChar() {
  static char buf[200010], *p1 = buf + 200010, *pend = buf + 200010;
  if (p1 == pend) {
    p1 = buf;
    pend = buf + fread(buf, 1, 200010, stdin);
    if (pend == p1) {
      IOError = 1;
      return -1;
    }
  }
  return *p1++;
}
inline bool Blank(char c) {
  return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
inline void read(int &x) {
  bool sign = 0;
  char ch = NextChar();
  x = 0;
  for (; Blank(ch); ch = NextChar())
    ;
  if (IOError) return;
  if (ch == '-') sign = 1, ch = NextChar();
  for (; ch >= '0' && ch <= '9'; ch = NextChar()) x = x * 10 + ch - '0';
  if (sign) x = -x;
}
inline void read(long long &x) {
  bool sign = 0;
  char ch = NextChar();
  x = 0;
  for (; Blank(ch); ch = NextChar())
    ;
  if (IOError) return;
  if (ch == '-') sign = 1, ch = NextChar();
  for (; ch >= '0' && ch <= '9'; ch = NextChar()) x = x * 10 + ch - '0';
  if (sign) x = -x;
}
inline void read(double &x) {
  bool sign = 0;
  char ch = NextChar();
  x = 0;
  for (; Blank(ch); ch = NextChar())
    ;
  if (IOError) return;
  if (ch == '-') sign = 1, ch = NextChar();
  for (; ch >= '0' && ch <= '9'; ch = NextChar()) x = x * 10 + ch - '0';
  if (ch == '.') {
    double tmp = 1;
    ch = NextChar();
    for (; ch >= '0' && ch <= '9'; ch = NextChar())
      tmp /= 10.0, x += tmp * (ch - '0');
  }
  if (sign) x = -x;
}
inline void read(char *s) {
  char ch = NextChar();
  for (; Blank(ch); ch = NextChar())
    ;
  if (IOError) return;
  for (; !Blank(ch) && !IOError; ch = NextChar()) *s++ = ch;
  *s = 0;
}
inline void read(char &c) {
  for (c = NextChar(); Blank(c); c = NextChar())
    ;
  if (IOError) {
    c = -1;
    return;
  }
}
}  // namespace FastIO
using namespace FastIO;
int n;
int ch[501000][26], par[501000], val[501000], tot;
int cnt[501000];
int last[501000];
char s[501000], t[501000];
int creat(int v) {
  int k = ++tot;
  par[k] = 0;
  val[k] = v;
  cnt[k] = 0;
  return k;
}
int extend(int p, int c, int o) {
  int np = creat(val[p] + 1);
  cnt[np] = o;
  while (p && ch[p][c] == 0) ch[p][c] = np, p = par[p];
  if (!p)
    par[np] = 1;
  else {
    int q = ch[p][c];
    if (val[q] == val[p] + 1)
      par[np] = q;
    else {
      int nq = creat(val[p] + 1);
      memcpy(ch[nq], ch[q], sizeof ch[q]);
      par[nq] = par[q];
      par[q] = par[np] = nq;
      while (p && ch[p][c] == q) ch[p][c] = nq, p = par[p];
    }
  }
  return np;
}
vector<int> g[501000];
void dfs(int u) {
  for (auto v : g[u]) {
    dfs(v);
    cnt[u] += cnt[v];
  }
}
int main() {
  read(n);
  read(s);
  read(t);
  last[0] = ++tot;
  for (int i = 1; i <= n; i++) {
    last[i] = extend(last[i - 1], s[i - 1] - 'a', 1 ^ (t[i - 1] - '0'));
  }
  for (int i = tot; i; i--) {
    g[par[i]].push_back(i);
  }
  dfs(1);
  long long ans = 0;
  for (int i = 1; i <= tot; i++) {
    chkmax(ans, 1LL * val[i] * cnt[i]);
  }
  cout << ans << '\n';
  return 0;
}
