Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), math.MaxInt32)
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		return []byte(f1())
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()

	for {
		ans, ok := solve()
		if !ok {
			break
		}
		println(ans)
	}

}

func solve() (int, bool) {
	w := readInt()
	h := readInt()
	if w == 0 && h == 0 {
		return 0, false
	}

	c := make([][]int, h+2)
	c[0] = make([]int, w+2)
	c[h+1] = make([]int, w+2)
	for i := 1; i-1 < h; i++ {
		c[i] = make([]int, w+2)
		for j := 1; j-1 < w; j++ {
			c[i][j] = readInt()
		}
	}

	var ans int
	for i := 1; i-1 < h; i++ {
		for j := 1; j-1 < w; j++ {
			if 0 < c[i][j] {
				ans++
				dfs(c, i, j)
			}
		}
	}
	return ans, true
}

func dfs(c [][]int, y, x int) {
	c[y][x] = 0
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{-1, -1, -1, 0, 0, 1, 1, 1}
	for i := range dx {
		nx := x + dx[i]
		ny := y + dy[i]
		if 0 < c[ny][nx] {
			dfs(c, ny, nx)
		}
	}
}

// -----------------------------------------------------------------------------

Python: #template
def inputlist(): return [int(j) for j in input().split()]
#template
dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-1,-1,-1]
from collections import deque
def dfs(x,y,w,h,lis,flag):
    if lis[x][y] == 1:
        flag = 1
        lis[x][y] = 0
        stack = deque([])
        l = [x,y]
        stack.appendleft(l)
        while len(stack) != 0:
            li = stack.popleft()
            xa = li[0]
            ya = li[1]
            for i in range(8):
                nx = xa + dx[i]
                ny = ya + dy[i]
                if nx < 0 or ny < 0 or nx > h-1 or ny > w-1:
                    continue
                if lis[nx][ny] == 0:
                    continue
                stack.appendleft([nx,ny])
                lis[nx][ny] = 0
    if flag:
        return 1
    else:
        return 0

while True:
    w,h = inputlist()
    if w == 0 and h == 0:
        break
    maze = [[0] for _ in range(h)]
    for i in range(h):
        maze[i] = inputlist()
    ans = 0
    for x in range(h):
        for y in range(w):
            ans += dfs(x,y,w,h,maze,0)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)

def bfs(x, y, graph):
  dxy = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
  for dx, dy in dxy:
    nextx = x + dx
    nexty = y + dy
    if (graph[nextx][nexty] == 1):
      graph[nextx][nexty] = 0
      bfs(nextx, nexty, graph)

while(1):
  W, H = map(int, input().split())
  if(H == 0 and W == 0):
    break
    
  graph = [[0] * (W+2) for _ in range(H+2)]
  for i in range(H):
    x = list(map(int, input().split()))
    for j in range(W):
      graph[i+1][j+1] = x[j]
  
  cnt = 0
  for i in range(1,H+1):
    for j in range(1,W+1):
      if(graph[i][j] == 1):
        cnt += 1
        bfs(i, j, graph)
  print(cnt)

Python: from collections import deque

def main(H,W):
  area = [[] for i in range(H)]
  island = []
  
  for _ in range(H):
    h = list(map(int,input().split()))
    area[_] = h
    for i in range(W):
      if h[i] == 1:
        island.append([_,i])
  
  visited = [[0] * W for i in range(H)]
  
  def dfs(area, visited,sh,sw):
    stack = deque([[sh,sw]])
    visited[sh][sw] = 1
    while stack:
      h,w = stack.pop()
      for j in range(-1,2):
        for k in range(-1,2):
          new_h, new_w = h + j, w + k
          if new_h < 0 or new_h >= H or \
            new_w < 0 or new_w >= W:
              continue
          elif area[new_h][new_w] == 1 and \
            visited[new_h][new_w] == 0:
            visited[new_h][new_w] = 1
            stack.append([new_h, new_w])
    return 
  
  ans = 0
  
  for i in island:
    sh,sw = i[0],i[1]
    if visited[sh][sw] == 0:
      dfs(area,visited,sh,sw)
      ans += 1
  
  print(ans)
  
while True:
  W,H = map(int,input().split())
  if H == 0 and W == 0:
    break
  else:
    main(H,W)

Python: import sys
from collections import deque
sys.setrecursionlimit(10**9)

dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,-1,-1,-1,0,1,1,1]
  
def DFS(y,x):
  if table[y][x] == 0:
    return
  table[y][x] = 0
  for i in range(8):
    nx = x + dx[i]
    ny = y + dy[i]
    if 0<=ny<h and 0<=nx<w:
      DFS(ny, nx)
while True:  
  w, h = map(int, input().split())
  if w == 0 and h == 0:
    exit()
  table = []
  ans = 0
  for i in range(h):
    c = list(map(int, input().split()))
    table.append(c)
  for y in range(h):
    for x in range(w):
      if table[y][x] == 1:
        ans += 1
        DFS(y,x)
  print(ans)

Python: import sys
sys.setrecursionlimit(5000)

input = sys.stdin.readline
sea_map = []
chk_map = []
h = 0
w = 0
def chk_island(i,j):
    chk_map[i][j] = False
    if sea_map[i][j] == 0:
        return 0
    for d in ((-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1)):
        if 0 <= i + d[0] < h and 0 <= j + d[1] < w:
            if chk_map[i+d[0]][j+d[1]]:
                chk_island(i+d[0],j+d[1])
    return 1

while True:
    count = 0
    w, h = map(int,input().split())
    if w == 0 and h == 0:
        break
    sea_map = [list(map(int,input().split())) for _ in range(h)]
    chk_map = [[True for _ in range(w)] for _ in range(h)]

    for i in range(h):
        for j in range(w):
            if chk_map[i][j]:
                count += chk_island(i,j)
    print (count)

Python: import sys
sys.setrecursionlimit(10000)

def dfs(x,y):
	for i in range(8):
		nx=x+dx[i]
		ny=y+dy[i]
		if 0<=nx<h and 0<=ny<w and C[nx][ny]==1 and D[nx][ny]==0:
			D[nx][ny]=d
			dfs(nx,ny)

dx=[1,1,0,-1,-1,-1,0,1]
dy=[0,1,1,1,0,-1,-1,-1]

while True:
	w,h=map(int,input().split())
	if w==0 and h==0:
		exit()
	d=0
	C=[list(map(int,input().split())) for i in range(h)]
	D=[[0]*w for i in range(h)]
	for x in range(h):
		for y in range(w):
			if C[x][y]==1 and D[x][y]==0:
				d+=1
				dfs(x,y)
	print(d)