#include bool f[3002][3002]={0}; int h,w; void check(short i,short j){ if (!f[i][j])return; f[i][j]=false; check(i-1,j); check(i+1,j); check(i,j-1); check(i,j+1); } int main() { scanf("%d %d", &h, &w); for (short i=1;i<=h;i++) { for (short j=1;j<=w;j++) { scanf("%d", &f[i][j]); } } int ans=0; for (short i=1;i<=h;i++) { for (short j=1;j<=w;j++) { if (f[i][j]) { check(i,j); ++ans; } } } printf("%d\n",ans); return 0; }