#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, a[N][N], s[N][N], sx, sy, ml;
vector<int> l, ans;
vector<pair<int, int> > edges;
int v[N * N];
int mark[N][N], cnt, nbr;
int number(int x, int y) { return (x - 1) * m + y; }
void init() {
  memset(mark, 0, sizeof(mark));
  memset(v, 0, sizeof(v));
  edges.clear();
  cnt = 0;
}
bool in(int x, int y) {
  if (x < 0 || x > n) return 0;
  if (y < 0 || y > m) return 0;
  return 1;
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int sum(int x, int y, int x2, int y2) {
  int sum = s[x2][y2] - s[x - 1][y2] - s[x2][y - 1] + s[x - 1][y - 1];
  int sum2 = s[x][y] - s[x2 - 1][y] - s[x][y2 - 1] + s[x2 - 1][y2 - 1];
  sum = max(sum, sum2);
  return sum;
}
void dfs(int x, int y, int l) {
  mark[x][y] = 1;
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i] * l;
    int ny = y + dy[i] * l;
    if (in(nx, ny))
      if (sum(nx, ny, x, y) == l + 1) {
        if (mark[nx][ny] == 0) {
          dfs(nx, ny, l);
        }
        int c1 = number(x, y);
        int c2 = number(nx, ny);
        edges.push_back(make_pair(min(c1, c2), max(c1, c2)));
      }
  }
}
void make_graph(int l) {
  sort(edges.begin(), edges.end());
  edges.erase(unique(edges.begin(), edges.end()), edges.end());
  for (int i = 0; i < int(edges.size()); ++i) {
    int x = edges[i].first;
    int y = edges[i].second;
    v[x]++;
    v[y]++;
    cnt += l;
  }
  for (int i = 0; i <= n * m; ++i)
    if (v[i] > 2) cnt -= (v[i] + 1) / 2 - 1;
}
bool good() {
  int co = 0;
  for (int i = 1; i <= m * n; ++i)
    if (v[i] % 2 == 1) ++co;
  if (co == 2) return 1;
  if (co == 0) return 0;
  return -1;
}
bool solve(int l) {
  init();
  dfs(sx, sy, l);
  make_graph(l);
  int val = good();
  cnt += val;
  if (nbr == cnt)
    if (val != -1) return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 1 && !sx && !sy) {
        sx = i;
        sy = j;
      }
      s[i][j] = a[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
      nbr += a[i][j];
    }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j] == 1) {
        int lth = 1;
        while (a[i][j + 1] == 1) ++j, ++lth;
        ml = max(lth, ml);
      }
  for (int j = 1; j <= m; ++j)
    for (int i = 1; i <= n; ++i)
      if (a[i][j] == 1) {
        int lth = 1;
        while (a[i + 1][j] == 1) ++i, ++lth;
        ml = max(lth, ml);
      }
  ml--;
  for (int i = 2; i <= ml; ++i)
    if (ml % i == 0) l.push_back(i);
  for (size_t i = 0; i < l.size(); ++i)
    if (solve(l[i])) ans.push_back(l[i]);
  if (ans.empty())
    cout << "-1";
  else
    for (size_t i = 0; i < ans.size(); ++i) cout << ans[i] << ' ';
  cout << '\n';
}
