#include <bits/stdc++.h>
using namespace std;
char mat[50][50];
int vis[50];
long long dfs(int v, int n, int cnt) {
  vis[v] = cnt;
  long long ans = (1LL << v);
  for (int i = 0; i < n; i++) {
    if (vis[i] != 0) continue;
    if (mat[i][v] == 'A') ans += dfs(i, n, cnt);
  }
  return ans;
}
bool g[50][50];
int colorfy(int n) {
  long long a[1 << n], s[1 << n];
  int mask[n + 1];
  for (int i = 0; i < n; i++) {
    mask[i] = 0;
    for (int j = 0; j < n; j++) {
      if (g[i][j]) mask[i] += (1 << j);
    }
  }
  a[0] = 1;
  for (int i = 1; i < (1 << n); i++) {
    int j = __builtin_ctz(i);
    a[i] = a[i & (i - 1)] + a[(i & (i - 1)) & ~mask[j]];
    if (a[i] >= 1500450271) a[i] -= 1500450271;
  }
  for (int i = 0; i < (1 << n); i++)
    s[i] = (n - __builtin_popcount(i)) % 2 ? (1500450271 - a[i]) % 1500450271
                                           : a[i];
  for (int k = 1; k <= n; k++) {
    long long sum = 0;
    for (int i = 0; i < (1 << n); i++) {
      sum += s[i];
      if (sum >= 1500450271) sum -= 1500450271;
      s[i] *= a[i];
      s[i] %= 1500450271;
    }
    if (sum != 0) return k;
  }
  return n;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int x = 0; x < n; x++) {
    scanf("%s", mat[x]);
  }
  int sz = 0;
  for (int x = 0; x < n; x++) {
    if (!vis[x]) {
      long long tmp = dfs(x, n, ++sz);
      if (__builtin_popcountll(tmp) == 1) {
        vis[x] = -1;
        sz--;
      }
    }
  }
  for (int x = 0; x < n; x++) {
    if (vis[x] < 0) continue;
    for (int y = 0; y < n; y++) {
      if (vis[y] < 0) continue;
      if (mat[x][y] == 'X') {
        g[vis[x] - 1][vis[y] - 1] = true;
        g[vis[y] - 1][vis[x] - 1] = true;
      }
    }
  }
  for (int i = 0; i < sz; i++) {
    if (g[i][i]) {
      puts("-1");
      return 0;
    }
  }
  printf("%d\n", n - 1 + colorfy(sz));
}
