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]; boolean[][] used = 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); } } 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; if (field[r - 1][c] && !used[r - 1][c]) { setUse(r - 1, c, field, used); } if (field[r + 1][c] && !used[r + 1][c]) { setUse(r + 1, c, field, used); } if (field[r][c - 1] && !used[r][c - 1]) { setUse(r, c - 1, field, used); } if (field[r][c + 1] && !used[r][c + 1]) { setUse(r, c + 1, field, used); } } } }