#include <bits/stdc++.h>
using namespace std;
string tob(string s) {
  string t;
  for (int i = 0; i < s.size(); i++) {
    switch (s[i]) {
      case '0':
        t += "0000";
        break;
      case '1':
        t += "0001";
        break;
      case '2':
        t += "0010";
        break;
      case '3':
        t += "0011";
        break;
      case '4':
        t += "0100";
        break;
      case '5':
        t += "0101";
        break;
      case '6':
        t += "0110";
        break;
      case '7':
        t += "0111";
        break;
      case '8':
        t += "1000";
        break;
      case '9':
        t += "1001";
        break;
      case 'A':
        t += "1010";
        break;
      case 'B':
        t += "1011";
        break;
      case 'C':
        t += "1100";
        break;
      case 'D':
        t += "1101";
        break;
      case 'E':
        t += "1110";
        break;
      case 'F':
        t += "1111";
        break;
      case 'a':
        t += "1010";
        break;
      case 'b':
        t += "1011";
        break;
      case 'c':
        t += "1100";
        break;
      case 'd':
        t += "1101";
        break;
      case 'e':
        t += "1110";
        break;
      case 'f':
        t += "1111";
        break;
    }
  }
  return t;
}
int main() {
  int n;
  scanf("%d", &n);
  string arr[n];
  int mnn = 1e9;
  for (int i = 0; i < n; i++) {
    char ss[7009];
    scanf("%s", ss);
    arr[i] = tob(ss);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j += 2) {
      if (arr[i][j] != arr[i][j + 1]) {
        puts("1");
        return 0;
      }
    }
    vector<pair<int, int>> v;
    for (int j = 0; j < n; j++) {
      if (v.empty() || v.back().second != arr[i][j]) {
        v.push_back({1, arr[i][j]});
      } else {
        v.back().first++;
      }
    }
    int mn = 1e9;
    for (int ii = 0; ii < v.size(); ii++) {
      mn = min(mn, v[ii].first);
    }
    mnn = min(mnn, mn);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j += 2) {
      if (arr[j][i] != arr[j + 1][i]) {
        puts("1");
        return 0;
      }
    }
    vector<pair<int, int>> v;
    for (int j = 0; j < n; j++) {
      if (v.empty() || v.back().second != arr[j][i]) {
        v.push_back({1, arr[j][i]});
      } else {
        v.back().first++;
      }
    }
    int mn = 1e9;
    for (int ii = 0; ii < v.size(); ii++) {
      mn = min(mn, v[ii].first);
    }
    mnn = min(mnn, mn);
  }
  printf("%d", mnn);
  return 0;
}
