#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long n, par[1000010], sz[1000010], mx = 0, ans;
string s, t;
vector<pair<long long, long long> > e[1000010];
namespace SA {
long long sa[1000010], lcp[1000010], rk[2000010], cnt[1000010], tmp[1000010],
    add, cur;
void rsort() {
  cur = 0;
  for (long long i = n - 1; i >= n - add; --i) tmp[cur++] = i;
  for (long long i = 0; i < n; ++i)
    if (sa[i] - add >= 0) tmp[cur++] = sa[i] - add;
  long long bd = max(n, 300LL);
  for (long long i = 0; i < bd + 1; ++i) cnt[i] = 0;
  for (long long i = 0; i < n; ++i) ++cnt[rk[i]];
  for (long long i = 1; i <= bd + 1; ++i) cnt[i] += cnt[i - 1];
  for (long long i = n - 1; i >= 0; --i) sa[--cnt[rk[tmp[i]]]] = tmp[i];
}
long long cmp(long long x, long long y) {
  return (long long)(rk[x] != rk[y] || rk[x + add] != rk[y + add]);
}
void getSA() {
  for (long long i = n; i < n + n + 5; ++i) rk[i] = 0;
  for (long long i = 0; i < n; ++i) rk[i] = s[i], sa[i] = i;
  long long m = 0;
  for (long long mask = 0; m < n; ++mask) {
    add = (mask == 0 ? 0 : (1 << (mask - 1)));
    rsort();
    tmp[sa[0]] = 1;
    for (long long i = 1; i <= n - 1; ++i)
      tmp[sa[i]] = tmp[sa[i - 1]] + cmp(sa[i - 1], sa[i]);
    for (long long i = 0; i < n; ++i) rk[i] = tmp[i];
    if ((1 << mask) >= n) break;
  }
}
void getLCP() {
  for (long long i = 0; i < n; ++i) --rk[i];
  lcp[0] = 0;
  for (long long i = 0; i < n; ++i) {
    if (rk[i] == 0) continue;
    long long least =
        (i - 1 >= 0 && lcp[rk[i - 1]] > 1 ? lcp[rk[i - 1]] - 1 : 0);
    while (i + least < n && sa[rk[i] - 1] + least < n &&
           s[i + least] == s[sa[rk[i] - 1] + least])
      ++least;
    lcp[rk[i]] = least;
  }
}
}  // namespace SA
long long Find(long long pos) {
  if (par[pos] != pos) par[pos] = Find(par[pos]);
  return par[pos];
}
void Merge(long long pos, long long pos2) {
  sz[Find(pos2)] += sz[Find(pos)];
  mx = max(mx, sz[Find(pos2)]);
  par[Find(pos)] = Find(pos2);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s >> t;
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  SA::getSA();
  SA::getLCP();
  long long cnt = 0;
  for (long long i = 0; i < n - 1; ++i) {
    if (t[SA::sa[i]] == '1') continue;
    long long p = i + 1, mn = SA::lcp[i + 1];
    while (p < n && t[SA::sa[p]] == '1') {
      ++p;
      mn = min(mn, SA::lcp[p]);
    }
    if (p < n) e[mn].push_back(make_pair(cnt, cnt + 1));
    ++cnt;
  }
  for (long long i = 0; i < cnt + 5; ++i) par[i] = i, sz[i] = 1;
  for (long long i = 0; i < n; ++i)
    if (t[i] == '0') {
      ans = n - i;
      break;
    }
  for (long long i = n - 1; i > 0; --i) {
    for (long long j = 0; j < e[i].size(); ++j)
      Merge(e[i][j].first, e[i][j].second);
    ans = max(ans, i * mx);
  }
  cout << ans << endl;
  return 0;
}
