#include <bits/stdc++.h>
using namespace std;

bool a[3000][3000];

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }
    int ans = 0, dd[] = {0, 1, 0, -1, 0};
    function<void(int, int)> f = [&](int x, int y) {
        queue<pair<int, int>> q;
        q.push({x, y});
        while (!q.empty()) {
            int i = q.front().first;
            int j = q.front().second;
            q.pop();
            if (!a[i][j]) {
                continue;
            }
            a[i][j] = false;
            for (int k = 0; k < 4; k++) {
                int ni = i + dd[k], nj = j + dd[k + 1];
                if (0 <= ni && ni < n && 0 <= nj && nj < m && a[ni][nj]) {
                    q.push({ni, nj});
                }
            }
        }
    };
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j]) {
                f(i, j);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}