import java.util.*; import java.io.*; public class Main { public static void main (String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int h = Integer.parseInt(first[0]); int w = Integer.parseInt(first[1]); int ww = w + 2; boolean[][] field = new boolean[h + 2][w + 2]; for (int i = 1; i <= h; i++) { char[] arr = br.readLine().toCharArray(); for (int j = 0; j < w; j++) { field[i][j + 1] = arr[j * 2] == '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; if (field[x / ww][x % ww - 1]) { deq.add(x - 1); } if (field[x / ww][x % ww + 1]) { deq.add(x + 1); } if (field[x / ww - 1][x % ww]) { deq.add(x - ww); } if (field[x / ww + 1][x % ww]) { deq.add(x + ww); } } } } } } System.out.println(count); } }