Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	results := []int{}
	for true {
		w, h := scanInt(sc), scanInt(sc)
		if w == 0 && h == 0 {
			break
		}
		grid := make([][]int, h)
		for i := 0; i < h; i++ {
			grid[i] = make([]int, w)
			for j := 0; j < w; j++ {
				grid[i][j] = scanInt(sc)
			}
		}
		num := 0
		for i := range loop0(h) {
			for j := range loop0(w) {
				start := pos{y: i, x: j}
				if grid[start.y][start.x] == 0 {
					continue
				}
				num++
				_dfs(grid, w, h, start)
			}
		}
		results = append(results, num)
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func _dfs(grid [][]int, w, h int, start pos) {
	grid[start.y][start.x] = 0
	for _, df := range []pos{
		pos{1, 0}, pos{-1, 0},
		pos{0, -1}, pos{0, 1},
		pos{1, 1}, pos{-1, -1},
		pos{-1, 1}, pos{1, -1},
	} {
		np := addPos(start, df)
		if np.y < 0 || h <= np.y || np.x < 0 || w <= np.x {
			continue
		}
		if grid[np.y][np.x] == 0 {
			continue
		}
		_dfs(grid, w, h, np)
	}
}

type pos struct{ x, y int }

func addPos(p, d pos) pos {
	return pos{
		x: p.x + d.x,
		y: p.y + d.y,
	}
}

func loop0(n int) chan int {
	return loop(0, n-1, 1)
}

func loop1(n int) chan int {
	return loop(1, n, 1)
}

func loop(s, e, d int) chan int {
	ch := make(chan int)
	go func() {
		for i := s; i <= e; i += d {
			ch <- i
		}
		close(ch)
	}()
	return ch
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

def dfs(s,Map,H,W,c):
    dxdy = [(1,0),(-1,0),(0,1),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]
    for i in range(1):
        Map[s[0]][s[1]] = c
        nextPos = (s[0]+dxdy[i][0],s[1]+dxdy[i][1])
        if nextPos[0] >= 0 and nextPos[1] >= 0 and nextPos[0] < H and nextPos[1] < W:
            if Map[nextPos[0]][nextPos[1]] == 1:
                dfs(nextPos,Map,H,W,c)

while True:
    W,H = map(int,input().split(" "))
    if W == 0 and H == 0:
        break
    Map = [list(map(int,input().split())) for i in range(0,H)]
    c = 1
    for i in range(H):
        for j in range(W):
            if Map[i][j] == 1:
                c += 1
                dfs((i,j),Map,H,W,c)
    print(c-1)

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)