#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void rd(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
  x *= f;
}
char A[55][55];
int fa[55], n;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int e[55], bit[1 << 23], f[1 << 23], coe[1 << 23], m;
long long g[1 << 23];
vector<int> nd[55];
int bel[55];
int main() {
  rd(n);
  for (int i = 1; i <= n; ++i) scanf("%s", A[i] + 1);
  for (int i = 1; i <= n; ++i) fa[i] = i;
  int flg = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (A[i][j] == 'A') fa[find(i)] = find(j), flg = 1;
  if (!flg) {
    printf("%d", n - 1);
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (A[i][j] == 'X' && find(i) == find(j)) {
        printf("-1");
        exit(0);
      };
  for (int i = 1; i <= n; ++i) nd[find(i)].push_back(i);
  memset(bel, -1, sizeof(bel));
  for (int i = 1; i <= n; ++i)
    if (find(i) == i && nd[i].size() > 1) {
      for (int j = 0; j < nd[i].size(); ++j) bel[nd[i][j]] = m;
      m++;
    }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (A[i][j] == 'X' && bel[i] != -1 && bel[j] != -1)
        e[bel[i]] |= 1 << bel[j], e[bel[j]] |= 1 << bel[i];
  for (int i = 0; i < m; ++i) bit[1 << i] = i;
  for (int i = 1; i < (1 << m); ++i) {
    int p = i & -i;
    f[i] = f[i - p] + f[(i - p) & (~e[bit[p]])] + 1;
  }
  coe[0] = (n & 1) ? -1 : 1;
  for (int i = 1; i < (1 << m); ++i) coe[i] = -coe[i & (i - 1)];
  fill(g, g + (1 << m), 1);
  for (int k = 1; k <= m; ++k) {
    long long tot = 0;
    for (int i = 0; i < (1 << m); ++i) g[i] *= f[i], tot += coe[i] * g[i];
    if (tot) {
      printf("%d", n - 1 + k);
      break;
    }
  }
  return 0;
}
