#include <bits/stdc++.h>
using namespace std;
const int modulo = 1077563119;
int Chromatic(const vector<int> &g) {
  int n = g.size();
  if (n == 0) return 0;
  int all = 1 << n;
  vector<int> a(all), s(all);
  a[0] = 1;
  for (int i = 1; i < all; i++) {
    int j = __builtin_ctz(i);
    a[i] = a[i - (1 << j)] + a[(i - (1 << j)) & ~g[j]];
    if (a[i] >= modulo) a[i] -= modulo;
  }
  for (int i = 0; i < all; i++) {
    s[i] = ((n - __builtin_popcount(i)) & 1 ? -1 : 1);
  }
  for (int k = 1; k < n; k++) {
    long long sum = 0;
    for (int i = 0; i < all; i++) {
      long long cur = ((s[i] * (long long)a[i]) % modulo);
      s[i] = (int)cur;
      sum += cur;
    }
    if (sum % modulo != 0) return k;
  }
  return n;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<string> c(n);
  vector<vector<bool>> reach(n, vector<bool>(n));
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      reach[i][j] = (i == j || c[i][j] == 'A');
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (reach[i][k] && reach[k][j]) {
          reach[i][j] = true;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (reach[i][j] && c[i][j] == 'X') {
        return !printf("-1\n");
      }
    }
  }
  vector<int> idx(n, -1);
  vector<bool> used(n, false);
  int N = 0;
  for (int i = 0; i < n; i++)
    if (!used[i]) {
      vector<int> v;
      for (int j = 0; j < n; j++)
        if (reach[i][j]) v.push_back(j);
      if (v.size() >= 2) {
        for (int j = 0; j < v.size(); j++) {
          idx[v[j]] = N;
        }
        N++;
      }
      for (int j = 0; j < v.size(); j++) used[v[j]] = true;
    }
  vector<int> g(N);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (c[i][j] == 'X' && idx[i] != idx[j] && idx[i] >= 0 && idx[j] >= 0) {
        g[idx[i]] |= 1 << idx[j];
      }
    }
  }
  int ans = n - 1 + Chromatic(g);
  printf("%d\n", ans);
  return 0;
}
