#include<stdio.h>

unsigned short room[22][22];

void DFS(unsigned short x, unsigned short y);

int main(void) {

	unsigned short W, H, manx, many;
	unsigned short i, j, count = 0;

	scanf("%hu%hu", &W, &H);
	for(i = 0; i < H + 2; i++) {
		for(j = 0; j < W + 2; j++) {
			if(i == 0 || j == 0 || i == H + 1 || j == W + 1) room[i][j] = 0;
			else scanf("%hu", &room[i][j]);

			if(room[i][j] == 2) {
				manx = i;
				many = j;
				room[i][j] = 1;
			}
		}
	}

	DFS(manx, many);

	for(i = 1; i < H + 1; i++) for(j = 1; j < W + 1; j++) count += (room[i][j] == 2);

	printf("%hd\n", count);
	return 0;
}

void DFS(unsigned short x, unsigned short y) {
	if(room[x][y] != 1) return;
	room[x][y] = 2;
	DFS(x - 1, y);
	DFS(x + 1, y);
	DFS(x, y - 1);
	DFS(x, y + 1);
}