import java.util.*; import java.io.*; public class Main { private static int[] dy = {0,1,0,-1}; private static int[] dx = {1,0,-1,0}; private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { try { int[] h_w = readIntArray(); int h = h_w[0]; int w = h_w[1]; int[][] field = new int[h][w]; for(int i = 0 ; i < h ; i++){ int[] terrains = readIntArray(); field[i] = terrains; } boolean[][] seen = new boolean[h][w]; int ans = 0; for(int i = 0 ; i < h ; i++){ for(int j = 0 ; j < w ; j++){ int currentH = i; int currentW = j; if(seen[currentH][currentW] || field[currentH][currentW] == 0){ continue; } seen = bfs(field , seen , currentH , currentW , h , w); ans++; } } System.out.println(ans); } catch (Exception e) { } } private static boolean[][] bfs(int[][] field , boolean[][] seen , int currentH , int currentW , int h , int w){ LinkedList todo = new LinkedList<>(); int[] h_w = {currentH , currentW}; todo.add(h_w); while(!todo.isEmpty()){ int[] currentH_W = todo.pop(); currentH = currentH_W[0]; currentW = currentH_W[1]; seen[currentH][currentW] = true; for(int i = 0 ; i < 4 ; i++){ int nextH = currentH + dy[i]; int nextW = currentW + dx[i]; if (nextH < 0 || nextW < 0 || nextH >= h || nextW >= w) { continue; } if(seen[nextH][nextW] || field[nextH][nextW] == 0){ continue; } int[] nextH_W = new int[2]; nextH_W[0] = nextH; nextH_W[1] = nextW; todo.add(nextH_W); } } return seen; } private static int[] readIntArray() throws Exception{ int[] intArray = Arrays.stream(br.readLine().split(" ")) .mapToInt(Integer::parseInt).toArray(); return intArray; } }