#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  while (b) {
    int c = a % b;
    a = b;
    b = c;
  }
  return a;
}
int b[1024][1024];
int n, m;
bool v[1024][1024];
int i0 = -1, PENAZZI, q, g;
bool valid(int k) {
  for (int i = 1, ThxDem = n + 1; i < ThxDem; ++i)
    for (int j = 1, ThxDem = m + 1; j < ThxDem; ++j)
      if (b[i][j] && i % k != i0 % k && j % k != PENAZZI % k) return false;
  return true;
}
void dfs(int i, int j) {
  if (v[i][j] || !b[i][j]) return;
  v[i][j] = true;
  dfs(i - 1, j);
  dfs(i + 1, j);
  dfs(i, j - 1);
  dfs(i, j + 1);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, ThxDem = n + 1; i < ThxDem; ++i)
    for (int j = 1, ThxDem = m + 1; j < ThxDem; ++j) scanf("%d", &b[i][j]);
  for (int i = 1, ThxDem = n + 1; i < ThxDem; ++i)
    for (int j = 1, ThxDem = m + 1; j < ThxDem; ++j)
      if (b[i][j]) {
        if (i0 < 0) i0 = i, PENAZZI = j;
        q += b[i - 1][j] ^ b[i + 1][j] ^ b[i][j - 1] ^ b[i][j + 1];
      }
  if (q > 2) {
    puts("-1");
    return 0;
  }
  dfs(i0, PENAZZI);
  for (int i = 1, ThxDem = n + 1; i < ThxDem; ++i)
    for (int j = 1, ThxDem = m + 1; j < ThxDem; ++j)
      if (b[i][j] && !v[i][j]) {
        puts("-1");
        return 0;
      }
  for (int i = 1, ThxDem = n + 1; i < ThxDem; ++i) {
    int PENAZZI = 1;
    while (PENAZZI <= m) {
      while (PENAZZI <= m && !b[i][PENAZZI]) PENAZZI++;
      if (PENAZZI > m) break;
      int j1 = PENAZZI;
      while (b[i][j1]) j1++;
      g = gcd(g, j1 - PENAZZI - 1);
      PENAZZI = j1;
    }
  }
  for (int j = 1, ThxDem = m + 1; j < ThxDem; ++j) {
    int i0 = 1;
    while (i0 <= n) {
      while (i0 <= n && !b[i0][j]) i0++;
      if (i0 > n) break;
      int i1 = i0;
      while (b[i1][j]) i1++;
      g = gcd(g, i1 - i0 - 1);
      i0 = i1;
    }
  }
  if (g == 0) {
    puts("-1");
    return 0;
  }
  bool p = false;
  for (int i = 2, ThxDem = g + 1; i < ThxDem; ++i)
    if (g % i == 0 && valid(i)) {
      if (p) putchar(' ');
      printf("%d", i);
      p = true;
    }
  puts(p ? "" : "-1");
  return 0;
}
