#include <bits/stdc++.h>
using namespace std;
int n, l, i, j, k, o, m[1010][1010];
bool used[1010][1010];
vector<int> a, b;
int gcd(int a, int b) {
  while (b > 0) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
void dfs(int x, int y) {
  used[x][y] = true;
  if (m[x - 1][y] == 1 && used[x - 1][y] == false) dfs(x - 1, y);
  if (m[x + 1][y] == 1 && used[x + 1][y] == false) dfs(x + 1, y);
  if (m[x][y - 1] == 1 && used[x][y - 1] == false) dfs(x, y - 1);
  if (m[x][y + 1] == 1 && used[x][y + 1] == false) dfs(x, y + 1);
}
int main() {
  cin >> n >> l;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= l; j++) cin >> m[i][j];
  o = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= l; j++)
      if (m[i][j] == 1) {
        k = 0;
        if (m[i - 1][j] == 1 || m[i + 1][j] == 1) k++;
        if (m[i][j - 1] == 1 || m[i][j + 1] == 1) k++;
        if (k == 2) {
          a.push_back(i);
          b.push_back(j);
        }
        k = 0;
        if (m[i - 1][j] == 1) k++;
        if (m[i][j - 1] == 1) k++;
        if (m[i + 1][j] == 1) k++;
        if (m[i][j + 1] == 1) k++;
        if (k == 1) {
          a.push_back(i);
          b.push_back(j);
        }
        if (k % 2 == 1) o++;
      }
  if (!(o == 0 || o == 2)) {
    printf("-1\n");
    return 0;
  }
  if (a.size() == 0 && b.size() == 0) {
    printf("-1\n");
    return 0;
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  for (i = a.size() - 1; i >= 0; i--) a[i] -= a[0];
  for (i = b.size() - 1; i >= 0; i--) b[i] -= b[0];
  k = max(a.back(), b.back());
  for (i = a.size() - 1; i > 0; i--) k = gcd(k, a[i]);
  for (i = b.size() - 1; i > 0; i--) k = gcd(k, b[i]);
  if (k == 1) {
    printf("-1\n");
    return 0;
  }
  o = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= l; j++)
      if (m[i][j] == 1 && used[i][j] == false) {
        dfs(i, j);
        o++;
      }
  if (o > 1) {
    printf("-1\n");
    return 0;
  }
  for (i = 2; i < k; i++)
    if (k % i == 0) cout << i << " ";
  cout << i << "\n";
  return 0;
}
