import java.util.*; public class No697{ static int H, W; static int[][] a; static int[] dx = {0, 1, 0, -1}; static int[] dy = {-1, 0, 1, 0}; public static void main(String[] args){ Scanner sc = new Scanner(System.in); H = sc.nextInt(); W = sc.nextInt(); a = new int[H][W]; for(int h = 0; h < H; h++){ for(int w = 0; w < W; w++){ a[h][w] = sc.nextInt(); } } int ans = 0; for(int h = 0; h < H; h++){ for(int w = 0; w < W; w++){ if(a[h][w] == 1){ search(h, w); ans++; } } } System.out.println(ans); } private static void search(int h, int w){ a[h][w] = 0; for(int i = 0; i < 4; i++){ int x = w+dx[i], y = h+dy[i]; if(x >= 0 && x < W && y >= 0 && y < H){ if(a[y][x] == 1){ search(y, x); } } } } }