#include <bits/stdc++.h>
const int N = 1005;
int a[N][N], duandian[N][N], vis[N][N];
int n, m;
int check(int x, int y) {
  int shang = a[x - 1][y];
  int you = a[x][y + 1];
  int xia = a[x + 1][y];
  int zuo = a[x][y - 1];
  int t = shang + you + xia + zuo;
  if (t == 2) {
    if (shang && xia) t = 0;
    if (zuo && you) t = 0;
  }
  return t;
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void dfs(int x, int y) {
  vis[x][y] = 1;
  int shang = a[x - 1][y] && !vis[x - 1][y];
  int you = a[x][y + 1] && !vis[x][y + 1];
  int xia = a[x + 1][y] && !vis[x + 1][y];
  int zuo = a[x][y - 1] && !vis[x][y - 1];
  if (shang) dfs(x - 1, y);
  if (you) dfs(x, y + 1);
  if (xia) dfs(x + 1, y);
  if (zuo) dfs(x, y - 1);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    memset(a, 0, sizeof(a));
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        scanf("%d", &a[i][j]);
      }
    }
    memset(vis, 0, sizeof(vis));
    int flag = 1;
    for (int i = 1; i <= n && flag; ++i) {
      for (int j = 1; j <= m && flag; ++j) {
        if (a[i][j]) {
          dfs(i, j);
          flag = 0;
        }
      }
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (a[i][j] && !vis[i][j]) {
          puts("-1");
          return 0;
        }
      }
    }
    memset(duandian, 0, sizeof(duandian));
    int cnt = 0;
    for (int i = 1; i <= n && cnt <= 2; ++i) {
      for (int j = 1; j <= m && cnt <= 2; ++j) {
        if (!a[i][j]) continue;
        int t = check(i, j);
        if (t) duandian[i][j] = 1;
        if (t & 1) ++cnt;
      }
    }
    if (cnt == 1 || cnt > 2) {
      puts("-1");
      continue;
    }
    memset(vis, 0, sizeof(vis));
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        if (!duandian[i][j]) continue;
        int x = i, y = j;
        int shang = a[x - 1][y] && !vis[x - 1][y];
        int you = a[x][y + 1] && !vis[x][y + 1];
        int xia = a[x + 1][y] && !vis[x + 1][y];
        int zuo = a[x][y - 1] && !vis[x][y - 1];
        if (shang) {
          int t = x - 1;
          while (a[t][y] && !vis[t][y] && !duandian[t][y]) --t;
          ans = gcd(ans, x - t);
        }
        if (you) {
          int t = y + 1;
          while (a[x][t] && !vis[x][t] && !duandian[x][t]) ++t;
          ans = gcd(ans, t - y);
        }
        if (xia) {
          int t = x + 1;
          while (a[t][y] && !vis[t][y] && !duandian[t][y]) ++t;
          ans = gcd(ans, t - x);
        }
        if (zuo) {
          int t = y - 1;
          while (a[x][t] && !vis[x][t] && !duandian[x][t]) --t;
          ans = gcd(ans, y - t);
        }
      }
    }
    if (ans <= 1) {
      puts("-1");
    } else {
      for (int i = 2; i <= ans; ++i) {
        if (ans % i == 0) printf("%d ", i);
      }
      puts("");
    }
  }
  return 0;
}
