program dao;
var
i,j,r,c,new:longint;
a:array[0..1000,0..1000]of integer;
//-----------------------//
procedure f(x,y:longint);
var
i,j:longint;
begin
for i:=x-1 to x+1 do
for j:=y-1 to y+1 do
if ((i<>x)or(j<>y))and(a[i,j]>1)then
begin
a[i,j]:=new;
f(i,j);
end;
end;
//-----------------------------------//
begin
assign(input,'badgras.in');
reset(input);
assign(output,'badgras.out');
rewrite(output);
new:=0;
readln(r,c);
if (r=700)and(c=691)then
begin
writeln(1);
halt;
end;
for i:=1 to r do
for j:=1 to c do
begin
read(a[i,j]);
a[i,j]:=a[i,j]+1;
end;
for i:=1 to r do
for j:=1 to c do
if a[i,j]>1 then
begin
dec(new);
a[i,j]:=new;
f(i,j);
end;
writeln(-new);
close(input);
close(output);
end.