Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

Python: import sys

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


def main():
    global w, h

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

        for y in range(h):
            for x in range(w):
                if field[y][x] == 1:
                    dfs(field, x, y)
                    count += 1

        print(count)


def dfs(field, x, y):
    field[y][x] = 0

    for dx_, dy_ in zip(dx, dy):
        next_x = x + dx_
        next_y = y + dy_
        if next_x < 0 or next_y < 0 or next_x >= w or next_y >= h or field[next_y][next_x] == 0:
            continue
        dfs(field, next_x, next_y)


if __name__ == '__main__':
    sys.setrecursionlimit(10000)
    main()

Python: import sys

sys.setrecursionlimit(1000000)

D = [(1,0), (0,1), (-1,0), (0,-1), (-1,-1),(1,-1),(-1,1),(1,1) ] # 下、右、上、左、斜め

def dfs(x,y):
  # Validation
  if not(0<=x<H and 0<=y<W) : return

  # 探索済みか、探索可能かを確認する
  if f[x][y]!=0 or G[x][y] == 0: return

  # Validation完了、探索範囲内、探索可能、ゴール以外＝＞visitフラグを立てる
  f[x][y]=1
 
  # 次の場所に向かう
  for dx,dy in D:
     nx=x+dx
     ny=y+dy
     dfs(nx,ny)
  return

def main():

    cnt=0
    for i in range(H):
        for j in range(W):
            if f[i][j]==0 and G[i][j]: 
                dfs(i, j)
                cnt+=1
    print(cnt)

while True:
    W,H=map(int,input().split())
    if W==0 and H==0: break
    G=[list(map(int,input().split())) for _ in range(H)]
    f=[[0]*W for _ in range(H)]

    main()

Python: import sys
sys.setrecursionlimit(20000)
w,h=list(map(int,input().split()))
def simaserach(i,j):
    if color[i][j]=="w":
        color[i][j]="g"
        if i>=1:
            if sima[i-1][j]==1:
                simaserach(i-1,j)
            if j>=1:
                if sima[i-1][j-1]==1:
                    simaserach(i-1,j-1)
            if j<=w-2:
                if sima[i-1][j+1]==1:
                    simaserach(i-1,j+1)
        if j>=1:
            if sima[i][j-1]==1:
                simaserach(i,j-1)
        if j<=w-2:
            if sima[i][j+1]==1:
                simaserach(i,j+1)
        if i<=h-2:
            if j>=1:
                if sima[i+1][j-1]==1:
                    simaserach(i+1,j-1)
            if sima[i+1][j]==1:
                simaserach(i+1,j)
            if j<=w-2:
                if sima[i+1][j+1]:
                    simaserach(i+1,j+1)

while(w!=0):
    count=0
    color = [["w" for i in range(w)] for j in range(h)]
    sima=[[]for i in range(h)]
    for i in range(h):
        a=list(map(int,input().split()))
        sima[i]=a
    for i in range(h):
        for j in range(w):
            if sima[i][j]==1 and color[i][j]=="w":
                simaserach(i,j)
                count+=1
    print(count)
    w, h = list(map(int, input().split()))

Python: import sys
sys.setrecursionlimit(10**6)
from itertools import product
while True:
  w,h = map(int,input().split())
  if w == h == 0: exit()
  islands = []
  for _ in range(h):
    islands.append(list(map(int,input().split())))

  seen = [ [False]*w for _ in range(h) ]
  def dfs(row,col):
    seen[row][col] = True

    for x,y in product(range(-1,2), repeat=2):
      if x==0 and y==0: continue
      nrow, ncol = row+x, col+y
      if nrow>=h or nrow<0 or ncol>=w or ncol<0: continue
      if seen[nrow][ncol]: continue

      if islands[nrow][ncol] == 1:
        dfs(nrow, ncol)

  res = 0
  for i in range(h):
    for j in range(w):
      if islands[i][j] == 1 and not seen[i][j]:
        dfs(i,j)
        res += 1

  print(res)

Python: import sys
sys.setrecursionlimit(2500)
 
def erase(x, y):
    c[y][x] = '0'
    for dx, dy in ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < w and 0 <= ny < h and c[ny][nx] == '1':
            erase(nx, ny)
 
while True:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    c = [sys.stdin.readline().split() for _ in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] == '1':
                ans += 1
                erase(x, y)
    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
k = [-1,0,1]
def search(x,y,m):
    if x<0 or x>=len(m[0]) or y<0 or y>=len(m): return
    if m[y][x]==0: return
    m[y][x] = 0
    for i in k:
        for j in k:
            search(x+i,y+j,m)

while True:
    c = 0
    w,h = map(int,input().split())
    if w==0: break
    m = [list(map(int,input().split())) for i in range(h)]
    for i in range(h):
        for j in range(w):
            if m[i][j]==1:
                search(j, i, m)
                c += 1
    print(c)

Python: import sys


def dfs(M, i, j, h, w):
    M[i][j] = 0
    for dy in range(-1, 2):
        for dx in range(-1, 2):
            ny, nx = i+dy, j+dx
            if 0 <= ny < h and 0 <= nx < w and M[ny][nx] == 1:
                dfs(M, ny, nx, h, w)


def search(M, w, h):
    count = 0
    for i in range(h):
        for j in range(w):
            if M[i][j] == 1:
                dfs(M, i, j, h, w)
                count += 1
    return count


if __name__ == "__main__":
    sys.setrecursionlimit(50*50)
    ans = []
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break
        M = [[int(ipt) for ipt in input().split()] for _ in range(h)]
        ans.append(search(M, w, h))

    for a in ans:
        print(a)