#include <bits/stdc++.h>
using namespace std;
int ma[4000][4000];
int used[4000][4000];
const pair<int, int> gone[4] = {make_pair(0, 1), make_pair(1, 0),
                                make_pair(0, -1), make_pair(-1, 0)};
int num = 0;
int n, m;
vector<int> col[4000], row[4000];
int color = 1;
int nm = 0;
void dfs(int x, int y, int len) {
  if (used[x][y] != color) nm++;
  used[x][y] = color;
  int cnt = 0;
  for (int i = 0; i < (4); i++) {
    int nx = x + gone[i].first * len;
    int ny = y + gone[i].second * len;
    if (nx >= 0 && ny >= 0 && nx < n && ny < m && ma[nx][ny]) {
      int minx = min(nx, x);
      int maxx = max(nx, x);
      int miny = min(ny, y);
      int maxy = max(ny, y);
      int f[2] = {minx, maxx}, s[2] = {miny, maxy};
      bool can = false;
      for (int j = minx; j <= maxx; j++) {
        for (int k = miny; k <= maxy; k++) {
          if (!ma[j][k]) {
            can = true;
            break;
          }
        }
        if (can) break;
      }
      if (can) continue;
      cnt++;
      bool yes = used[nx][ny] != color;
      for (int j = minx; j <= maxx; j++) {
        for (int k = miny; k <= maxy; k++) {
          if (used[j][k] != color) nm++;
          used[j][k] = color;
        }
      }
      if (yes) dfs(nx, ny, len);
    }
  }
  if (cnt & 1) num++;
}
int is[4000];
int main() {
  cin >> n >> m;
  memset(ma, 0, sizeof ma);
  int need = 0;
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (m); j++) {
      cin >> ma[i][j];
      need += ma[i][j];
    }
  if (need <= 1) {
    cout << -1;
    return 0;
  }
  int sx, sy;
  bool fi = false;
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      if (ma[i][j]) {
        sx = i;
        sy = j;
        fi = true;
        break;
      }
    }
    if (fi) break;
  }
  for (int i = 0; i < (n); i++) {
    for (int j = 0; j < (m); j++) {
      if (ma[i][j] && !ma[i][j + 1]) {
        row[i].push_back(j);
      }
    }
  }
  for (int j = 0; j < (m); j++) {
    for (int i = 0; i < (n); i++) {
      if (ma[i][j] && !ma[i + 1][j]) {
        col[j].push_back(i);
      }
    }
  }
  memset(used, 0, sizeof used);
  memset(is, 0, sizeof is);
  for (int i = max(n, m); i >= 2; i--) {
    if (is[i]) continue;
    color++;
    nm = 0;
    num = 0;
    dfs(sx, sy, i);
    if ((num == 0 || num == 2) && nm == need) {
      for (int j = 1; j * j <= i; j++) {
        if (i % j == 0) is[j]++, is[i / j]++;
      }
    }
  }
  bool yes = false;
  for (int i = 2; i <= max(n, m); i++) {
    if (!is[i]) continue;
    yes = true;
    cout << i << " ";
  }
  if (!yes) cout << -1;
}
