import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); int ww = w + 2; boolean[][] field = new boolean[h + 2][w + 2]; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { field[i][j] = sc.nextInt() == 1; } } ArrayDeque deq = new ArrayDeque<>(); int count = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (field[i][j]) { count++; deq.add(i * ww + j); while (deq.size() > 0) { int x = deq.poll(); if (field[x / ww][x % ww]) { field[x / ww][x % ww] = false; deq.add(x - 1); deq.add(x + 1); deq.add(x + ww); deq.add(x - ww); } } } } } System.out.println(count); } }