#include <bits/stdc++.h>
using std::pair;
using std::string;
using std::vector;
template <typename T>
bool chkmax(T& a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T& a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
template <typename T>
T read(T& x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
  return x *= f;
}
const int N = 100;
const int S = 1 << 23;
int n, m;
int fa[N + 5], id[N + 5], sz[N + 5];
int ban[S + 5], coe[S + 5], dp[S + 5];
int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); }
char st[N + 5][N + 5];
int main() {
  read(n);
  for (int i = 0; i < n; ++i) fa[i] = i;
  for (int i = 0; i < n; ++i) {
    scanf("%s", st[i]);
    for (int j = 0; j < n; ++j) {
      if (st[i][j] == 'A') {
        fa[findset(i)] = findset(j);
      }
    }
  }
  for (int i = 0; i < n; ++i) ++sz[findset(i)], id[i] = -1;
  for (int i = 0; i < n; ++i)
    if (findset(i) == i && sz[i] >= 2) id[i] = m++;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (st[i][j] == 'X') {
        if (findset(i) == findset(j)) return puts("-1"), 0;
        int x = id[findset(i)], y = id[findset(j)];
        if (x >= 0 && y >= 0) ban[1 << x] |= 1 << y;
      }
    }
  }
  coe[0] = 1;
  for (int i = 1; i < (1 << m); ++i) {
    coe[i] = coe[i ^ (i & -i)] & (!(ban[i & -i] & i));
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < (1 << m); ++j)
      if (j >> i & 1) {
        coe[j] += coe[j ^ (1 << i)];
      }
  }
  for (int i = 0; i < (1 << m); ++i) dp[i] = 1;
  int ans = n - 1;
  while (true) {
    int val = 0;
    for (int i = 0; i < (1 << m); ++i) {
      val += ((m - __builtin_popcount(i)) & 1) ? -dp[i] : dp[i];
    }
    if (val) break;
    ++ans;
    for (int i = 0; i < (1 << m); ++i) dp[i] *= coe[i];
  }
  printf("%d\n", ans);
  return 0;
}
