#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
void FMT(int* A, int n) {
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < (1 << n); ++j)
      if (j & (1 << i)) A[j] += A[j ^ (1 << i)];
}
int tmp[1 << 23];
int dp[1 << 23];
int can[1 << 23];
bool check(int n) {
  int ret = 0;
  int all = (1 << n) - 1;
  for (int i = 0; i < (1 << n); ++i) {
    int z = __builtin_popcount(i) & 1;
    if (z) {
      ret -= dp[all ^ i];
    } else {
      ret += dp[all ^ i];
    }
  }
  return ret;
}
char e[47][47];
int fa[47];
int siz[47];
bool edge[23][23];
int root(int now) { return fa[now] = (fa[now] == now ? now : root(fa[now])); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cin >> e[i][j];
  for (int i = 0; i < n; ++i) fa[i] = i;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (e[i][j] == 'A') {
        fa[root(i)] = root(j);
      }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (e[i][j] == 'X' && root(i) == root(j)) {
        puts("-1");
        return 0;
      }
  for (int i = 0; i < n; ++i) siz[root(i)]++;
  int num = 0;
  int Cn = 0;
  vector<int> id;
  for (int i = 0; i < n; ++i)
    if (root(i) == i) {
      ++Cn;
      if (siz[i] > 1) {
        num += siz[i];
        id.push_back(i);
      }
    }
  int n2 = id.size();
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (e[i][j] == 'X') {
        int i_, j_;
        i_ = root(i);
        j_ = root(j);
        if (siz[i_] == 1 || siz[j_] == 1) continue;
        i_ = lower_bound(id.begin(), id.end(), i_) - id.begin();
        j_ = lower_bound(id.begin(), id.end(), j_) - id.begin();
        edge[i_][j_] = 1;
      }
  for (int mask = 0; mask < 1 << n2; ++mask) {
    can[mask] = 1;
    for (int j = 0; j < n2; ++j)
      if ((mask >> j) & 1) {
        for (int k = 0; k < n2; ++k)
          if ((mask >> k) & 1)
            if (edge[j][k]) {
              can[mask] = false;
              break;
            }
        if (!can[mask]) break;
      }
  }
  num += Cn - 1;
  dp[0] = 1;
  FMT(can, n2);
  FMT(dp, n2);
  for (int i = 1; i <= n2; ++i) {
    for (int mask = 0; mask < 1 << n2; ++mask) dp[mask] = dp[mask] * can[mask];
    if (check(n2)) {
      num -= (n2 - i);
      break;
    }
  }
  cout << num << endl;
  return 0;
}
