content: //https://www.acmicpc.net/problem/4963
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.util.*;
  
  public class Main {
      private static int n, m;
      private static int[][] area, chk;
      private static int[] dr = {0, 1, 0, -1, -1, 1, 1, -1};
      private static int[] dc = {1, 0, -1, 0, 1, 1, -1, -1};
  
      public static void main(String[] args) throws IOException {
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          StringTokenizer st = null;
          while (true) {
              int ans = 0;
              st = new StringTokenizer(br.readLine());
              m = Integer.parseInt(st.nextToken());
              n = Integer.parseInt(st.nextToken());
              if (n == 0 && m == 0) break;
              area = new int[n][m];
              chk = new int[n][m];
  
              for (int i = 0; i < n; i++) {
                  st = new StringTokenizer(br.readLine());
                  for (int j = 0; j < m; j++) area[i][j] = Integer.parseInt(st.nextToken());
              }
  
              for (int i = 0; i < n; i++) {
                  for (int j = 0; j < m; j++) {
                      if (area[i][j] == 1 && chk[i][j] == 0) {
                          bfs(i, j);
                          ans++;
                      }
                  }
              }
              System.out.println(ans);
          }
      }
  
      private static void bfs(int sr, int sc) {
          Queue<Node> queue = new LinkedList<>();
          queue.offer(new Node(sr, sc));
          chk[sr][sc] = 1;
          while (!queue.isEmpty()) {
              int r = queue.peek().r;
              int c = queue.peek().c;
              queue.poll();
              for (int i = 0; i < 8; i++) {
                  int nr = r + dr[i];
                  int nc = c + dc[i];
                  if (nr < 0 || nc < 0 || nr >= n || nc >= m || area[nr][nc] == 0 || chk[nr][nc] == 1) continue;
                  chk[nr][nc] = 1;
                  queue.offer(new Node(nr, nc));
              }
          }
      }
  
      private static class Node {
          int r, c;
  
          public Node(int r, int c) {
              this.r = r;
              this.c = c;
          }
      }
  }
  