Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

Python: import queue

def proceess(start_x,start_y):
    #insland = []
    #insland.append([start_x,start_y])
    new_island = queue.Queue()
    map[start_x][start_y] = 0
    new_island.put([start_x,start_y])
    while True:
        ob = new_island.get()
        x = ob[0]
        y = ob[1]
        #print("x : " + str(x) + ", y : " + str(y))

        for i in [-1,0,1]:
            for j in [-1,0,1]:
                nx = x + i
                ny = y + j

                if nx >= 0 and ny >= 0 and nx < w and ny < h:
                    #print(map[nx][ny])
                    if map[nx][ny] == 1:
                        #insland.append([nx,ny])
                        #print("nx : " + str(nx) + ", ny : " + str(ny))
                        new_island.put([nx,ny])
                        map[nx][ny] = 0

        if new_island.empty():
            break

while True:
    map = []

    tmp = input().split(" ")
    w = int(tmp[0])
    h = int(tmp[1])

    if w == 1 and h == 1:
        break


    for j in range(h):
        nova = []
        arm = input()
        for k in arm.split(" "):
            #k.rstrip('\n')
            nova.append(int(k))
        map.append(nova)

    #print(map)

    count = 0
    for i in range(w):
        for j in range(h):
            if map[i][j] == 1:
                #print("i : " + str(i) + ", j : " + str(j))
                proceess(i,j)
                count += 1

    print(count)

Python: import sys
sys.setrecursionlimit(10000)

def main():
    ans=[]
    while True:
        w,h=map(int,input().split())
        if w==0 and h==0:
            break
        dis=[-1,0,1]
        c=[[0]*(w+2)]
        for i in range(h):
            c.append([0]+list(map(int,input().split()))+[0])
        c.append([0]*(w+2))
        chk=[[0]*(w+2) for _ in range(h+2)]

        def dfs(x,y):
            chk[x][y]=1
            if x<=0 or y<=0 or x>=h+1 or y>=w+1 or c[x][y]==0:
                return
            for dx in dis:
                for dy in dis:
                    if chk[x+dx][y+dy]==0 and c[x+dx][y+dy]==1:
                        dfs(x+dx,y+dy)
            return

        cnt=0
        for i in range(1,h+1):
            for j in range(1,w+1):
                if chk[i][j]==0:
                    chk[i][j]=1
                    if c[i][j]==1:
                        dfs(i,j)
                        cnt+=1
        ans.append(cnt)
    for i in ans:
        print(i)

if __name__ == '__main__':
    main()

Python: from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10**9)

def dfs(y,x):
    for dy,dx in (-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1):
        ny,nx=y+dy,x+dx
        if 0<=ny<h and 0<=nx<w:
            if grid[ny][nx]==1:
                grid[ny][nx]=0
                dfs(ny,nx)

while True:
    readline=stdin.readline
    w,h=map(int,readline().split())
    if w==0 and h==0:
        break
    grid=[list(map(int,readline().split())) for _ in range(h)]

    cnt=0
    for y in range(h):
        for x in range(w):
            if grid[y][x]==1:
                dfs(y,x)
                cnt+=1

    print(cnt)

Python: import sys
sys.setrecursionlimit(10000)
def dfs(x,y):
    for i in range(-1,2):
        if 0<=x+i<a[1]:
            for j in range(-1,2):
                if 0<=y+j<a[0]:
                    if G[x+i][y+j]==1:
                        G[x+i][y+j] = 0
                        dfs(x+i,y+j)
while  True:
    a = list(map(int, input().split()))
    if a[0] == 0:
        break
    G = []
    cnt = 0
    for i in range(a[1]):
        G.append(list(map(int, input().split())))
    for i in range(a[1]):
        for j in range(a[0]):
            if G[i][j] == 1:
                cnt += 1
                dfs(i,j)
    print(cnt)

Python: import sys

sys.setrecursionlimit(10 ** 4)

w = h = 0
islands = []


def dfs(ii: int, jj: int) -> None:
    global islands
    islands[jj][ii] = 0

    for i in range(ii - 1, ii + 2):
        for j in range(jj - 1, jj + 2):
            if 0 <= i < w and 0 <= j < h and islands[j][i] > 0:
                dfs(i, j)


def main() -> None:
    ans = []
    global islands, w, h
    while True:
        cnt = 0
        w, h = map(int, input().split())
        if w + h == 0:
            break
        islands = [list(map(int, input().split())) for _ in range(h)]
        for j in range(h):
            for i in range(w):
                if islands[j][i] > 0:
                    cnt += 1
                    dfs(i, j)
        ans += [cnt]
    print("\n".join(map(str, ans)))


if __name__ == "__main__":
    main()

Python: import sys
sys.setrecursionlimit(10000)


def integrate(x, i, j):
    if x[i-1][j-1] != 1 and x[i-1][j] != 1 and x[i-1][j+1] != 1 \
        and x[i][j-1] != 1 and x[i][j+1] != 1 and x[i+1][j-1] != 1 \
            and x[i+1][j] != 1 and x[i+1][j+1] != 1:
            return 0
    if x[i-1][j] == 1:
        x[i-1][j] = 0
        integrate(x, i-1, j)
    if x[i-1][j-1] == 1:
        x[i-1][j-1] = 0
        integrate(x, i-1, j-1)
    if x[i+1][j] == 1:
        x[i+1][j] = 0
        integrate(x, i+1, j)
    if x[i+1][j+1] == 1:
        x[i+1][j+1] = 0
        integrate(x, i+1, j+1)
    if x[i][j-1] == 1:
        x[i][j-1] = 0
        integrate(x, i, j-1)
    if x[i+1][j-1] == 1:
        x[i+1][j-1] = 0
        integrate(x, i+1, j-1)
    if x[i][j+1] == 1:
        x[i][j+1] = 0
        integrate(x, i, j+1)
    if x[i-1][j+1] == 1:
        x[i-1][j+1] = 0
        integrate(x, i-1, j+1)
    return 0


answer = []

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = [[] for i in range(h+2)]
    c[0] = [2 for i in range(w+2)]

    for i in range(1, h+1):
        c[i].append(2)
        c_i = list(map(int, input().split()))
        c[i].extend(c_i)
        c[i].append(2)

    c[h+1] = [2 for i in range(w+2)]

    count = 0

    for i in range(1, h+1):
        for j in range(1, w+1):
            if c[i][j] == 1:
                c[i][j] = 0
                integrate(c, i, j)
                count += 1

    answer.append(count)

for i in range(len(answer)):
    print(answer[i])