#include <bits/stdc++.h>
using namespace std;
int n;
char _map[52][52];
int pa[52];
int fin(int x) {
  if (pa[x] == x) return x;
  return pa[x] = fin(pa[x]);
}
vector<vector<int> > all;
int can[52][52];
int calc(vector<int> a, vector<int> b) {
  for (int i = 0; i < a.size(); i++)
    for (int j = 0; j < b.size(); j++)
      if (_map[a[i]][b[j]] == 'X') return 0;
  return 1;
}
int f[1 << 24];
int dp[1 << 24];
void fwt(int a[], int typ) {
  for (int i = 1; i < (1 << n); i <<= 1)
    for (int j = 0; j < (1 << n); j += i + i)
      for (int k = 0; k < i; k++)
        a[j + k + i] = (a[j + k] * typ + a[j + k + i]);
}
int cp[1 << 24];
void ini() {
  for (int i = 0; i < (1 << n); i++) cp[i] = 1;
  for (int i = 1; i < (1 << n); i <<= 1)
    for (int j = 0; j < (1 << n); j += i + i)
      for (int k = 0; k < i; k++) cp[j + k] *= -1;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%s", _map[i]);
  for (int i = 0; i < n; i++) pa[i] = i;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (_map[i][j] == 'A') pa[fin(i)] = fin(j);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (_map[i][j] == 'X')
        if (fin(i) == fin(j)) {
          puts("-1");
          exit(0);
        };
  int ans = n - 1;
  for (int i = 0; i < n; i++)
    if (fin(i) == i) {
      vector<int> tmp;
      for (int j = 0; j < n; j++)
        if (fin(j) == i) tmp.push_back(j);
      if (tmp.size() > 1) all.push_back(tmp);
    }
  n = all.size();
  assert(n <= 24);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      can[i][j] = calc(all[i], all[j]);
      if (can[i][j] == 0) f[(1 << i) | (1 << j)] = 1;
    }
  fwt(f, 1);
  dp[0] = 1;
  for (int i = 0; i < (1 << n); i++) f[i] = !f[i];
  fwt(f, 1);
  ini();
  fwt(dp, 1);
  for (int i = 0; i < n; i++) {
    int cur = 0;
    for (int j = 0; j < (1 << n); j++)
      cur += cp[j] * dp[j], dp[j] = dp[j] * f[j];
    if (cur) break;
    ans++;
  }
  cout << ans;
  return 0;
}
