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(); sc.nextLine(); boolean[][] field = new boolean[h + 2][w + 2]; for (int i = 1; i <= h; i++) { char[] arr = sc.nextLine().toCharArray(); for (int j = 1; j <= w; j++) { field[i][j] = (arr[(j - 1) * 2] == '1'); } } int count = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (field[i][j]) { count++; setUse(i, j, field); } } } System.out.println(count); } static void setUse(int r, int c, boolean[][] field) { if (field[r][c]) { field[r][c] = false; if (field[r - 1][c]) { setUse(r - 1, c, field); } if (field[r + 1][c]) { setUse(r + 1, c, field); } if (field[r][c - 1]) { setUse(r, c - 1, field); } if (field[r][c + 1]) { setUse(r, c + 1, field); } } } }