#include <bits/stdc++.h>
using namespace std;
const int maxn = 50;
int k, n, m, bel[maxn], sz[maxn], id[maxn], p[maxn], col[maxn];
bool g[maxn][maxn];
char s[maxn][maxn];
struct RND {
  static const int mod = 1e9 + 7, k = 233, b = 19260817;
  int seed;
  RND() { seed = k; }
  operator int() { return seed = ((long long)seed * k + b) % mod; }
} rnd;
void dfs(int u) {
  bel[u] = m;
  for (int v = 0; v < n; ++v)
    if (s[u][v] == 'A' && !bel[v]) dfs(v);
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%s", s + i);
  for (int u = 0; u < n; ++u)
    if (!bel[u]) {
      ++m;
      dfs(u);
    }
  for (int u = 0; u < n; ++u)
    for (int v = 0; v < n; ++v) {
      if (s[u][v] == 'A' && bel[u] != bel[v]) {
        puts("-1");
        exit(0);
      }
      if (s[u][v] == 'X' && bel[u] == bel[v]) {
        puts("-1");
        exit(0);
      }
    }
  for (int u = 0; u < n; ++u) ++sz[bel[u]];
  for (int u = 1; u <= m; ++u)
    if (sz[u] >= 2)
      id[u] = k++;
    else
      id[u] = -1;
  if (!k) {
    printf("%d\n", n - 1);
    return 0;
  }
  for (int u = 0; u < n; ++u)
    for (int v = 0; v < n; ++v)
      if (s[u][v] == 'X') {
        int x = id[bel[u]], y = id[bel[v]];
        if (~x && ~y) g[x][y] = g[y][x] = 1;
      }
  for (int i = 0; i < k; ++i) p[i] = i;
  int T = 0, ans = k;
  while (T <= 700000000) {
    random_shuffle(p, p + k);
    int cnt = 0;
    for (int i = 0; i < k; ++i) {
      ++T;
      int &u = p[i];
      if (!i) {
        col[i] = cnt++;
        continue;
      }
      int S = 0;
      for (int j = 0; j < i; ++j) {
        ++T;
        if (g[u][p[j]]) S |= (1 << col[j]);
      }
      S ^= (1 << cnt + 1) - 1;
      for (;;) {
        int t = rnd % (cnt + 1);
        T += 3;
        if ((S >> t) & 1) {
          col[i] = t;
          break;
        }
      }
      cnt = max(cnt, col[i] + 1);
    }
    ++T;
    ans = min(ans, cnt);
  }
  --ans;
  for (int i = 1; i <= m; ++i)
    if (sz[i] >= 2) ans += sz[i];
  printf("%d\n", ans + m - k);
  return 0;
}
