#include <bits/stdc++.h>
int n, m, id[50], fa[50], siz[50], f[8388615], g[8388615], a[8388615];
char mp[50][50];
void fwt(int a[], int n) {
  for (int k = 1; k < n; k <<= 1)
    for (int i = 0; i < n; i += (k << 1))
      for (int j = 0; j < k; j++) a[i + j + k] += a[i + j];
}
int find(int x) {
  if (fa[x] == x) return x;
  return fa[x] = find(fa[x]);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", mp[i]);
    fa[i] = i;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++)
      if (mp[i][j] == 'A') {
        int r1 = find(i), r2 = find(j);
        if (r1 != r2) fa[r1] = fa[r2];
      }
  for (int i = 0; i < n; i++) siz[find(i)]++;
  for (int i = 0; i < n; i++)
    if (find(i) == i && siz[i] > 1) id[i] = 1 << (m++);
  if (!m) {
    printf("%d\n", n - 1);
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++)
      if (mp[i][j] == 'X') {
        int r1 = find(i), r2 = find(j);
        if (r1 == r2) {
          printf("-1\n");
          return 0;
        } else if (id[r1] && id[r2])
          f[id[r1] | id[r2]] = 1;
      }
  int m1 = 1 << m;
  fwt(f, m1);
  for (int i = 0; i < m1; i++) f[i] = !f[i], g[i] = 1;
  fwt(f, m1);
  for (int i = 0; i < m1; i++) a[i] = 1;
  for (int k = 1; k < m1; k <<= 1)
    for (int i = 0; i < m1; i += (k << 1))
      for (int j = 0; j < k; j++) a[i + j] *= -1;
  for (int i = 0; i < m; i++) {
    int cur = 0;
    for (int j = 0; j < m1; j++) g[j] *= f[j], cur += a[j] * g[j];
    if (cur) {
      printf("%d\n", n + i);
      return 0;
    }
  }
}
