#include <bits/stdc++.h>
using namespace std;
struct SA {
  struct state {
    int len, link, nxt[26], c = 0;
    state() : len(0), link(-1) { fill(begin(nxt), end(nxt), 0); }
  };
  vector<state> d;
  int last = 0;
  SA() { d.push_back(state()); }
  void add(char ch, int occ) {
    int cur = d.size();
    d.push_back(state());
    d[cur].len = d[last].len + 1;
    d[cur].c = occ;
    int p = last;
    while (p != -1 && !d[p].nxt[ch - 'a']) {
      d[p].nxt[ch - 'a'] = cur;
      p = d[p].link;
    }
    if (p == -1) {
      d[cur].link = 0;
    } else {
      int q = d[p].nxt[ch - 'a'];
      if (d[q].len == d[p].len + 1) {
        d[cur].link = q;
      } else {
        int clone = d.size();
        d.push_back(d[q]);
        d[clone].len = d[p].len + 1;
        d[clone].c = 0;
        while (p != -1 && d[p].nxt[ch - 'a'] == q) {
          d[p].nxt[ch - 'a'] = clone;
          p = d[p].link;
        }
        d[cur].link = d[q].link = clone;
      }
    }
    last = cur;
  }
} sa;
const int N = 4e5 + 7;
vector<int> adj[N];
int dp[N];
void dfs(int v) {
  for (int u : adj[v]) {
    dfs(u);
    dp[v] += dp[u];
  }
  dp[v] += sa.d[v].c;
}
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s, t;
  cin >> n >> s >> t;
  for (int i = 0; i < n; i++) {
    sa.add(s[i], t[i] == '0');
  }
  for (int i = 1; i < sa.d.size(); i++) adj[sa.d[i].link].push_back(i);
  long long ans = 0;
  dfs(0);
  for (int i = 1; i < sa.d.size(); i++) {
    ans = max(ans, 1LL * sa.d[i].len * dp[i]);
  }
  cout << ans << endl;
}
