#include <bits/stdc++.h>
using namespace std;
const int INF = 1.01e9;
const int MOD = 1e9 + 613;
void fwht(vector<long long>& p, bool inverse) {
  int n = p.size();
  for (int k = 1; k < n; k *= 2) {
    for (int i = 0; i < n; i += k * 2) {
      for (int j = 0; j < k; j++) {
        long long v = p[i + j];
        long long u = p[i + j + k];
        if (inverse == 0) {
          p[i + j] = (v + u) % MOD;
          p[i + j + k] = v;
        } else {
          p[i + j] = u;
          p[i + j + k] = (v - u + MOD) % MOD;
        }
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    p[i] = i;
  }
  function<int(int)> getId = [&](int v) {
    return (p[v] == v) ? v : p[v] = getId(p[v]);
  };
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (s[i][j] == 'A') {
        int v = getId(i);
        int u = getId(j);
        p[v] = u;
      }
    }
  }
  vector<vector<int>> comp;
  for (int i = 0; i < n; i++) {
    if (p[i] == i) {
      vector<int> who;
      for (int j = 0; j < n; j++) {
        if (getId(j) == i) {
          who.push_back(j);
        }
      }
      for (auto v : who) {
        for (auto u : who) {
          if (s[v][u] == 'X') {
            puts("-1");
            return 0;
          }
        }
      }
      if (who.size() > 1) {
        comp.push_back(who);
      }
    }
  }
  int m = comp.size();
  vector<vector<int>> e2(m, vector<int>(m));
  for (int i = 0; i < m; i++) {
    for (int j = i + 1; j < m; j++) {
      bool haveXor = 0;
      for (auto v : comp[i]) {
        for (auto u : comp[j]) {
          haveXor |= s[v][u] == 'X';
        }
      }
      e2[i][j] = haveXor == 0;
      e2[j][i] = haveXor == 0;
    }
  }
  if (m == 0) {
    cout << n - 1 << endl;
    return 0;
  }
  vector<long long> dp((1ll << (m)), -1);
  dp[0] = 1;
  for (int mask = 0; mask < (1ll << (m)); mask++) {
    if (dp[mask] == -1) {
      dp[mask] = 0;
    }
    if (dp[mask] != 1) continue;
    for (int i = 0; i < m; i++) {
      if ((mask & (1ll << (i))) == 0 && dp[mask | (1ll << (i))] == -1) {
        bool ok = 1;
        for (int j = 0; j < m; j++) {
          if (mask & (1ll << (j))) {
            ok &= e2[i][j];
          }
        }
        dp[mask | (1ll << (i))] = ok;
      }
    }
  }
  fwht(dp, 0);
  auto step = dp;
  vector<int> megaCof((1ll << (m)));
  function<void(int, int, int)> build = [&](int l, int r, int cof) {
    if (l + 1 == r) {
      megaCof[l] = cof;
      return;
    }
    build(l, (l + r) / 2, cof);
    build((l + r) / 2, r, cof * -1);
  };
  build(0, (1ll << (m)), 1);
  int answer = n;
  for (;; answer++) {
    assert(answer <= n * 2);
    long long score = 0;
    for (int i = 0; i < (1ll << (m)); i++) {
      score = (score + megaCof[i] * dp[i] + MOD) % MOD;
    }
    if (score != 0) {
      cout << answer << endl;
      break;
    }
    for (int i = 0; i < (1ll << (m)); i++) {
      dp[i] = (dp[i] * step[i]) % MOD;
    }
  }
  return 0;
}
