#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 14) + 1;
const int mod = 1e9 + 7;
int a[2][N], when;
int par[2 * N], sz[2 * N], what[2 * N], last[2 * N];
int find(int x) {
  if (x == par[x]) return x;
  return par[x] = find(par[x]);
}
bool join(int x, int y) {
  x = find(x), y = find(y);
  if (x == y) return 0;
  par[y] = x;
  return 1;
}
char str[N];
int main() {
  int n, m, b = 0, cnt = 0;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str);
    for (int j = 0; j < m / 4; j++) {
      char ch = str[j];
      int val = ('0' <= ch && ch <= '9' ? ch - '0' : ch - 'A' + 10);
      for (int k = 0; k <= 3; k++) {
        int num = val & (1 << (3 - k));
        a[b][j * 4 + k + 1] = (num ? 1 : 0);
        par[j * 4 + k + 1] = (num ? j * 4 + k + 1 : 0);
        cnt += a[b][j * 4 + k + 1];
      }
    }
    for (int j = 1; j <= m; j++) {
      if (a[b][j - 1] && a[b][j]) {
        cnt -= join(j - 1, j);
      }
      if (a[b ^ 1][j] && a[b][j]) {
        cnt -= join(j, m + j);
      }
    }
    for (int j = 1; j <= m; j++) {
      find(j);
    }
    when++;
    for (int j = 1; j <= m; j++) {
      if (last[par[j]] != when) {
        last[par[j]] = when;
        what[par[j]] = j;
      }
    }
    for (int j = 1; j <= m; j++) {
      par[j + m] = what[par[j]] + m;
    }
    b ^= 1;
  }
  printf("%d\n", cnt);
  return 0;
}
