#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 23) + 10;
inline int read() {
  register int p = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    p = p * 10 + ch - '0';
    ch = getchar();
  }
  return p * f;
}
int lim;
namespace FWT {
void main(int *a, int typ) {
  for (int len = 1; len < lim; len <<= 1)
    for (int i = 0; i < lim; i += (len << 1))
      for (int j = 0; j < len; j++) a[i + j + len] += a[i + j] * typ;
}
}  // namespace FWT
int n;
int fa[50], sz[50];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int m, id[50], nxt[50];
char mp[50][50];
int lg[maxn], g[maxn], sg[maxn];
int A[maxn];
inline bool check() {
  for (int i = 0; i < lim; i++) A[i] = sg[i];
  FWT::main(A, -1);
  return A[lim - 1] != 0;
}
int main() {
  n = read();
  for (int i = 0; i < n; i++) fa[i] = i, sz[i] = 1, id[i] = -1;
  for (int i = 0; i < n; i++) {
    scanf("%s", mp[i]);
    for (int j = 0; j < n; j++)
      if (mp[i][j] == 'A' && find(i) != find(j)) {
        sz[find(i)] += sz[find(j)];
        fa[find(j)] = find(i);
      }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (find(i) == find(j) && mp[i][j] == 'X') {
        puts("-1");
        return 0;
      }
  for (int i = 0; i < n; i++)
    if (find(i) == i && sz[i] > 1) id[i] = m++;
  if (!m) {
    printf("%d\n", n - 1);
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (mp[i][j] == 'X') {
        int a = id[find(i)], b = id[find(j)];
        if (a != -1 && b != -1) nxt[a] |= (1 << b), nxt[b] |= (1 << a);
      }
  for (int i = 0; i < m; i++) lg[1 << i] = i;
  g[0] = 1;
  for (int i = 1; i < (1 << m); i++) {
    int x = lg[i & -i];
    g[i] = g[i ^ (1 << x)] & (!(i & nxt[x]));
  }
  lim = (1 << m);
  FWT::main(g, 1);
  for (int i = 0; i < lim; i++) sg[i] = g[i];
  for (int i = 1; i <= m; i++) {
    if (check()) {
      printf("%d\n", n - 1 + i);
      return 0;
    }
    for (int j = 0; j < lim; j++) sg[j] *= g[j];
  }
}
