#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 50;
const int M = 10000000;
const int MOD = 1e9 + 7;
string st[N];
int fa[N], lst[N], tot, cnt[N], ss[N];
int V[N], col[N], res;
int get_f(int t) {
  if (fa[t] == t)
    return t;
  else
    return get_f(fa[t]);
}
int ID[M], F[M], G[M];
int tmpA[M], tmpB[M], tmpC[M];
void FWT(int A[], int n) {
  for (int i = 2; i <= n; i <<= 1)
    for (int j = 0; j < n; j += i)
      for (int k = j; k < (j + (i >> 1)); ++k)
        A[k + (i >> 1)] = (A[k + (i >> 1)] + A[k]) % MOD;
}
void IFWT(int A[], int n) {
  for (int i = 2; i <= n; i <<= 1)
    for (int j = 0; j < n; j += i)
      for (int k = j; k < (j + (i >> 1)); ++k)
        A[k + (i >> 1)] = (A[k + (i >> 1)] - A[k] + MOD) % MOD;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> st[i];
  for (int i = 0; i < n; ++i) fa[i] = i;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < i; ++j)
      if (st[i][j] == 'A') fa[get_f(i)] = get_f(j);
  for (int i = 0; i < n; ++i) cnt[get_f(i)]++;
  tot = 0;
  for (int i = 0; i < n; ++i)
    if (fa[i] == i) {
      if (cnt[i] == 1)
        res++;
      else
        lst[i] = tot++;
    }
  for (int i = 0; i < n; ++i)
    if (cnt[i] != 1)
      for (int j = 0; j < i; ++j)
        if (cnt[j] != 1)
          if (st[i][j] == 'X') {
            int u = lst[get_f(i)], v = lst[get_f(j)];
            if (u == v) {
              puts("-1");
              exit(0);
            }
            if (u > v) swap(u, v);
            V[u] |= 1 << v;
            V[v] |= 1 << u;
          }
  for (int i = 0; i < tot; ++i) ID[1 << i] = i;
  F[0] = 1;
  for (int i = 1; i < (1 << tot); ++i) {
    int x = i & (-i);
    if (F[i ^ x] && !(V[ID[x]] & (i ^ x))) F[i] = 1;
  }
  G[0] = 1;
  FWT(F, 1 << tot);
  FWT(G, 1 << tot);
  for (int i = 0;; ++i) {
    int v = 0;
    for (int j = 0; j < (1 << tot); ++j)
      if (__builtin_popcount(((1 << tot) - 1) ^ j) & 1)
        v = (v + MOD - G[j]) % MOD;
      else
        v = (v + G[j]) % MOD;
    if (v) {
      cout << i - 1 + n;
      return 0;
    }
    for (int j = 0; j < (1 << tot); ++j) G[j] = 1ll * F[j] * G[j] % MOD;
  }
}
