#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 4096 + 7;
int n, m;
char s[N];
string a;
char str[][5] = {"0000", "0001", "0010", "0011", "0100", "0101",
                 "0110", "0111", "1000", "1001", "1010", "1011",
                 "1100", "1101", "1110", "1111"};
int ans;
int fa[N << 3];
void Init() {
  memset(fa, -1, sizeof(fa));
  ans = 0;
}
string chg(char x) {
  int t;
  if (x - '0' > 9)
    t = x - 'A' + 10;
  else
    t = x - '0';
  return str[t];
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void Union(int x, int y) {
  if (find(x) == find(y)) return;
  --ans;
  fa[find(x)] = find(y);
}
int Solve() {
  for (int i = (0); i < (n); ++i) {
    scanf("%s", s);
    a = "";
    for (int j = (0); j < (m / 4); ++j) a += chg(s[j]);
    for (int j = (0); j < (m); ++j) {
      if (a[j] == '1')
        fa[j + m] = j + m, ++ans;
      else
        fa[j + m] = -1;
    }
    for (int j = (0); j < (m); ++j)
      if (a[j] == '1') {
        if (fa[j] != -1) Union(j, j + m);
        if (j && a[j - 1] == '1') Union(j + m - 1, j + m);
      }
    for (int j = (0); j < (m); ++j) {
      if (a[j] == '1')
        fa[j] = find(j + m) - m;
      else
        fa[j] = -1;
    }
  }
  return printf("%d\n", ans);
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    Init();
    Solve();
  }
  return 0;
}
