#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
namespace suffix_array {
int sz, len;
int a[N], lcp[N], rank[N], tmp[N];
bool cmp(int x, int y) {
  if (rank[x] != rank[y]) return rank[x] < rank[y];
  x += len, y += len;
  return (x < sz && y < sz ? rank[x] < rank[y] : x > y);
}
void build(string s) {
  sz = s.size();
  for (int i = 0; i < sz; ++i) a[i] = i, rank[i] = s[i];
  for (len = 1;; len <<= 1) {
    sort(a, a + sz, cmp);
    for (int i = 1; i < sz; ++i) tmp[i] = tmp[i - 1] + cmp(a[i - 1], a[i]);
    for (int i = 0; i < sz; ++i) rank[a[i]] = tmp[i];
    if (tmp[sz - 1] == sz - 1) break;
  }
  int cur = 0;
  for (int i = 0; i < sz; ++i)
    if (rank[i] != sz - 1) {
      int j = a[rank[i] + 1];
      while (i + cur < sz && j + cur < sz && s[i + cur] == s[j + cur]) cur++;
      lcp[rank[i]] = cur;
      if (cur) cur--;
    }
}
};  // namespace suffix_array
using namespace suffix_array;
int n;
string s, t;
int l[N], r[N], f[N];
stack<int> st;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> s >> t;
  reverse(s.begin(), s.end()), reverse(t.begin(), t.end());
  build(s);
  for (int i = 0; i < n; ++i) {
    while (!st.empty() && lcp[i] <= lcp[st.top()]) st.pop();
    l[i] = st.empty() ? 0 : st.top() + 1;
    st.push(i);
  }
  while (!st.empty()) st.pop();
  for (int i = n - 1; i >= 0; --i) {
    while (!st.empty() && lcp[i] <= lcp[st.top()]) st.pop();
    r[i] = st.empty() ? n - 1 : st.top() - 1;
    st.push(i);
  }
  for (int i = 0; i < n; ++i) {
    f[i] = (i ? f[i - 1] : 0) + '1' - t[a[i]];
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i)
    if (t[i] == '0') {
      ans = 1ll * (n - i);
      break;
    }
  for (int i = 0; i < n; ++i) {
    int cnt = f[r[i] + 1] - (l[i] ? f[l[i] - 1] : 0);
    ans = max(ans, 1ll * cnt * lcp[i]);
  }
  cout << ans << '\n';
}
