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 { int[] h_w = readIntArray(); int h = h_w[0]; int w = h_w[1]; boolean[][] field = new boolean[h][w]; for(int i = 0 ; i < h ; i++){ String[] terrains = br.readLine().split(" "); for(int j = 0 ; j < w ; j++){ if(terrains[j].equals("1")){ field[i][j] = true; } } } int ans = 0; for(int i = 0 ; i < h ; i++){ for(int j = 0 ; j < w ; j++){ if(field[i][j]){ field = bfs(field , i , j , h , w); ans++; } } } System.out.println(ans); } private static boolean[][] bfs(boolean[][] field , int currentH , int currentW , int h , int w){ ArrayDeque todo = new ArrayDeque<>(); //Pair p = new Pair(currentH , currentW); todo.addLast(new Pair(currentH , currentW)); field[currentH][currentW] = false; while(todo.size() > 0){ Pair p = todo.pollFirst(); currentH = p.getY(); currentW = p.getX(); for(int i = 0 ; i < 4 ; i++){ int nextH = currentH + dy[i]; int nextW = currentW + dx[i]; if(nextH >= 0 && nextH < h && nextW >= 0 && nextW < w){ if(field[nextH][nextW]){ todo.addLast(new Pair(nextH , nextW)); field[nextH][nextW] = false; } } } } return field; } private static int[] readIntArray() throws Exception{ int[] intArray = Arrays.stream(br.readLine().split(" ")) .mapToInt(Integer::parseInt).toArray(); return intArray; } } class Pair{ int y , x; public Pair(int y , int x){ this.y = y; this.x = x; } public int getY(){ return y; } public int getX(){ return x; } }