#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<string> str(n);
  for (auto& s : str) cin >> s;
  vector<vector<long long>> dp(n, vector<long long>(26, 0));
  vector<vector<int>> best(n, vector<int>(26, 0));
  vector<int> pref(n, 0);
  vector<int> suff(n, 0);
  for (int i = 0; i < n; ++i) {
    while (pref[i] < str[i].size() && str[i][pref[i]] == str[i][0]) ++pref[i];
    while (suff[i] < str[i].size() &&
           str[i][str[i].size() - suff[i] - 1] == str[i][str[i].size() - 1])
      ++suff[i];
    int j = 0;
    while (j < str[i].size()) {
      int k = j;
      while (k < str[i].size() && str[i][j] == str[i][k]) ++k;
      best[i][str[i][j] - 'a'] = max(best[i][str[i][j] - 'a'], k - j);
      j = k;
    }
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 26; ++j) {
      dp[i][j] = best[i][j];
      if (i == 0) continue;
      if (best[i][j] != str[i].size()) {
        if (dp[i - 1][j] > 0) {
          long long cur = 1;
          if (str[i][0] - 'a' == j) cur += pref[i];
          if (str[i][str[i].size() - 1] - 'a' == j) cur += suff[i];
          dp[i][j] = max(dp[i][j], cur);
        }
      } else {
        dp[i][j] = max(dp[i][j], dp[i - 1][j] + (long long)str[i].size() *
                                                    (dp[i - 1][j] + 1));
        dp[i][j] = min(dp[i][j], (long long)1e9);
      }
    }
  long long res = -1;
  for (int i = 0; i < 26; ++i) res = max(res, dp[n - 1][i]);
  cout << res << endl;
  return 0;
}
