#include <bits/stdc++.h>
using namespace std;
const int maxn = 50000;
string s;
char ch;
int ans;
int n, m, x, now[maxn], last[maxn];
int c[maxn << 1], link[maxn << 1], sz[maxn << 1];
int id(int x) { return (x == link[x] ? x : link[x] = id(link[x])); }
void unite(int a, int b) {
  a = id(a);
  b = id(b);
  if (a == b) return;
  --ans;
  link[b] = a;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i <= 2 * m; ++i) link[i] = i;
  for (int k = 0; k < n; ++k) {
    cin >> s;
    for (int j = 0; j < (s).size(); ++j) {
      ch = s[j];
      if ('0' <= ch and ch <= '9')
        x = ch - '0';
      else
        x = ch - 'A' + 10;
      for (int k = 0; k < 4; ++k) now[1 + 4 * j + k] = (x >> (3 - k)) & 1;
    }
    for (int i = 0; i <= 2 * m; ++i) c[i] = false;
    for (int i = 1; i <= m; ++i) c[last[i]] = true;
    vector<int> aux;
    for (int i = 1 + 1; i <= m + 1; ++i) {
      if (!c[i]) {
        aux.push_back(i);
      }
    }
    for (int i = 1; i <= m; ++i) {
      if (now[i] == 0) continue;
      if (now[i - 1]) {
        now[i] = now[i - 1];
      }
      if (last[i]) {
        if (now[i] == 1) {
          now[i] = last[i];
        } else {
          unite(now[i], last[i]);
        }
      }
      if (now[i] == 1) {
        now[i] = aux.back();
        aux.pop_back();
        ++ans;
      }
    }
    for (int i = 1; i <= m; ++i) {
      last[i] = id(now[i]);
    }
    for (int i = 1 + 1; i <= m + 1; ++i) {
      link[i] = i;
    }
  }
  cout << ans << '\n';
  return 0;
}
