#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string get() {
  char ch[1000010];
  scanf("%s", ch);
  return ch;
}
const int MAX_LG = 21;
const long long maxn = 1e3 + 10;
const long long base = 29;
const long long mod = 1e9 + 7;
const long long INF = 1e9;
long long n, m;
long long a[maxn][maxn];
long long dpR[maxn][maxn], dpD[maxn][maxn];
pair<long long, long long> st[1000500];
long long l, r;
long long maxi;
vector<long long> res;
bool mark[maxn][maxn];
inline void fail() {
  cout << -1 << "\n";
  exit(0);
}
long long dx[] = {0, 0, 1, -1};
long long dy[] = {1, -1, 0, 0};
long long x, y;
bool inside(long long x, long long y) {
  return (x >= 0 && x < n && y >= 0 && y < m);
}
vector<pair<long long, long long> > visited;
inline void check(long long ln) {
  visited.clear();
  l = r = 0;
  st[r++] = {x, y};
  while (l < r) {
    long long x = st[l].first, y = st[l].second;
    l++;
    visited.push_back({x, y});
    mark[x][y] = true;
    for (long long i = 0; i < 4; i++) {
      long long x2 = x + dx[i], y2 = y + dy[i];
      if (inside(x2, y2) && !mark[x2][y2] && a[x2][y2]) {
        x2 = x + dx[i] * ln, y2 = y + dy[i] * ln;
        if (!inside(x2, y2)) return;
        if (!mark[x2][y2] && a[x2][y2]) {
          st[r++] = {x2, y2};
          mark[x2][y2] = true;
        }
      }
    }
  }
  long long oddCnt = 0;
  for (long long i = 0; i < visited.size(); i++) {
    long long x = visited[i].first, y = visited[i].second;
    long long deg = 0;
    for (long long j = 0; j < 4; j++) {
      long long x2 = x + dx[j], y2 = y + dy[j];
      if (!inside(x2, y2) || !a[x2][y2]) continue;
      deg++;
    }
    if (deg & 1) oddCnt++;
  }
  if (oddCnt > 2) return;
  for (long long i = 0; i < visited.size(); i++) {
    long long x = visited[i].first, y = visited[i].second;
    for (long long j = 0; j < 4; j++) {
      long long x2 = x + dx[j], y2 = dy[j] + y;
      if (!inside(x2, y2) || !a[x2][y2]) continue;
      x2 = x + dx[j] * ln, y2 = dy[j] * ln + y;
      for (long long f = min(x, x2); f <= max(x, x2); f++) {
        for (long long s = min(y, y2); s <= max(y, y2); s++) {
          mark[f][s] = true;
        }
      }
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (a[i][j] && !mark[i][j]) return;
    }
  }
  res.push_back(ln);
}
inline void findThepoint() {
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (a[i][j]) {
        x = i, y = j;
        return;
      }
    }
  }
}
inline bool connectivity() {
  l = r = 0;
  st[r++] = {x, y};
  while (l < r) {
    long long x = st[l].first, y = st[l].second;
    l++;
    mark[x][y] = true;
    for (long long i = 0; i < 4; i++) {
      long long x2 = x + dx[i], y2 = y + dy[i];
      if (inside(x2, y2) && a[x2][y2] && !mark[x2][y2]) {
        st[r++] = {x2, y2};
        mark[x2][y2] = true;
      }
    }
  }
  return true;
}
inline void reset() {
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      mark[i][j] = false;
    }
  }
}
int32_t main() {
  n = in(), m = in();
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      a[i][j] = in();
    }
  }
  findThepoint();
  if (!connectivity()) {
    fail();
  }
  reset();
  for (long long i = 0; i < n; i++)
    for (long long j = m - 1; j >= 0; j--)
      dpR[i][j] = bool(a[i][j] == 1) * (1 + dpR[i][j + 1]);
  for (long long i = n - 1; i >= 0; i--)
    for (long long j = 0; j < m; j++)
      dpD[i][j] = bool(a[i][j] == 1) * (1 + dpD[i + 1][j]);
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++)
      maxi = max(maxi, max(dpR[i][j], dpD[i][j]));
  maxi--;
  for (long long i = 2; i <= sqrt(maxi); i++) {
    if (maxi % i) continue;
    long long j = maxi / i;
    reset();
    if (j != i) {
      check(j);
      reset();
    }
    check(i);
  }
  reset();
  if (maxi > 1) check(maxi);
  if (!res.size()) fail();
  sort(res.begin(), res.end());
  for (long long i = 0; i < res.size(); i++) cout << res[i] << " ";
  cout << "\n";
  return 0;
}
