#include <algorithm>
#include <iostream>
#include <cstring>
#include <sstream>
#include <vector>
#include <cmath>
#include <set>
#include <map>

using namespace std;

#define int long long

#define ff first
#define ss second
#define mp make_pair
#define sqr(x) ((x)*(x))

typedef long long ll;
typedef pair <int, int> pie;

const int maxN = 1600 + 50;
const int off = 4;

int a[maxN][maxN];

vector <pie> comp;

void dfs (int i, int j, int fc, int ec) {
	comp.push_back (pie (i, j));
	a[i][j] = ec;
	for (int ax = -1; ax <= 1; ax++)
		for (int ay = -1; ay <= 1; ay++)
			if (a[i + ax][j + ay] == fc) dfs (i + ax, j + ay, fc, ec);
}

int count () {
	int mx = maxN, Mx = 0, my = maxN, My = 0;
	for (int i = 0; i < comp.size(); i++) {
		int x = comp[i].ff, y = comp[i].ss;
		mx = min (mx, x); Mx = max (Mx, x);
		my = min (my, y); My = max (My, y);
		int cnt = 0, best = 0;
		for (int k = -off; k < off; k++) if (a[x + k][y - off]) cnt++, best = max (cnt, best); else cnt = 0;
		for (int k = -off; k < off; k++) if (a[x + off][y + k]) cnt++, best = max (cnt, best); else cnt = 0;
		for (int k = off; k > -off; k--) if (a[x + k][y + off]) cnt++, best = max (cnt, best); else cnt = 0;
		for (int k = off; k > -off; k--) if (a[x - off][y + k]) cnt++, best = max (cnt, best); else cnt = 0;
		for (int k = -off; k < off; k++) if (a[x + k][y - off]) cnt++, best = max (cnt, best); else cnt = 0;
		if (best <= 6) a[x][y] = 3;
	}
	int cnt = 0;
	for (int i = mx; i <= Mx; i++)
		for (int j = my; j <= My; j++) {
			if (a[i][j] == 3) dfs (i, j, 3, 4), cnt++;
		}
	//cerr << comp[0].ff << ' ' << comp[0].ss << ' ' << cnt << endl;
	return cnt;
}

main() {
	ios::sync_with_stdio (false);
	
	int h, w;
	cin >> h >> w;
	for (int i = off; i < h + off; i++)
		for (int j = off; j < w + off; j++)
			cin >> a[i][j];

	vector <int> res;
	for (int i = 0; i < h + off; i++)
		for (int j = 0; j < w + off; j++) if (a[i][j] == 1) {
			comp.clear();
			dfs (i, j, 1, 2);
			res.push_back (count ());
		}
	
	sort (res.begin(), res.end());
	cout << res.size() << endl;
	for (int i = 0; i < res.size(); i++) cout << res[i] << ' '; cout << endl;
	/*for (int i = off; i < h + off; i++, cout << endl)
		for (int j = off; j < w + off; j++)
			if (a[i][j] == 4) cout << 'X'; else cout << ' ';*/

	return 0;
}
