#include <bits/stdc++.h>
using namespace std;
int n, p[50], sz[50], v[50], id[50], ct, lg[(1 << 24)], bc[(1 << 24)];
unsigned int f[(1 << 24)], av[50];
char s[50][50];
vector<int> vc[50];
int fd(int x) { return p[x] == x ? x : p[x] = fd(p[x]); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) p[i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= n; j++)
      if (s[i][j] == 'A') {
        int xx = fd(i), yy = fd(j);
        p[xx] = yy;
      }
  }
  for (int i = 1; i <= n; i++) {
    int xx = fd(i);
    sz[xx]++;
    vc[xx].push_back(i);
  }
  for (int i = 1; i <= n; i++)
    if (sz[i] > 1 && p[i] == i) {
      id[i] = ct++;
      for (auto x : vc[i])
        for (auto y : vc[i])
          if (s[x][y] == 'X') {
            printf("-1\n");
            return 0;
          }
    }
  for (int i = 0; i < ct; i++) bc[1 << i] = i;
  if (!ct) {
    printf("%d", n - 1);
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (p[i] == i && sz[i] == 1) continue;
    for (int j = 1; j <= n; j++) {
      if (p[j] == j && sz[j] == 1) continue;
      if (s[i][j] == 'X') {
        int xx = fd(i), yy = fd(j);
        v[id[xx]] |= (1 << id[yy]);
        v[id[yy]] |= (1 << id[xx]);
      }
    }
  }
  f[0] = 1;
  for (int i = 1; i < (1 << ct); i++) {
    int lb = i & -i;
    lg[i] = lg[i - lb] + 1;
    f[i] = f[i - lb] + f[(i - lb) ^ (i & v[bc[lb]])];
  }
  for (int i = 0; i < (1 << ct); i++) f[i]--;
  for (int i = 0; i < (1 << ct); i++) {
    unsigned int r = 1;
    for (int j = 1; j <= ct; j++) {
      r *= f[i];
      if ((ct - lg[i]) & 1)
        av[j] -= r;
      else
        av[j] += r;
    }
  }
  for (int j = 1; j <= ct; j++)
    if (av[j]) {
      printf("%d", j + n - 1);
      return 0;
    }
}
