#include <bits/stdc++.h>
using namespace std;
const int mo = 998244353;
const int M = (1 << 23) | 5;
const int N = 50;
int n, m, fa[N], e[N][N];
int sz[N], id[N];
int GG[M], cnt[M];
char s[N][N];
int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) fa[i] = i;
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%s", s[i] + 1);
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(1); j <= (int)(n); j++)
      if (s[i][j] == 'A') fa[get(i)] = get(j);
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(1); j <= (int)(n); j++)
      if (s[i][j] == 'X' && get(i) == get(j)) return puts("-1"), 0;
  for (int i = (int)(1); i <= (int)(n); i++) ++sz[get(i)];
  for (int i = (int)(1); i <= (int)(n); i++)
    if (get(i) == i && sz[i] != 1) id[i] = ++m;
  if (!m) return printf("%d\n", n - 1), 0;
  for (int i = (int)(1); i <= (int)(n); i++)
    for (int j = (int)(1); j <= (int)(n); j++)
      if (s[i][j] == 'X') e[id[get(i)]][id[get(j)]] = 1;
  for (int i = (int)(1); i <= (int)(m); i++)
    for (int j = (int)(1); j <= (int)(m); j++)
      if (e[i][j]) GG[((1 << i) | (1 << j)) >> 1] = 1;
  for (int d = 1; d < 1 << m; d <<= 1)
    for (int i = 0; i < 1 << m; i += d << 1)
      for (int j = 0; j < d; j++) GG[i | j | d] |= GG[i | j];
  for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)
    cnt[i] = cnt[i / 2] ^ (i & 1);
  for (int i = (int)(0); i <= (int)((1 << m) - 1); i++) GG[i] = 1 - GG[i];
  for (int d = 1; d < 1 << m; d <<= 1)
    for (int i = 0; i < 1 << m; i += d << 1)
      for (int j = 0; j < d; j++) GG[i | j | d] += GG[i | j];
  for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)
    cnt[i] = ((cnt[i] + m) & 1 ? mo - 1 : 1);
  for (int ans = 1;; ++ans) {
    int sum = 0;
    for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)
      cnt[i] = 1ll * cnt[i] * GG[i] % mo;
    for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)
      sum = (sum + cnt[i]) % mo;
    if (sum) return printf("%d\n", n - 1 + ans), 0;
  }
}
