#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int n, last, num, len[N], link[N], child[N][26];
int sz[N];
vector<int> g[N];
string s, r;
void sa_init() {
  last = num = 0, len[0] = 0, link[0] = -1;
  memset(child, 0, sizeof(child));
}
void sa_extend(int c, int vt) {
  int cur = ++num, p = last;
  len[cur] = len[last] + 1, sz[cur] = (r[vt] == '0');
  while (p != -1 && child[p][c] == 0) child[p][c] = cur, p = link[p];
  if (p == -1)
    link[cur] = 0;
  else {
    int q = child[p][c];
    if (len[p] + 1 == len[q])
      link[cur] = q;
    else {
      int clone = ++num;
      len[clone] = len[p] + 1;
      copy(child[q], child[q] + 26, child[clone]);
      link[clone] = link[q];
      while (p != -1 && child[p][c] == q) child[p][c] = clone, p = link[p];
      link[cur] = link[q] = clone;
    }
  }
  last = cur;
}
void dfs(int u) {
  for (auto v : g[u]) {
    dfs(v);
    sz[u] += sz[v];
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> s >> r;
  sa_init();
  for (int i = 0; i < s.size(); i++) sa_extend(s[i] - 'a', i);
  for (int i = 1; i <= num; i++) g[link[i]].emplace_back(i);
  dfs(0);
  long long ans = 0;
  for (int i = 1; i <= num; i++) ans = max(ans, 1ll * len[i] * sz[i]);
  cout << ans;
  return 0;
}
