#include <bits/stdc++.h>
using namespace std;
const int MaxC = 256;
const int MAX = 300020;
int offset, n;
int suma[MAX], tmp[MAX];
int rank1[MAX], rank2[MAX];
int c[MAX], p[MAX], lcp[MAX];
bool different(int a, int b) {
  if (b + offset >= n) return true;
  if (rank1[a] != rank1[b]) return true;
  if (rank2[a] != rank2[b]) return true;
  return false;
}
void suffix_array(string &text) {
  n = text.size();
  for (int i = 0; i < MaxC; i++) suma[i] = 0;
  for (int i = 0; i < n; i++) suma[text[i]]++;
  for (int i = 0; i < MaxC; i++) suma[i + 1] += suma[i];
  for (int i = 0; i < n; i++) c[i] = suma[text[i]] - 1;
  for (offset = 1; offset <= n; offset <<= 1) {
    for (int i = 0; i < n; i++) {
      rank1[i] = c[i];
      rank2[i] = (i + offset < n) ? c[i + offset] : 0;
    }
    fill(suma, suma + n, 0);
    for (int i = 0; i < n; i++) suma[rank2[i]]++;
    for (int i = 0; i < n; i++) suma[i + 1] += suma[i];
    for (int i = 0; i < n; i++) tmp[--suma[rank2[i]]] = i;
    fill(suma, suma + n, 0);
    for (int i = 0; i < n; i++) suma[rank1[i]]++;
    for (int i = 0; i < n; i++) suma[i + 1] += suma[i];
    for (int i = n - 1; i >= 0; i--) p[--suma[rank1[tmp[i]]]] = tmp[i];
    c[p[0]] = 0;
    for (int i = 1; i < n; i++) {
      c[p[i]] = c[p[i - 1]];
      if (different(p[i], p[i - 1])) c[p[i]]++;
    }
  }
  offset = 0;
  for (int i = 0; i < n; i++) {
    if (c[i] == n - 1) {
      lcp[n - 1] = 0;
      continue;
    }
    while (text[i + offset] == text[p[c[i] + 1] + offset]) offset++;
    lcp[c[i]] = offset;
    if (offset > 0) offset--;
  }
}
int sum[MAX];
int get(int i, int j) {
  if (i == 0) {
    return sum[j];
  }
  return sum[j] - sum[i - 1];
}
int l[MAX], r[MAX];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int sz;
  cin >> sz;
  string s;
  cin >> s;
  string b;
  cin >> b;
  reverse(s.begin(), s.end());
  reverse(b.begin(), b.end());
  suffix_array(s);
  stack<int> st;
  for (int i = 0; i < n; i++) {
    sum[i] = (b[p[i]] - '0') ^ 1;
    if (i > 0) {
      sum[i] += sum[i - 1];
    }
    while (!st.empty() && lcp[st.top()] >= lcp[i]) {
      st.pop();
    }
    if (st.empty()) {
      l[i] = 0;
    } else {
      l[i] = st.top() + 1;
    }
    st.push(i);
  }
  st = stack<int>();
  for (int i = n - 1; i >= 0; i--) {
    while (!st.empty() && lcp[i] <= lcp[st.top()]) {
      st.pop();
    }
    if (st.empty()) {
      r[i] = n - 1;
    } else {
      r[i] = st.top();
    }
    st.push(i);
  }
  long long sol = 0ll;
  for (int i = 0; i < n; i++) {
    sol = max(sol, (long long)lcp[i] * (long long)get(l[i], r[i]));
    if (b[p[i]] == '1' || lcp[i] == n - p[i]) {
      continue;
    }
    sol = max(sol, (long long)(n - p[i]));
  }
  cout << sol << '\n';
}
