#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e7*2+10;
int n, m;
char g[300][300], g2[300][300];
bool vis[300][300];
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
struct node {
int a, b, step;
};
bool inMap(int x, int y) {
return (x >= 1 && x <= n && y >= 1 && y <= m);
}
int rec[10000000];
int cnt = 0;
void dfs(int x, int y) {
g[x][y] = '0';
cnt++;
for(int i = 0; i < 4; ++i) {
int xx = dx[i] + x;
int yy = dy[i] + y;
if(inMap(xx,yy) && g[xx][yy] == '1') {
dfs(xx,yy);
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
while(cin >> n >> m) {
if(n == 0 && m == 0) break;
int res = 0;
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
cin >> g[i][j];
}
}
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
if(g[i][j] == '1') {
cnt=0;
res++;
dfs(i,j);
rec[cnt]++;
}
}
}
cout << res << endl;
for(int i = 1; i <= 1000000; ++i) {
if(rec[i]) {
cout << i << " " << rec[i] << endl;
}
}
memset(rec,0,sizeof(rec));
}
return 0;
}