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 numpy
import sys

sys.setrecursionlimit(10**6)

digtmp = [[0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1]]

def dfs(i, j):
    global dfsflag
    if Mapdata[i][j] == 1 and Mapcheck[i][j] == 0:
        Mapcheck[i][j] = 1
        dfsflag = 1
        for tmp in digtmp:
            digh = i + tmp[0]
            digw = j + tmp[1]
            if 0 <= digh < h and 0 <= digw < w:
                dfs(digh, digw)
    else:
        Mapcheck[i][j] = 1

while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    Mapdata = []
    for _ in range(h):
        tmp = list(map(int, input().split()))
        Mapdata.append(tmp)
    Mapcheck = [[0] * w for _ in range(h)]
    count = 0
    for i in range(h):
        for j in range(w):
            dfsflag = 0
            dfs(i, j)
            if dfsflag == 1:
                count += 1
    print(count)

Python: from itertools import product
import sys

sys.setrecursionlimit(1000000)

def s(m, x, y):
    m[y][x] = 0

    for dx, dy in product(range(-1, 2), range(-1, 2)):
        if m[y + dy][x + dx]:
            s(m, x + dx, y + dy)

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

    if w == h == 0:
        break

    m = [[0] * (w + 2)] + [[0] + list(map(int, input().split())) + [0] for _ in range(h)] + [[0] * (w + 2)]

    for x, y in product(range(w + 2), range(h + 2)):
        if m[y][x]:
            c += 1
            s(m, x, y)
            
    print(c)

Python: import sys
sys.setrecursionlimit(10 ** 5)
 
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 = [input().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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

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

while True:
    w, h = na()
    if w == 0 and h == 0:
        break
    g = [na() for _ in range(h)]
    #seen = [[False] * w for _ in range(h)]
    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(g, i, j)
                ans += 1
    print(ans)

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(len(dxdy)):
        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: import sys
sys.setrecursionlimit(10000)
def f(x,y):
    if 0<=x<w and 0<=y<h and a[y][x]=='1':
        a[y][x]='0'
        for dx,dy in[[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[1,-1],[-1,1],[1,1]]:f(x+dx,y+dy)
while 1:
    w,h=map(int,input().split())
    if w==0:break
    a=[input().split() for _ in[0]*h];b=0
    for i in range(w):
        for j in range(h):
            if a[j][i]=='1':
                b+=1;f(i,j)
    print(b)

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

 Map = []
 Map.append(['@']*(w+2))
 land = []
 for i in range(h):
  c = list('@'+input().replace(' ', '')+'@')
  Map.append(c)
  for j in range(w+2):
   if(c[j] == '1'): land.append([i+1,j])
 Map.append(['@']*(w+2))
 #print(land)

 #深さ優先探索
 def dfs(start_x,start_y):
  stack = []
  stack.append([start_x,start_y])
  
  while(1):      
   #stackが空になると終了
   if(stack == []): return
  
   #座標の取り出し
   cord = stack.pop()
   x,y = cord[0],cord[1]
  
   #行き止まりor海なら何もしない
   if((Map[x][y] == '0')or(Map[x][y] == '@')): continue
   #訪問済みなら何もしない
   elif(vis[x][y] == 1): continue
   else:
    vis[x][y] = 1 #訪問済みに

   #次行くところ
   vec_x = [0,0,1,-1,1,-1,1,-1]
   vec_y = [1,-1,0,0,1,-1,-1,1]
   for i in range(8):
     if(Map[x+vec_x[i]][y+vec_y[i]] == '1'):
       stack.append([x+vec_x[i],y+vec_y[i]])    
  
 ans = 0
 vis = [[0]*(w+2) for _ in range(h+2)]
 for i in range(len(land)):
  start_x,start_y = land[i][0],land[i][1]
  if(vis[start_x][start_y] == 0):
      dfs(start_x,start_y)
      ans += 1
      #print('aaa')
 print(ans)

Python: from collections import deque
QUE=deque()


while True:
    w,h=map(int,input().split())
    
    if w==h==0:
        break
    
    MAP=[list(map(int,input().split())) for i in range(h)]
    ANSLIST=[[0 for i in range(w)] for j in range(h)]

    NOW=1

    for i in range(h):
        for j in range(w):
            if MAP[i][j]==0:
                continue
            if ANSLIST[i][j]!=0:
                continue
            
            QUE.append((i,j))
            while QUE:
                i,j=QUE.pop()
                for x,y in [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]:
                    if 0<=x<h and 0<=y<w and MAP[x][y]==1 and ANSLIST[x][y]==0:
                        ANSLIST[x][y]=NOW
                        QUE.append((x,y))

            NOW+=1

    #for an in ANSLIST:
    #    print(an)

    print(NOW-1)