Go: package main

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

var sc = bufio.NewScanner(os.Stdin)
var route [][]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func dfs(h, w int) {
	if route[h][w] == 0 {
		return
	}

	route[h][w] = 0
	for _, x := range []int{-1, 0, 1} {
		for _, y := range []int{-1, 0, 1} {
			if route[h+x][w+y] == 0 {
				continue
			}
			dfs(h+x, w+y)
		}
	}
}

func main() {
	writer := bufio.NewWriter(os.Stdout)
	for {
		w, h := scanInt(), scanInt()
		if w == 0 && h == 0 {
			break
		}

		route = make([][]int, h+2)
		route[0] = make([]int, w+2)
		for i := 1; i <= h; i++ {
			route[i] = make([]int, w+2)
			for j := 1; j <= w; j++ {
				route[i][j] = scanInt()
			}
		}
		route[h+1] = make([]int, w+2)

		total := 0
		for i := 1; i <= h; i++ {
			for j := 1; j <= w; j++ {
				if route[i][j] == 1 {
					total++
					dfs(i, j)
				}
			}
		}
		fmt.Fprintln(writer, total)
	}
	writer.Flush()
}

Python: import sys
sys.setrecursionlimit(200000)
readline = sys.stdin.buffer.readline


def dfs(C, h, w):
    # 一度訪ねた陸地は'0'(海)に置き換える
    C[h][w] = '0'
    for i in range(-1, 2):
        for j in range(-1, 2):
            if 0 <= h+i < len(C) and 0 <= w+j < len(C[0]) and C[h+i][w+j] == '1':
                dfs(C, h+i, w+j)


W, H = 1, 1
while W != 0 or H != 0:
    W, H = map(int, input().split())
    C = []
    for h in range(H):
        c = list(input().split())
        #c = readline().split()
        C.append(c)

    count = 0
    for h in range(H):
        for w in range(W):
            if C[h][w] == '1':
                dfs(C, h, w)
                count += 1
    if W != 0 or H != 0:
        print(count)

Python: import sys
sys.setrecursionlimit(10**6)

# 移動ベクトル
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]

def dfs(h, w):
    seen[h][w] = True
    
    # 次の探索位置を取得
    for dir in range(8):
        nh, nw = h + dx[dir], w + dy[dir]
        if nh < 0 or nh >= H or nw < 0 or nw >= W: continue
        if field[nh][nw] == 0: continue
        if seen[nh][nw] == True: continue
        dfs(nh, nw)
        
while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    field = [[int(i) for i in input().split()] for j in range(H)]

    seen = [[False for i in range(W)] for j in range(H)]

    count = 0
    for h in range(H):
        for w in range(W):
            if seen[h][w] == False and field[h][w] == 1:
                dfs(h, w)
                count += 1

    print(count)

Python: import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7) 

count_list = []

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

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

    move = [-1, +1, -(w + 2) - 1, -(w + 2), -(w + 2) + 1, w + 2 - 1, w + 2, w + 2 + 1]
    visit = set()
    count = 0

    def search(x):
        if x not in visit:
            visit.add(x)
            for dx in move:
                y = x + dx
                if grid[y] == 0:
                    continue
                else:
                    search(y)

    for i in range((w+2) * (h+2)):
        if grid[i] == 0 or i in visit:
            continue
        count += 1
        search(i)
    count_list.append(count)

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

Python: def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [[c for c in input().split()] for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    main()

Python: 
dirs = [(-1,-1),(-1,0),(-1,1), (0,-1),(0,1), (1,-1),(1,0),(1,1)]

while True:
	w, h = map(int, input().split())
	if (w,h) == (0,0):
		break
	tile = [input().split() for i in range(h)]
	count = 0
	for i in range(h):
		for j in range(w):
			if tile[i][j] == '1':
				count += 1
				q = [(i,j)]
				while q:
					(x,y) = q.pop()
					for d in dirs:
						xx = x+d[0]
						yy = y+d[1]
						if (0 <= xx and xx < h) and (0 <= yy and yy < w):
							if tile[xx][yy] == '1':
								tile[xx][yy] = '0'
								q.append((xx,yy))
	print(count)

Python: import sys
sys.setrecursionlimit(2500)
w,h = map(int,input().split())
while not(w==h==0):
    m = [[0]*(w+2)]
    m += [[0]+list(map(int,input().split()))+[0] for i in range(h)]
    m += [[0]*(w+2)]
    g = [[] for i in range(w*h)]
    for i in range(1,h+1):
        for j in range(1,w+1):
            if m[i][j]==1:
                for k in [-1,0,1]:
                    for l in [-1,0,1]:
                        if m[i+k][j+l]==1:
                            g[(i-1)*w+j-1].append((i+k-1)*w+j+l-1)
    reach = [False]*(w*h)
    def dfs(v):
        reach[v] = True
        for i in g[v]:
            if not reach[i]:
                dfs(i)
    ans = 0   
    for i in range(h*w):
        if g[i] and not reach[i]:
            ans += 1
            dfs(i)

    print(ans)

    w,h = map(int,input().split())

Python: from collections import deque
import sys
sys.setrecursionlimit(10**6)
def dfs(x,y):
    c[x][y] = 0
    for dx in range(-1,2):
        for dy in range(-1,2):
            nx = x + dx
            ny = y + dy

            if 0 <= nx  <  h and 0 <= ny  < w and c[nx][ny] == 1:
                dfs(nx,ny)
    
while 1:
    w,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    c = [list(map(int,input().split())) for i in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                cnt += 1
    print(cnt)

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)