#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1010;
const int mod = 1e9 + 7;
int n;
int tab[N][N];
int pr[N][N], le[N][N];
vector<vector<pair<int, int>>> rows, cols, pra, lew;
vector<vector<int>> zrows, zcols, zpra, zlew;
int pov(int base, int exp = mod - 2) {
  int ret = 1;
  while (exp) {
    if (exp & 1) ret = 1ll * ret * base % mod;
    base = 1ll * base * base % mod;
    exp >>= 1;
  }
  return ret;
}
pair<int, int> dod(pair<int, int> a, pair<int, int> b) {
  return {a.first + b.first, a.second + b.second};
}
pair<int, int> ode(pair<int, int> a, pair<int, int> b) {
  return {a.first - b.first, a.second - b.second};
}
pair<int, int> dud(pair<int, int> a, int b) {
  if (b == 2) a.first++;
  if (b == 3) a.second++;
  return a;
}
pair<int, int> udu(pair<int, int> a, int b) {
  if (b == 2) a.first--;
  if (b == 3) a.second--;
  return a;
}
pair<int, int> row(int kt, int l, int r) {
  if (r + 1 >= ((int)(rows[kt]).size()) || l < 0) return {-INF, 0};
  if (zrows[kt][r + 1] - zrows[kt][l] > 0) return {-INF, 0};
  return ode(rows[kt][r + 1], rows[kt][l]);
}
pair<int, int> col(int kt, int l, int r) {
  if (r + 1 >= ((int)(cols[kt]).size()) || l < 0) return {-INF, 0};
  if (zcols[kt][r + 1] - zcols[kt][l] > 0) return {-INF, 0};
  return ode(cols[kt][r + 1], cols[kt][l]);
}
pair<int, int> praq(int kt, int l, int r) {
  if (r + 1 >= ((int)(pra[kt]).size()) || l < 0) return {-INF, 0};
  if (zpra[kt][r + 1] - zpra[kt][l] > 0) return {-INF, 0};
  return ode(pra[kt][r + 1], pra[kt][l]);
}
pair<int, int> lewq(int kt, int l, int r) {
  if (r + 1 >= ((int)(lew[kt]).size()) || l < 0) return {-INF, 0};
  if (zlew[kt][r + 1] - zlew[kt][l] > 0) return {-INF, 0};
  return ode(lew[kt][r + 1], lew[kt][l]);
}
int copr(int i, int j) { return j - i + (n - 1); }
int cole(int i, int j) { return i + j; }
pair<int, int> cross(int i, int j, int dl, int typ) {
  pair<int, int> ans = {0, 0};
  if (typ == 0) {
    ans = dod(row(i, j - dl, j + dl), col(j, i - dl, i + dl));
  } else {
    ans = dod(praq(copr(i, j), pr[i][j] - dl, pr[i][j] + dl),
              lewq(cole(i, j), le[i][j] - dl, le[i][j] + dl));
  }
  return udu(ans, tab[i][j]);
}
pair<int, int> maks(pair<int, int> a, pair<int, int> b) {
  pair<int, int> a2 = a, b2 = b;
  if (a.first < 0 || b.first < 0) return max(a, b);
  int jed = min(a.first, b.first);
  a.first -= jed;
  b.first -= jed;
  int dwa = min(a.second, b.second);
  a.second -= dwa;
  b.second -= dwa;
  if (a.first > 0 && a.second > 0) return a2;
  if (b.first > 0 && b.second > 0) return b2;
  if (a.second > 0) swap(a, b), swap(a2, b2);
  if (pow(2, a.first) > pow(3, b.second))
    return a2;
  else
    return b2;
}
void solve() {
  bool zapal = 0;
  pair<int, int> ans = {-INF, 0};
  cin >> n;
  rows.resize(n, vector<pair<int, int>>(1));
  cols.resize(n, vector<pair<int, int>>(1));
  pra.resize(n * 2 - 1, vector<pair<int, int>>(1));
  lew.resize(n * 2 - 1, vector<pair<int, int>>(1));
  zrows.resize(n, vector<int>(1, 0));
  zcols.resize(n, vector<int>(1, 0));
  zpra.resize(n * 2 - 1, vector<int>(1, 0));
  zlew.resize(n * 2 - 1, vector<int>(1, 0));
  for (int i = (0); i < n; i++)
    for (int j = (0); j < n; j++) {
      char c;
      cin >> c;
      tab[i][j] = c - '0';
      zapal |= tab[i][j];
    }
  for (int i = (0); i < n; i++)
    for (int j = (0); j < n; j++) {
      pr[i][j] = ((int)(pra[copr(i, j)]).size()) - 1;
      le[i][j] = ((int)(lew[cole(i, j)]).size()) - 1;
      rows[i].push_back(dud(rows[i].back(), tab[i][j]));
      cols[j].push_back(dud(cols[j].back(), tab[i][j]));
      pra[copr(i, j)].push_back(dud(pra[copr(i, j)].back(), tab[i][j]));
      lew[cole(i, j)].push_back(dud(lew[cole(i, j)].back(), tab[i][j]));
      zrows[i].push_back(zrows[i].back() + (tab[i][j] == 0));
      zcols[j].push_back(zcols[j].back() + (tab[i][j] == 0));
      zpra[copr(i, j)].push_back(zpra[copr(i, j)].back() + (tab[i][j] == 0));
      zlew[cole(i, j)].push_back(zlew[cole(i, j)].back() + (tab[i][j] == 0));
    }
  for (int i = (0); i < n; i++)
    for (int j = (0); j < n; j++)
      for (int k = (0); k < 2; k++) {
        int l = 0, r = n + 2, mid;
        while (l + 1 < r) {
          mid = (l + r) / 2;
          if (cross(i, j, mid, k).first > -1)
            l = mid;
          else
            r = mid;
        }
        ans = maks(ans, cross(i, j, l, k));
      }
  if (!zapal) {
    cout << 0 << '\n';
  } else if (ans.first < 0) {
    cout << -1 << '\n';
  } else {
    cout << 1ll * pov(2, ans.first) * pov(3, ans.second) % mod << '\n';
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
