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(); 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); } } boolean[][] used = new boolean[h + 2][w + 2]; int count = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (field[i][j] && !used[i][j]) { count++; setUse(i, j, field, used); } } } System.out.println(count); } static void setUse(int r, int c, boolean[][] field, boolean[][] used) { if (field[r][c] && !used[r][c]) { used[r][c] = true; setUse(r - 1, c, field, used); setUse(r + 1, c, field, used); setUse(r, c - 1, field, used); setUse(r, c + 1, field, used); } } }