#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(pair<int, int> l, pair<int, int> r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
pair<int, int> ul = make_pair(-1, -1), lr, oddPos = make_pair(-1, -1);
pair<int, int> mov[] = {make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0),
                        make_pair(0, -1)};
int n, m, oddcount = 0;
int a[1000][1000], curDir[1000][1000];
bool valid(pair<int, int> pos) {
  return pos.first >= 0 && pos.first < n && pos.second >= 0 && pos.second < m &&
         a[pos.first][pos.second];
}
bool tryK(int k) {
  if ((lr.first - ul.first) % k != 0 || abs(lr.second - ul.second) % k != 0)
    return false;
  int ymod = oddPos.first % k;
  int xmod = oddPos.second % k;
  vector<pair<int, int> > front(1, oddPos);
  fill(curDir[0], curDir[0] + 1000 * 1000, 0);
  while (front.size() > 0) {
    auto cur = front.back();
    if (curDir[cur.first][cur.second] == 4) {
      front.pop_back();
    } else {
      int cdir = curDir[cur.first][cur.second];
      curDir[cur.first][cur.second]++;
      if (cur.first % k != ymod && cdir % 2 == 1) continue;
      if (cur.second % k != xmod && cdir % 2 == 0) continue;
      auto next = cur + mov[cdir];
      if (!valid(next) || (cdir + 2) % 4 < curDir[next.first][next.second])
        continue;
      front.push_back(next);
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] && curDir[i][j] != 4) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 1) {
        if (ul.first == -1) ul = make_pair(i, j);
        lr = make_pair(i, j);
      }
    }
  try {
    if (ul == lr) throw 1;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (valid(make_pair(i, j))) {
          int cnt = 0;
          for (int d = 0; d < 4; d++)
            if (valid(make_pair(i, j) + mov[d])) cnt++;
          if (cnt % 2) {
            oddcount++;
            oddPos = make_pair(i, j);
          }
        }
    if (oddcount != 0 && oddcount != 2) throw 1;
    if (oddPos.first == -1) oddPos = ul;
    vector<int> result;
    for (int i = 2; i <= n; i++)
      if (tryK(i)) result.push_back(i);
    if (result.size() == 0) throw 1;
    cout << result[0];
    for (int i = 1; i < (int)result.size(); i++) cout << ' ' << result[i];
    cout << '\n';
  } catch (int a) {
    cout << "-1\n";
  }
  return 0;
}
