#include <bits/stdc++.h>
using namespace std;
int m, n, cnt, d[1000010];
bool a[1010][1010], visited[1010][1010];
bool node(int x, int y) {
  if (!a[x][y]) return false;
  int cnt = a[x - 1][y] + a[x + 1][y] + a[x][y - 1] + a[x][y + 1];
  if (cnt & 1 || cnt == 4)
    return true;
  else
    return a[x - 1][y] != a[x + 1][y] && a[x][y - 1] != a[x][y + 1];
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dfs(int x, int y) {
  int res = 1;
  visited[x][y] = true;
  if (a[x - 1][y] && !visited[x - 1][y]) res += dfs(x - 1, y);
  if (a[x + 1][y] && !visited[x + 1][y]) res += dfs(x + 1, y);
  if (a[x][y - 1] && !visited[x][y - 1]) res += dfs(x, y - 1);
  if (a[x][y + 1] && !visited[x][y + 1]) res += dfs(x, y + 1);
  return res;
}
bool Euler_graph() {
  int cnt = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j])
        cnt += (a[i - 1][j] + a[i + 1][j] + a[i][j - 1] + a[i][j + 1]) & 1;
  return cnt == 0 || cnt == 2;
}
int main() {
  cin >> m >> n;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) cin >> a[i][j], cnt += a[i][j];
  int k = 0;
  for (int i = 1; i <= m; i++) {
    int x = 0;
    for (int j = 1; j <= n; j++)
      if (node(i, j)) {
        if (x) {
          if (k)
            k = gcd(k, j - x);
          else
            k = j - x;
        } else
          x = j;
      }
  }
  for (int j = 1; j <= n; j++) {
    int x = 0;
    for (int i = 1; i <= m; i++)
      if (node(i, j)) {
        if (x) {
          if (k)
            k = gcd(k, i - x);
          else
            k = i - x;
        } else
          x = i;
      }
  }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][j])
        if (dfs(i, j) < cnt)
          goto report_Dima;
        else
          goto next;
next:
  if (!Euler_graph()) goto report_Dima;
  if (k <= 1) goto report_Dima;
  n = 0;
  for (int i = 1; i * i <= k; i++)
    if (k % i == 0) {
      if (i > 1) cout << i << ' ';
      if (k / i > i) d[++n] = k / i;
    }
  for (int i = n; i; i--) cout << d[i] << ' ';
  return 0;
report_Dima:
  cout << -1;
}
