#include <bits/stdc++.h>
using namespace std;
int n, Fa[55], res = 0, ans, sym[55], Mat2[55], Cst[55], F[1 << 23], tmpn;
char Mat1[55][55];
bool Can[1 << 23];
void cmin(int &x, int y) {
  if (x > y) x = y;
}
int getf(int x) { return Fa[x] == x ? x : Fa[x] = getf(Fa[x]); }
int main() {
  scanf("%d", &n);
  tmpn = n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", Mat1[i] + 1);
    Fa[i] = i;
  }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (Mat1[i][j] == 'A') {
        if (getf(i) != getf(j)) Fa[getf(i)] = getf(j);
      }
  int tim = -1;
  for (int i = 1; i <= n; i++) Cst[getf(i)]++;
  memset(sym, -1, sizeof sym);
  for (int i = 1; i <= n; i++)
    if (Fa[i] == i) {
      if (Cst[i] > 1)
        sym[i] = ++tim;
      else
        res++;
    }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (sym[getf(i)] != -1 && sym[getf(j)] != -1 && Mat1[i][j] == 'X') {
        if (getf(i) == getf(j)) {
          puts("-1");
          return 0;
        } else
          Mat2[sym[getf(i)]] |= 1 << sym[getf(j)];
      }
  n = tim + 1;
  assert(n <= 23);
  for (int i = 1; i < (1 << n); i++) {
    Can[i] = 1;
    for (int j = 0; j < n; j++)
      if (((i >> j) & 1) && (Mat2[j] & i)) {
        Can[i] = 0;
        break;
      }
  }
  memset(F, 63, sizeof F);
  F[0] = 0;
  for (int i = (1 << n) - 1; i; i--)
    if (Can[i]) {
      int nw = 0;
      for (int j = 0; j < n; j++)
        if (!((i >> j) & 1) && Can[i | (1 << j)]) nw |= (1 << j);
      for (int tmp = (1 << n) - 1 - i - nw, j = tmp;; j = (j - 1) & tmp) {
        cmin(F[(j + nw) | i], F[j + nw] + 1);
        if (!j) break;
      }
    }
  printf("%d\n", tmpn + F[(1 << n) - 1] - 1);
  return 0;
}
