#include <bits/stdc++.h>
const long long MAX = 200000 + 10;
using namespace std;
void radix(long long *str, long long *a, long long *b, long long n,
           long long m) {
  static long long count[MAX];
  memset(count, 0, sizeof(count));
  long long i;
  for (i = 0; i < n; i++) count[str[a[i]]]++;
  for (i = 1; i <= m; i++) count[i] += count[i - 1];
  for (i = n - 1; i >= 0; i--) b[--count[str[a[i]]]] = a[i];
}
void s_a(long long *str, long long *sa, long long n, long long m) {
  static long long Rank[MAX], a[MAX], b[MAX];
  long long i, j;
  for (i = 0; i < n; i++) Rank[i] = i;
  radix(str, Rank, sa, n, m);
  Rank[sa[0]] = 0;
  for (i = 1; i < n; i++)
    Rank[sa[i]] = Rank[sa[i - 1]] + (str[sa[i]] != str[sa[i - 1]]);
  for (i = 0; (1 << i) < n; i++) {
    for (j = 0; j < n; j++) {
      a[j] = Rank[j] + 1;
      b[j] = (j + (1 << i)) >= n ? 0 : Rank[j + (1 << i)] + 1;
      sa[j] = j;
    }
    radix(b, sa, Rank, n, n);
    radix(a, Rank, sa, n, n);
    Rank[sa[0]] = 0;
    for (j = 1; j < n; j++) {
      Rank[sa[j]] = Rank[sa[j - 1]] +
                    (a[sa[j]] != a[sa[j - 1]] || b[sa[j]] != b[sa[j - 1]]);
    }
  }
}
void c_h(long long *str, long long *sa, long long *h, long long n) {
  static long long Rank[MAX];
  long long k = 0;
  h[0] = 0;
  long long i;
  for (i = 0; i < n; i++) Rank[sa[i]] = i;
  for (i = 0; i < n; i++) {
    k = (k == 0 ? 0 : k - 1);
    if (Rank[i]) {
      while (str[i + k] == str[sa[Rank[i] - 1] + k]) k++;
    }
    h[Rank[i]] = k;
  }
}
char in[MAX];
char ava[MAX];
long long len;
long long s[MAX];
long long SA[MAX];
long long H[MAX];
long long tall[MAX];
long long tallcnt;
struct node2 {
  long long pos, val;
};
stack<node2> SS;
int main() {
  cin >> len >> in >> ava;
  long long i;
  long long ans = 0;
  for (i = 0; i < len; i++) {
    s[i] = in[len - i - 1] - 'a' + 1;
    if (ava[i] == '0') ans = i + 1;
  }
  if (ans == 0) {
    puts("0");
    return 0;
  }
  for (i = 0; (i << 1) < len; i++) {
    swap(ava[i], ava[len - i - 1]);
  }
  s_a(s, SA, len, 27);
  c_h(s, SA, H, len);
  tallcnt = 0;
  tall[tallcnt++] = 0;
  for (i = 0; i < len; i++) {
    if (ava[SA[i]] == '1')
      H[i + 1] = min(H[i], H[i + 1]);
    else {
      tall[tallcnt++] = H[i];
    }
  }
  tall[tallcnt++] = 0;
  for (i = 0; i < tallcnt; i++) {
    node2 nn, nnn;
    nnn.pos = i;
    while (!SS.empty()) {
      nn = SS.top();
      if (nn.val < tall[i]) break;
      SS.pop();
      long long temp = nn.val;
      temp *= 1 + i - nn.pos;
      ans = max(ans, temp);
      nnn.pos = nn.pos;
    }
    nnn.val = tall[i];
    SS.push(nnn);
  }
  cout << ans;
  return 0;
}
