#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C &a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C &a4, C b4) {
  a4 = max(a4, b4);
}
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
struct SuffixAutomaton {
  vector<map<char, long long>> edges;
  vector<long long> link, length;
  long long last;
  SuffixAutomaton(string s) : edges(1), link{-1}, length(1), last(0) {
    for (long long i = 0; i < ((long long)(s).size()); i++) {
      edges.push_back(map<char, long long>());
      length.push_back(i + 1);
      link.push_back(0);
      long long r = ((long long)(edges).size()) - 1, p = last;
      while (p >= 0 && edges[p].find(s[i]) == edges[p].end()) {
        edges[p][s[i]] = r;
        p = link[p];
      }
      if (p != -1) {
        long long q = edges[p][s[i]];
        if (length[p] + 1 == length[q]) {
          link[r] = q;
        } else {
          edges.push_back(edges[q]);
          length.push_back(length[p] + 1);
          link.push_back(link[q]);
          long long qq = ((long long)(edges).size()) - 1;
          link[q] = qq;
          link[r] = qq;
          while (p >= 0 && edges[p][s[i]] == q) {
            edges[p][s[i]] = qq;
            p = link[p];
          }
        }
      }
      last = r;
    }
  }
};
void dfs(long long v, vector<vector<long long>> &tree, vector<long long> &co,
         vector<bool> &prefix) {
  co[v] += prefix[v];
  for (long long son : tree[v]) {
    dfs(son, tree, co, prefix);
    co[v] += co[son];
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  if (0) cout << fixed << setprecision(6);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  string mask;
  cin >> mask;
  SuffixAutomaton aut(s);
  long long idx = 0;
  long long m = aut.edges.size();
  vector<bool> prefix(m);
  for (long long i = (0); i <= ((long long)(n)-1); i++) {
    idx = aut.edges[idx][s[i]];
    if (mask[i] != '1') {
      prefix[idx] = 1;
    }
  }
  vector<vector<long long>> tree(m);
  for (long long i = (0); i <= ((long long)(m)-1); i++) {
    long long o = aut.link[i];
    if (o != -1) tree[o].push_back(i);
  }
  vector<long long> co(m);
  dfs(0, tree, co, prefix);
  long long res = 0;
  for (long long i = (0); i <= ((long long)(m)-1); i++) {
    maxi(res, aut.length[i] * co[i]);
  }
  cout << res << endl;
  return 0;
}
