#include <bits/stdc++.h>
using namespace std;
class e {
 public:
  int u, v, w;
  e() {}
  e(int u, int v, int w) : u(u), v(v), w(w) {}
};
class p {
 public:
  int r, c, idx;
  p(int r, int c, int idx) : r(r), c(c), idx(idx) {}
};
bool visit[1002][1002];
int b[1001][1002];
int n, m, dr[4] = {-1, 0, 1, 0}, dc[4] = {0, 1, 0, -1};
int ps[1002][1002];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
bool pos(int r, int c) {
  return 1 <= r && r <= n && 1 <= c && c <= m && b[r][c] == 1;
}
void bfs(int r, int c) {
  queue<pair<int, int> > q;
  q.push(pair<int, int>(r, c));
  visit[r][c] = true;
  while (!q.empty()) {
    int cr = q.front().first, cc = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nr = cr + dr[i], nc = cc + dc[i];
      if (pos(nr, nc) && !visit[nr][nc]) {
        visit[nr][nc] = true;
        q.push(pair<int, int>(nr, nc));
      }
    }
  }
}
vector<p> cand;
vector<int> con[1000000];
bool cmpr(const p& i, const p& j) {
  if (i.r != j.r)
    return i.r < j.r;
  else
    return i.c < j.c;
}
bool cmpc(const p& i, const p& j) {
  if (i.c != j.c)
    return i.c < j.c;
  else
    return i.r < j.r;
}
bool cmpi(const p& i, const p& j) { return i.idx < j.idx; }
vector<e> edge;
int deg[1000000];
bool vis[1000000];
void bfs(int s) {
  queue<int> q;
  vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < con[u].size(); i++) {
      int v = con[u][i];
      if (!vis[v]) {
        vis[v] = true;
        q.push(v);
      }
    }
  }
}
int sum(int r1, int c1, int r2, int c2) {
  return ps[r2][c2] - ps[r1 - 1][c2] - ps[r2][c1 - 1] + ps[r1 - 1][c1 - 1];
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &b[i][j]);
      ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1];
      if (b[i][j] == 1) ps[i][j]++;
    }
  }
  int tt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      if (pos(i, j) && !visit[i][j]) {
        bfs(i, j);
        tt++;
      }
  }
  if (tt > 1) {
    puts("-1");
    return 0;
  }
  int idx = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++)
      if (b[i][j] == 1) {
        int cr = 0, cc = 0;
        for (int k = 0; k < 4; k++) {
          int nr = i + dr[k], nc = j + dc[k];
          if (pos(nr, nc)) {
            if (k % 2 == 0)
              cc++;
            else
              cr++;
          }
        }
        if ((cr && cc) || (cr + cc == 1)) cand.push_back(p(i, j, idx++));
      }
  }
  sort(cand.begin(), cand.end(), cmpr);
  for (int i = 1; i < cand.size(); i++) {
    if (cand[i - 1].r != cand[i].r) continue;
    int r = cand[i].r, c1 = cand[i - 1].c, c2 = cand[i].c;
    if (sum(r, c1, r, c2) != c2 - c1 + 1) continue;
    con[cand[i - 1].idx].push_back(cand[i].idx);
    con[cand[i].idx].push_back(cand[i - 1].idx);
  }
  sort(cand.begin(), cand.end(), cmpc);
  for (int i = 1; i < cand.size(); i++) {
    if (cand[i - 1].c != cand[i].c) continue;
    int r1 = cand[i - 1].r, r2 = cand[i].r, c = cand[i].c;
    if (sum(r1, c, r2, c) != r2 - r1 + 1) continue;
    con[cand[i - 1].idx].push_back(cand[i].idx);
    con[cand[i].idx].push_back(cand[i - 1].idx);
  }
  sort(cand.begin(), cand.end(), cmpi);
  int mx = 0, mn = (1 << 30), g = 0;
  for (int i = 0; i < idx; i++) {
    for (int j = 0; j < con[i].size(); j++) {
      int u = i, v = con[i][j],
          w = abs(cand[u].r - cand[v].r) + abs(cand[u].c - cand[v].c);
      if (u < v) {
        edge.push_back(e(u, v, w));
        mx = max(mx, w);
        mn = min(mn, w);
        g = gcd(g, w);
      }
    }
  }
  bfs(0);
  for (int i = 0; i < idx; i++) {
    if (!vis[i]) {
      puts("-1");
      return 0;
    }
  }
  if (mn == (1 << 30) || g == 1) {
    puts("-1");
    return 0;
  }
  for (int i = 0; i < edge.size(); i++) {
    int u = edge[i].u, v = edge[i].v, w = edge[i].w;
    deg[u]++;
    deg[v]++;
  }
  int oc = 0;
  for (int i = 0; i < idx; i++) {
    if (deg[i] & 1) oc++;
  }
  if (oc != 0 && oc != 2) {
    puts("-1");
    return 0;
  }
  for (int i = 2; i <= g; i++) {
    if (g % i == 0) printf("%d ", i);
  }
  puts("");
  return 0;
}
