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: def solver():
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        return None
    islands = set()
    counter = 2
    c = [[0] + [int(x) for x in input().split()] + [0]  for _ in range(h)]
    c = [[0 for _ in range(w+2)]] + c + [[0 for _ in range(w+2)]]
    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] > 0:
                neighbour = {c[i-1][j-1], c[i-1][j], c[i-1][j+1], c[i][j-1]}
                if max(neighbour) > 1:
                    c[i][j] = max(neighbour)
                    if len(neighbour - {0}) > 1:
                        islands -= {max(neighbour)}
                else:
                    c[i][j] = counter
                    islands |= {counter}
                    counter += 1
    for row in c:
        print(row)
    print(islands)
    return len(islands)

outputs = []
while(True):
    ans = solver()
    if ans == None:
        break
    outputs.append(ans)
print('\n'.join(map(str, outputs)))

Python: move = [[0, 1], [0, -1], [-1, 0], [1, 0], [1, 1], [1, -1], [-1, -1], [-1, 1]]
from collections import deque
def main(w, h):
    c = [list(map(int, input().split())) for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x]:
                ans += 1
                q = deque()
                q.append((x, y))
                while q:
                    xx, yy = q.popleft()
                    for mx, my in move:
                        xm = xx + mx
                        ym = yy + my
                        if 0 <= xm < w and 0 <= ym < h:
                            if c[ym][xm]:
                                c[ym][xm] = 0
                                q.append((xm,ym))
    print(ans)


while 1:
    w, h = map(int, input().split())
    if w == h == 0:
        break
    main(w,h)

Python: import sys

# 再帰の深さの上限変更
sys.setrecursionlimit(10**4)

def DFS(v,visit):
  visit[v] = True
  for u in adj[v]:
    if visit[u] == False:
      DFS(u,visit)

while(1):
  W, H = map(int, input().split())
  if (W,H) == (0,0):# 入力終了
    sys.exit()
  
  C = []
  for _ in range(H):
    tmp = list(map(int, input().split()))
    C.append(tmp)

  # 縦横斜め8方向が島か探索し、隣接リストを作成する
  dx = [1,-1,0,0,1,-1,-1,1]
  dy = [0,0,1,-1,1,1,-1,-1]
  
  # 隣接リスト
  adj = [[] for i in range(H*W)]
  
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1:# 陸である
        # 周りが陸か探索する
        tmp = []
        for k in range(8):
          x,y = j+dx[k], i+dy[k]
          if 0 <= y < H and 0 <= x < W:
            if C[y][x] == 1:
              tmp.append(W*y + x)
        adj[W*i+j] = tmp
  # print(adj)
        
  # 深さ優先探索を行う
  # 訪問チェック
  visit = [False for i in range(H*W)]
  
  ans = 0
  for i in range(H):
    for j in range(W):
      if C[i][j] == 1 and visit[W*i+j] == False:
        DFS(W*i+j,visit)
        ans += 1
  print(ans)

Python: from queue import Queue

def dfs(x,y,c):
    que = Queue()
    que.put((x, y))

    while not que.empty():
        x, y = que.get()
        if c[x][y] == 0:
            continue

        c[x][y] = 0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                sx, sy = x + dx, y + dy
                if sx < 0 or sx >= h or sy < 0 or sy >= w:
                    continue
                que.put((sx, sy))
    return c

while True:
    w,h = map(int,input().split())
    if w == h == 0:
        break
    wh = w * h
    c = [list(map(int,input().split())) for _ in range(h)]
    ans = 0
    for x in range(h):
        for y in range(w):
            if c[x][y] == 0:
                continue
            ans += 1
            c = dfs(x,y,c)
    print(ans)