#include <bits/stdc++.h>
using namespace std;
int ni() {
  int val;
  scanf("%i", &val);
  return val;
}
pair<int, int> npi() {
  pair<int, int> val;
  scanf("%i %i", &val.first, &val.second);
  return val;
}
int64_t nll() {
  int64_t val;
  scanf("%I64d", &val);
  return val;
}
vector<int> nvi(int n, int corr = 0) {
  vector<int> a(n);
  for (int i = 0; i < n; ++i) a[i] = ni() + corr;
  return move(a);
}
char nc() {
  char val;
  do {
    val = getchar();
  } while (val == ' ' || val == '\r' || val == '\n');
  return val;
}
char ncs() {
  char val;
  do {
    val = getchar();
  } while (false);
  return val;
}
string ns() {
  static char buff[1024 * 4000];
  scanf("%s", buff);
  return string{buff};
}
int64_t gcd(int64_t a, int64_t b) {
  while (b) {
    auto tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}
int64_t tr2(int xv1, int yv1, int xv2, int yv2, int x3, int y3) {
  return 1LL * (xv2 - xv1) * (y3 - yv1) - 1LL * (yv2 - yv1) * (x3 - xv1);
}
const long double eps = 1e-12;
const long double pi = acos(-1.0);
bool eq(long double a, long double b) { return abs(a - b) <= eps; }
int8_t bits_cnt[256];
const string input_dir = "inputs\\";
string input_file = input_dir + "input.txt";
string output_file = input_dir + "output.txt";
void init_streams() {}
void init_data() {
  for (int i = 1; i <= 255; ++i)
    for (int j = 0; j < 8; ++j)
      if ((1 << j) & i) ++bits_cnt[i];
}
int get_bit_count(int v) {
  return bits_cnt[v & 0xFF] + bits_cnt[(v >> 8) & 0xFF] +
         bits_cnt[(v >> 16) & 0xFF] + bits_cnt[(v >> 24) & 0xFF];
}
int p[1 << 15];
int get(int v) {
  if (p[v] == v) return v;
  p[v] = get(p[v]);
  return p[v];
}
int do_mrg(int u, int v) {
  u = get(u);
  v = get(v);
  if (u == v) return 0;
  p[v] = u;
  return 1;
}
int main() {
  init_streams();
  init_data();
  auto n = ni(), m = ni();
  string pres, curs;
  for (int i = 0; i < m; ++i) pres += '0';
  for (int i = 0; i < 2 * m; ++i) p[i] = i;
  int x = 0, y = 1, ans = 0;
  for (int i = 1; i <= n; ++i, pres = curs) {
    auto rw = ns();
    curs.clear();
    for (int j = 0; j < m / 4; ++j) {
      auto val = rw[j] >= 'A' ? int(10 + rw[j] - 'A') : int(rw[j] - '0');
      for (int k = 8; k; k >>= 1) {
        curs += '0';
        if (k & val) curs.back() = '1';
      }
    }
    swap(x, y);
    for (int j = m - 1; j >= 0; --j)
      if (curs[j] == '1') {
        ++ans;
        if (pres[j] == '1') ans -= do_mrg(2 * j + x, 2 * j + y);
        if (j > 0 && curs[j - 1] == '1')
          ans -= do_mrg(2 * j + x, 2 * j + x - 2);
      }
    for (int j = m - 1; j >= 0; --j) p[2 * j + y] = 2 * j + y;
  }
  printf("%i\n", ans);
  return 0;
}
