/*
#include <stdio.h>
#define MAXSIZE 102
int m,n,k;
int i,j;
int temp;
int map[MAXSIZE][MAXSIZE] = {};
int a[50000] = {};
void dfs(int x,int y,int cnt) {
map[x][y] = 0;
a[cnt]++;
if(map[x+1][y] == 2) {
dfs(x+1,y,cnt);
}
if(map[x][y+1] == 2) {
dfs(x,y+1,cnt);
}
if(map[x-1][y] == 2) {
dfs(x-1,y,cnt);
}
if(map[x][y-1] == 2) {
dfs(x,y-1,cnt);
}
}
void sort(int cnt) {
for(i=1; i<cnt; i++) {
for(j=1; j<=cnt-i; j++) {
if (a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i=1; i<=cnt; i++) {
if(a[i] != 0)
printf("%d ", a[i]);
}
}
int main() {
int x1,y1,x2,y2;
int x,y;
int cnt = 0;
scanf("%d %d %d", &m, &n, &k);
for(x=1; x<=m; x++) {
for(y=1; y<=n; y++) {
map[x][y] = 2;
}
}
for(i=0; i<k; i++) {
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
for(x=x1+1; x<=x2; x++) {
for(y=y1+1; y<=y2; y++) {
map[y][x] = 1;
}
}
}
for(x=0; x<=m; x++) {
for(y=0; y<=n; y++) {
if(map[x][y] == 2) {
cnt++;
dfs(x,y,cnt);
}
}
}
printf("%d\n", cnt);
sort(cnt);
}
*/
/*
#include <stdio.h>
#define MAXSIZE 101
int n;
int map[MAXSIZE][MAXSIZE] = {};
int arr[MAXSIZE] = {};
int queue[MAXSIZE] = {};
int f = -1;
int r = -1;
void enque(int data) {
queue[++r] = data;
}
int deque() {
return queue[++f];
}
void bfs(int node) {
for(int i=1; i<=n; i++) {
if(map[node][i] == 1 && arr[i] == 0) {
enque(i);
arr[i] = 1;
}
}
while(f != r) {
int x = deque();
for(int i=1; i<=n; i++) {
if(map[x][i] == 1 && arr[i] ==0) {
enque(i);
arr[i] = 1;
map[node][i] = 1;
}
}
}
}
int main() {
int i,j;
int networkNum;
scanf("%d %d", &n, &networkNum);
for(i=0; i<networkNum; i++) {
int x,y;
scanf("%d %d", &x, &y);
map[x][y] = 1;
map[y][x] = 1;
}
// for(i=1; i<=n; i++) {
// for(j=1; j<=n; j++) {
// printf("%d ", map[i][j]);
// }
// printf("\n");
// }
bfs(1);
printf("%d", f);
}
*/
#include <stdio.h>
#define MAXSIZE 1001
int m,n;
int map[MAXSIZE][MAXSIZE] = {};
int visited[MAXSIZE] = {};
int queue[MAXSIZE] = {};
int f = -1;
int r = -1;
void enque(int data) {
queue[++r] = data;
}
int deque() {
return queue[++f];
}
void dfs(int x,int y) {
}
int main() {
int i,j;
scanf("%d %d", &m, &n);
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
scanf("%d", &map[i][j]);
}
}
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
if(map[i][j] == 1) {
bfs(i,j);
}
}
}
}