#include <bits/stdc++.h>
using namespace std;
char s[1000010];
int n, m, ans = 0, f[2000010], g[1000010];
int trans(const char& x) { return isdigit(x) ? x ^ 48 : x - 'A' + 10; }
int getf(int x) {
  if (x < 0 || x > m - 1) return 0;
  return trans(s[x / 4]) >> (3 - x % 4) & 1;
}
int find(const int& x) { return x ^ f[x] ? f[x] = find(f[x]) : x; }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  fx ^ fy ? f[fx] = fy, --ans : 0;
}
int main() {
  scanf("%d%d", &n, &m);
  memset(f, -1, sizeof(f));
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    for (int j = 0; j < m; ++j) g[j] = getf(j);
    for (int j = 0; j < m; ++j) g[j] ? ++ans, f[j + m] = j + m : f[j + m] = -1;
    for (int j = 0; j < m; ++j) {
      if (!g[j]) continue;
      if (~f[j]) merge(j, j + m);
      if (getf(j - 1)) merge(j + m - 1, j + m);
    }
    for (int j = 0; j < m; ++j) f[j] = g[j] ? find(j + m) - m : -1;
  }
  cout << ans;
  return 0;
}
