#include <bits/stdc++.h>
using namespace std;
int m, n, k, one = 0, ar = 0, ac, oddDeg;
int sr[1001][1001], sc[1001][1001], visited[1001][1001];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void doc() {
  scanf("%d%d", &m, &n);
  memset(sr, 0, sizeof(sr));
  memset(sc, 0, sizeof(sc));
  int x;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      scanf("%d", &x);
      if (x == 1 && ar == 0) {
        ar = i;
        ac = j;
      }
      one += x;
      sr[i][j] = sr[i][j - 1] + x;
      sc[j][i] = sc[j][i - 1] + x;
    }
}
bool check(int r, int c) { return (r >= 1 && r <= m && c >= 1 && c <= n); }
int sum(int i, int j, int r, int c) {
  if (i > r) swap(i, r);
  if (j > c) swap(j, c);
  return i == r ? sr[i][c] - sr[i][j - 1] : sc[j][r] - sc[j][i - 1];
}
int dfs(int i, int j) {
  visited[i][j] = k;
  int one2 = 2;
  int deg = 0;
  for (int d = 0; d < 4; d++) {
    int r = i + dx[d] * k;
    int c = j + dy[d] * k;
    if (check(r, c) && sum(i, j, r, c) == k + 1) {
      one2 += k - 1;
      deg++;
      if (visited[r][c] != k) one2 += dfs(r, c);
    }
  }
  oddDeg += deg & 1;
  return one2;
}
void xuli() {
  bool ok = false;
  memset(visited, 0, sizeof(visited));
  for (k = 2; one > 1 && k <= max(m, n); k++) {
    oddDeg = 0;
    if (dfs(ar, ac) == one * 2 && (oddDeg == 0 || oddDeg == 2)) {
      if (ok) printf(" ");
      printf("%d", k);
      ok = true;
    }
  }
  if (!ok) printf("-1");
  printf("\n");
}
int main() {
  doc();
  xuli();
  return 0;
}
