import java.util.*; import java.io.*; public class Main { static boolean[][] field; 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]); field = new boolean[h + 2][w + 2]; for (int i = 1; i <= h; i++) { String[] line = br.readLine().split(" ", w); for (int j = 1; j <= w; j++) { field[i][j] = line[j - 1].charAt(0) == '1'; } } int count = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (field[i][j]) { setCell(i, j); count++; } } } System.out.println(count); } static void setCell(int hh, int ww) { if (!field[hh][ww]) { return; } field[hh][ww] = false; setCell(hh - 1, ww); setCell(hh + 1, ww); setCell(hh, ww - 1); setCell(hh, ww + 1); } }