Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

Python: from sys import stdin, setrecursionlimit
input = stdin.readline

setrecursionlimit(100000)

move = ((-1,-1),(-1,0),(-1,1),(0,1),(0,-1),(1,-1),(1,0),(1,1))
def DFS(x,y):
    if mapmatrix[j][i] == 1 and not checked[y][x]:
        checked[y][x] = True
        
        for dx, dy in move:
            X = x + dx; Y = y + dy
            if X >= 0 and X < W and Y >= 0 and Y < H:
                if mapmatrix[Y][X] == 1:
                    DFS(X,Y)
    
while True:
    W,H = map(int, input().split())
    if W == 0 and H == 0:
        break
    
    mapmatrix = [None]*H
    for i in range(H):
        mapmatrix[i] = list(map(int, input().split()))
    
    checked = [[False]*W for _ in range(H)]
    
    islandcnt = 0
    for j in range(H):
        for i in range(W):
            if mapmatrix[j][i] == 1 and not checked[j][i]:
                DFS(i,j)
                islandcnt += 1
    
    print(islandcnt)

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

def dfs(pos):
  i = pos[0]
  j = pos[1]
  seen[j][i] = True
  for k in range(-1+i,2+i):
    for l in range(-1+j,2+j):
      if 0 <= k < w and 0 <= l < h:
        if field[l][k] == 1 and (not seen[l][k]):
          dfs([k,l])

while True:
  w, h = map(int,input().split())
  if w == 0 and h == 0:
    break
  field = [list(map(int,input().split())) for _ in range(h)]
  seen = [[False]*w for _ in range(h)]
  count = 0
  for i in range(w):
    for j in range(h):
      if field[j][i] == 1 and (not seen[j][i]):
        dfs([i,j])
        count += 1
  print(count)

Python: import sys
sys.setrecursionlimit(100000)

def dfs(i, j):
    g[i][j] = 0

    for x in range(-1, 2):
        for y in range(-1, 2):
            dx = x + i
            dy = y + j

            if 0<=dx<h and 0<=dy<w and g[dx][dy] == 1:
                dfs(dx, dy)
    return

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

    g = [list(map(int, input().split())) for _ in range(h)]

    ans = 0
    for i in range(h):
        for j in range(w):
            if g[i][j] == 1:
                dfs(i, j)
                ans += 1

    print(ans)

Python: m = []

def walk(i,j):
    stack = []
    stack.append((i,j))
    while stack:
        p = stack.pop()
        pi = p[0]
        pj = p[1]
        m[pi][pj] = 0
        for k in range(-1,2):
            for l in range(-1,2):
                a = pi+k
                b = pj+l
                if a < 0 or b < 0 \
                        or a >= len(m) or b >= len(m[a]):
                            pass
                elif m[a][b] == 0:
                    pass
                else:
                    stack.append((a,b))

while True:
    w, h = map(int, input().split())
    if w == 0: break
    for i in range(h):
        m.append(list(map(int, input().split())))
    ans = 0
    for i in range(len(m)):
        for j in range(len(m[i])):
            if m[i][j] == 1:
                walk(i,j)
                ans += 1
    print(ans)
    m = []

Python: import sys
sys.setrecursionlimit(10 ** 6)
islands = []
visited_list = []
w_list = []
h_list = []
while True:
    w ,h = map(int,input().split())
    if w == 0 and h == 0:
        break
    w_list.append(w)
    h_list.append(h)
    island = [list(input().split()) for _ in range(h)]
    islands.append(island)
    visited_list.append([[False] * w for _ in range(h)])

dx = [0,1,1, 1, 0,-1,-1,-1]
dy = [1,1,0,-1,-1,-1, 0, 1]

def dfs(island_m ,now_row , now_column , idx):
    if now_row < 0 or h_list[idx] <= now_row or now_column < 0 or w_list[idx] <= now_column or island_m[now_row][now_column] == "0":
        return
    if visited_list[idx][now_row][now_column]:
        return
    visited_list[idx][now_row][now_column] = True
    for i in range(8):
        dfs(island_m,now_row + dx[i] ,now_column + dy[i],idx)


for m in range(len(islands)):
    cnt = 0
    for row in range(h_list[m]):
        for column in range(w_list[m]):
            if not visited_list[m][row][column] and islands[m][row][column] == "1":
                dfs(islands[m] , row , column , m)
                cnt += 1
    print(cnt)

Python: from collections import deque
from itertools import product
import itertools

def dfs(w, h, graph):
    visited = [[0] * w for _ in range(h)]
    dy_dx = [(0, 0), (0, 1), (0, -1), (1, 1), (1, -1), (1, 0), (-1, 1), (-1, -1), (-1, 0)]
    dots = itertools.product(range(h), range(w))
    count = 0
    for dot in dots:
        dot_y, dot_x = dot
        if graph[dot_y][dot_x] == 0:
            continue
        if visited[dot_y][dot_x] == 1:
            continue

        q = deque()
        q.append((dot_y, dot_x))
        while q:
            move_y, move_x = q.pop()

            for delta in dy_dx:
                dy, dx = delta

                moved_x = move_x + dx
                moved_y = move_y + dy

                if moved_y < 0 or h -1 < moved_y or moved_x < 0 or w -1 < moved_x:
                    continue
                if graph[moved_y][moved_x] == 0:
                    continue
                if visited[moved_y][moved_x] == 0:
                    q.append((moved_y, moved_x))
                    visited[moved_y][moved_x] = 1
        
        count += 1

    return count

if __name__ == "__main__":

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

        graph = [list(map(int, input().split())) for _ in range(h)]
        answer_list.append(dfs(w, h, graph))
    
    for answer in answer_list:
        print(answer)

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

def dfs(h,w,visited,S):
    #print(h,w)
    for i in range(8):
        h0 = h+dh[i]
        w0 = w+dw[i]
        if visited[h0][w0]==False and S[h0][w0]=='1':
            visited[h0][w0]=True
            dfs(h0,w0,visited,S)

ans = []
while True:
    W,H = map(int, input().split())
    if (W,H) == (0,0):
        break
    S = [0]*(H+2)
    S[0] = ['*']*(W+2)
    S[H+1] = ['*']*(W+2)
    for h in range(1,H+1):
        S[h] = ['*'] + input().split() + ['*']

    visited = [[False]*(W+2) for _ in range(H+2)]
    dh = [0,1,1,1,0,-1,-1,-1]
    dw = [1,1,0,-1,-1,-1,0,1]

    cnt = 0
    for h in range(1,H+2):
        for w in range(1,W+2):
            if visited[h][w]==False and S[h][w]=='1':
                visited[h][w]=True
                dfs(h,w,visited,S)
                cnt += 1
    ans.append(cnt)
print(*ans, sep='\n')