#include <bits/stdc++.h>
using namespace std;
const int N = 50, M = (1 << 23) + 10;
int fa[N], sz[N];
int find_father(int x) {
  if (x == fa[x]) return x;
  return fa[x] = find_father(fa[x]);
}
void Merge(int x, int y) {
  int f1 = find_father(x), f2 = find_father(y);
  if (f1 == f2) return;
  sz[f2] += sz[f1];
  fa[f1] = f2;
}
int n, m;
void FWT(int *a, int len) {
  for (int i = 1; i < len; i <<= 1)
    for (int j = 0; j < len; j += (i << 1))
      for (int k = j; k < j + i; k++) a[k + i] += a[k];
}
char s[N][N];
int id[N];
int a[M], f[M], g[M];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) fa[i] = i, sz[i] = 1;
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < i; j++)
      if (s[i][j] == 'A') Merge(i, j);
  for (int i = 0; i < n; i++)
    if (find_father(i) == i && sz[i] > 1) {
      id[i] = 1 << m;
      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 (s[i][j] == 'X') {
        if (find_father(i) == find_father(j)) {
          puts("-1");
          return 0;
        } else if (id[find_father(i)] && id[find_father(j)])
          f[id[find_father(i)] | id[find_father(j)]] = 1;
      }
  int len = 1 << m;
  FWT(f, len);
  for (int i = 0; i < len; i++) f[i] = !f[i], g[i] = 1;
  FWT(f, len);
  for (int i = 0; i < len; i++) a[i] = 1;
  for (int i = 1; i < len; i <<= 1)
    for (int j = 0; j < len; j += (i << 1))
      for (int k = j; k < j + i; k++) a[k] *= -1;
  for (int i = 0; i < m; i++) {
    int cur = 0;
    for (int j = 0; j < len; j++) g[j] *= f[j], cur += a[j] * g[j];
    if (cur) {
      printf("%d\n", n + i);
      return 0;
    }
  }
}
