#include <bits/stdc++.h>
using namespace std;
int n, m, odd, root;
int maze[1100][1100];
int Fa[1000010];
bool vis[1100][1100], vet[1100][1100];
vector<int> ans;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
void init() {
  for (int i = 0; i < 1000010; i++) Fa[i] = i;
}
int find(int x) { return Fa[x] = Fa[x] == x ? x : find(Fa[x]); }
void dfs(int x, int y) {
  vis[x][y] = 1;
  Fa[find(x * m + y)] = root;
  int sta = 0;
  for (int i = 0; i < 4; i++) {
    int xx = x + (i == 0) - (i == 1), yy = y + (i == 2) - (i == 3);
    if (xx < 0 || xx >= n || yy < 0 || yy >= m || !maze[xx][yy]) continue;
    if (!vis[xx][yy]) dfs(xx, yy);
    sta |= (1 << i);
  }
  if (sta == 1 || sta == 2 || sta == 4 || sta == 8 || sta == 7 || sta == 11 ||
      sta == 13 || sta == 14)
    odd++;
  if (sta != 0 && sta != 3 && sta != 12) vet[x][y] = 1;
}
int main() {
  init();
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", maze[i] + j);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (maze[i][j] == 1) {
        root = find(i * m + j);
        dfs(i, j);
        goto P;
      }
P:;
  if (odd != 0 && odd != 2) return 0 * puts("-1");
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!maze[i][j]) continue;
      if (find(i * m + j) != root) return 0 * puts("-1");
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!vet[i][j]) continue;
      int ma[4] = {0, 0, 0, 0};
      for (int k = 0; k < 4; k++) {
        int x = i + (k == 0) - (k == 1), y = j + (k == 2) - (k == 3), t = 0;
        if (x < 0 || x >= n || y < 0 || y >= m || !maze[x][y]) continue;
        if (k == 0)
          while (x < n && maze[x][y] && !vet[x][y]) x++, t++;
        if (k == 1)
          while (x >= 0 && maze[x][y] && !vet[x][y]) x--, t++;
        if (k == 2)
          while (y < m && maze[x][y] && !vet[x][y]) y++, t++;
        if (k == 3)
          while (y >= 0 && maze[x][y] && !vet[x][y]) y--, t++;
        ma[k] = t + 1;
      }
      for (int k = 0; k < 4; k++)
        if (ma[k]) ans.push_back(ma[k]);
    }
  }
  if (ans.empty()) return 0 * puts("-1");
  int res = ans[0], len = (int)ans.size();
  for (int i = 1; i < len; i++) res = gcd(res, ans[i]);
  if (res == 1) return 0 * puts("-1");
  for (int i = 2; i <= res; i++)
    if (res % i == 0) printf("%d ", i);
  return 0;
}
