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: w, h = 0, 0

def dfs(x, y, data):
    data[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
            if data[sx][sy] > 0:
                data = dfs(sx, sy, data)
    return data

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

    data = []
    for i in range(h):
        data.append(list(map(int, input().split())))

    ans = 0
    for x in range(h):
        for y in range(w):
            if data[x][y] > 0:
                ans += 1
                data = dfs(x, y, data)
    print (ans)

Python: #!python
# -*- coding: utf-8 -*-

import sys

import numpy as np

sys.setswitchinterval(10**8)


next_s = ((-1, 1), (0, 1), (1, 1),
          (-1, 0), (1, 0),
          (-1, -1), (0, -1), (1, -1))


def DFS(x, y):
    if not (0 <= x < W and 0 <= y < H):
        return
    if not map_[y, x]:
        return

    map_[y, x] = 0

    for dx, dy in next_s:
        DFS(x + dx, y + dy)


while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break

    map_ = np.array([list(map(int, input().split())) for h in range(H)])
    # print(map_)
    island_num = 0
    while np.amax(map_):
        x = np.argmax(map_) % W
        y = np.argmax(map_) // W
        # print(map_)
        # print("x: {} | y: {}".format(x, y))
        DFS(x, y)
        island_num += 1

    print('\t', island_num)

Python: import sys

sys.setrecursionlimit(10**7)

while(True):
    # w, h <= 50
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break

    # 0=>海, 1=>陸
    island = [list(input().split()) for _ in range(h)]

    island_number = 0

    def dfs(y, x):
        if x < 0 or w <= x or y < 0 or h <= y:
            return

        if island[y][x] == "0":
            return

        island[y][x] = "0"

        dfs(y, x - 1)
        dfs(y, x + 1)
        dfs(y - 1, x)
        dfs(y + 1, x)
        dfs(y + 1, x + 1)
        dfs(y + 1, x - 1)
        dfs(y - 1, x + 1)
        dfs(y - 1, x - 1)



    # iは横軸 jは縦軸
    for i in range(w):
        for j in range(h):

            if island[j][i] == "1":
                dfs(j, i)
                island_number += 1

    print(island_number)

Python: from collections import deque

move = [(0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1), (1, 0), (-1, 0)]

def count(chizu, start):
    stack = deque([start])
    while stack:
        y, x = stack.pop()
        for dxi, dyi in move:
            if not (0<=x+dxi<w and 0<=y+dyi<h) or chizu[y+dyi][x+dxi] == "0":
                continue
            else:
                stack.append((y+dyi, x+dxi))
        chizu[y][x] = "0"


while True:
    w, h = map(int,input().split())
    if w == 0:
        break
    
    island = [0]*h
    ans = 0
    for i in range(h):
        island[i] = list(input().split())
    
    for r in range(h):
        for c in range(w):
            if island[r][c] == "1":
                count(island, (r, c))
                ans += 1
    print(ans)

Python: #!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

D = [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]
def solve(w,h):
    def bfs(y,x):
        q = deque([(y,x)])
        while q:
            y,x = q.popleft()
            for dx,dy in D:
                ny,nx = y+dy,x+dx
                if 0 <= ny < h and 0 <= nx < w:
                    if c[ny][nx] and not d[ny][nx]:
                        d[ny][nx] = 1
                        q.append((ny,nx))
    c = LIR(h)
    d = [[0]*w for i in range(h)]
    ans = 0
    for y in range(h):
        for x in range(w):
            if c[y][x] and not d[y][x]:
                ans += 1
                d[y][x] = 1
                bfs(y,x)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    while 1:
        w,h = LI()
        if w == h == 0:
            break
        solve(w,h)

Python: import sys
sys.setrecursionlimit(10**9)
def solve(w,h):
    G = [list(map(int,input().split())) for _ in range(h)]
    seen = [[False]*w for _ in range(h)]
    ans = 0
    def dfs(x,y):
        if seen[y][x]:
            return
        seen[y][x] = True
        for dx in range(-1,2):
            for dy in range(-1,2):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < w and 0 <= ny < h:
                    if G[y][x] == 1:
                        dfs(nx,ny)
    for x in range(w):
        for y in range(h):
            if G[y][x] == 1 and seen[y][x] == False:
                dfs(x,y)
                ans += 1
    print(ans)


while True:
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    solve(w,h)