#include <bits/stdc++.h>
using namespace std;
const int T = 1e3 + 5;
int deg[T][T], fix[T][T], s[T][T], d[T][T];
int raod, sul, have, m, p, k, i, j, n;
vector<int> v;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int getsum(int xx1, int yy1, int xx2, int yy2) {
  return s[xx2][yy2] - s[xx1 - 1][yy2] - s[xx2][yy1 - 1] + s[xx1 - 1][yy1 - 1];
}
int look(int xx1, int yy1, int xx2, int yy2) {
  if (xx1 > xx2) swap(xx1, xx2);
  if (yy1 > yy2) swap(yy1, yy2);
  return (getsum(xx1, yy1, xx2, yy2) == (xx2 - xx1 + 1) * (yy2 - yy1 + 1));
}
void go(int x, int y) {
  fix[x][y] = raod;
  sul++;
  for (int t = 0; t < 4; t++) {
    int newx = x + dx[t] * k;
    int newy = y + dy[t] * k;
    if (newx < 1 || newx > n) continue;
    if (newy < 1 || newy > m) continue;
    if (!look(x, y, newx, newy)) continue;
    deg[x][y]++;
    if (fix[newx][newy] == raod) continue;
    go(newx, newy);
  }
}
bool check(int k) {
  int i, j, I, J;
  bool flag = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) deg[i][j] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      if (d[i][j]) {
        I = i;
        J = j;
        flag = 1;
        break;
      }
    if (flag) break;
  }
  int num = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if ((abs(i - I) % k == 0) && (abs(J - j) % k == 0))
        if (d[i][j]) num++;
  raod++;
  sul = 0;
  have = 0;
  go(I, J);
  if (sul != num) return 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if ((abs(i - I) % k == 0) && (abs(J - j) % k == 0))
        if (deg[i][j] & 1) {
          have++;
        }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (!((abs(i - I) % k == 0) && (abs(J - j) % k == 0)))
        if (d[i][j]) {
          if ((!(abs(i - I) % k == 0)) && (!(abs(j - J) % k == 0))) return 0;
        }
  return (have == 0 || have == 2);
}
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) scanf("%d", &d[i][j]);
    scanf("\n");
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + d[i][j];
  bool flag = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      if (d[i][j]) {
        p = j;
        while (d[i][p] == 1) p++;
        p -= j + 1;
        flag = 1;
        break;
      }
    if (flag) break;
  }
  int P = p;
  flag = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++)
      if (d[i][j]) {
        p = i;
        while (d[p][j] == 1) p++;
        p -= i + 1;
        flag = 1;
        break;
      }
    if (flag) break;
  }
  p = (p > P ? p : P);
  for (k = 2; k <= p; k++)
    if (p % k == 0)
      if (check(k)) v.push_back(k);
  if (v.size()) {
    for (i = 0; i < v.size(); i++) cout << v[i] << " ";
    cout << endl;
    return 0;
  } else
    cout << -1 << endl;
}
