#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long Mod = 1000000007LL, INF = 1e9, LINF = 1e18;
const long double Pi = 3.141592653589793116, EPS = 1e-9,
                  Gold = ((1 + sqrt(5)) / 2);
long long keymod[] = {1000000007LL, 1000000009LL, 1000000021LL, 1000000033LL};
long long keyCount = sizeof(keymod) / sizeof(long long);
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
int getbit(T s, int i) {
  return (s >> i) & 1;
}
template <class T>
T onbit(T s, int i) {
  return s | (T(1) << i);
}
template <class T>
T offbit(T s, int i) {
  return s & (~(T(1) << i));
}
template <class T>
int cntbit(T s) {
  return __builtin_popcountll(s);
}
auto TimeStart = chrono::steady_clock::now();
auto TimeEnd = chrono::steady_clock::now();
void ControlIO(int argc, char* argv[]);
void TimerStart();
void TimerStop();
void Exit();
string cppstr_infile = "FILE.IN";
string cppstr_outfile = "FILE.OUT";
long long n;
vector<string> p;
void Input() {
  cin >> n;
  p.resize(n);
  for (auto& z : p) cin >> z;
}
void Solve() {
  vector<long long> beautyGraph(26, 0);
  for (long long i = 0; i < p[0].size(); i++) {
    long long j = i;
    while (j + 1 < p[0].size() && p[0][j + 1] == p[0][i]) j++;
    beautyGraph[p[0][i] - 'a'] = max(beautyGraph[p[0][i] - 'a'], j - i + 1);
    i = j;
  }
  for (long long id = 1; id < n; id++) {
    if (*min_element(p[id].begin(), p[id].end()) ==
        *max_element(p[id].begin(), p[id].end())) {
      long long pos = p[id][0] - 'a';
      long long largestBeauty =
          max((beautyGraph[pos] + 1) * ((long long)p[id].size() + 1) - 1, 1LL);
      for (long long z = 0; z < 26; z++) {
        if (z == pos)
          beautyGraph[z] = largestBeauty;
        else
          beautyGraph[z] = (beautyGraph[z] > 0);
      }
      continue;
    }
    vector<long long> newGraph(26, 0);
    for (long long i = 0; i < p[id].size(); i++) {
      long long j = i;
      while (j + 1 < p[id].size() && p[id][j + 1] == p[id][i]) j++;
      newGraph[p[id][i] - 'a'] = max(newGraph[p[id][i] - 'a'], j - i + 1);
      i = j;
    }
    long long posPrefix = p[id][0] - 'a',
              posSuffix = p[id][p[id].size() - 1] - 'a';
    long long lenPrefix = 1, lenSuffix = 1;
    while (p[id][0 + lenPrefix] == p[id][0]) lenPrefix++;
    while (p[id][(long long)p[id].size() - 1 - lenSuffix] ==
           p[id][p[id].size() - 1])
      lenSuffix++;
    vector<long long> tmpGraph = newGraph;
    if (posPrefix == posSuffix) {
      if (beautyGraph[posPrefix] > 0)
        tmpGraph[posPrefix] =
            max(tmpGraph[posPrefix], lenPrefix + 1 + lenSuffix);
    } else {
      if (beautyGraph[posPrefix] > 0)
        tmpGraph[posPrefix] = max(tmpGraph[posPrefix], lenPrefix + 1);
      if (beautyGraph[posSuffix] > 0)
        tmpGraph[posSuffix] = max(tmpGraph[posSuffix], lenSuffix + 1);
    }
    for (long long z = 0; z < 26; z++) {
      if (beautyGraph[z] > 0) tmpGraph[z] = max(tmpGraph[z], 1LL);
    }
    beautyGraph = tmpGraph;
  }
  cout << *max_element(beautyGraph.begin(), beautyGraph.end()) << '\n';
}
int main(int argc, char* argv[]) {
  ControlIO(argc, argv);
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  Input();
  TimerStart();
  Solve();
  TimerStop();
  return 0;
}
void ControlIO(int argc, char* argv[]) {
  char* infile = new char[cppstr_infile.size() + 1];
  char* outfile = new char[cppstr_outfile.size() + 1];
  strcpy(infile, cppstr_infile.c_str());
  strcpy(outfile, cppstr_outfile.c_str());
}
void TimerStart() {}
void TimerStop() {}
void Exit() {
  TimerStop();
  exit(0);
}
