import java.util.*;classMain{static int[][] directions ={{0,1},{1,0},{0,-1},{-1,0}};static boolean[][] visited;public static void main(String[] args){Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] grids = new int[n][m];for(int i =0; i < n; i++){for(int j =0; j < m; j++){grids[i][j]= sc.nextInt();}}int cnt =0;visited = new boolean[n][m];for(int i =0; i < n; i++){for(int j =0; j < m; j++){if(grids[i][j]==1&& !visited[i][j]){cnt++;dfs(grids, i, j);}}}System.out.println(cnt);}private static void dfs(int[][] grids,int x,int y){visited[x][y]= true;for(int i =0; i <4; i++){int nextX = x + directions[i][0];int nextY = y + directions[i][1];if(check(grids, nextX, nextY)){dfs(grids, nextX, nextY);}}}private static boolean check(int[][] grids,int x,int y){int n = grids.length;int m = grids[0].length;return x >=0&& x < n && y >=0&& y < m && !visited[x][y]&& grids[x][y]==1;}}
import java.util.*;// dfs
classMain{public static void main (String[] args){Main main = new Main();Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int[][] island = new int[n][m];for(int i =0; i < n;++i){for(int j =0; j < m;++j){island[i][j]= sc.nextInt();}}int result = main.getIslandNum(island);System.out.println(result);}public int getIslandNum(int[][] island){int numsIsland =0;int row = island.length;int col = island[0].length;for(int i =0; i < row;++i){for(int j =0; j < col;++j){if(island[i][j]==1){++numsIsland;dfs(island, i, j);}}}return numsIsland;}public void dfs(int[][] island,int x,int y){int row = island.length;int col = island[0].length;if(x <0|| y <0|| x >= row || y >= col || island[x][y]==0){return;}island[x][y]=0;// 标记为已经访问dfs(island, x, y +1);dfs(island, x, y -1);dfs(island, x +1, y);dfs(island, x -1, y);}}