  The first example of a non-residually finite group in the classes of finitely
presented small-cancelation groups, automatic groups, and CAT(0) groups was
constructed by Wise as the fundamental group of a complete square complex (CSC
for short) with twelve squares. At the same time, Janzen and Wise proved that
CSCs with at most three squares, five or seven squares have residually finite
fundamental group. The smallest open cases were CSCs with four squares and
directed complete VH complexes with six squares. We prove that the CSC with
four squares studied by Janzen and Wise has a non-residually finite fundamental
group. In particular, this gives a non-residually finite CAT(0) group isometric
to $F_2\times F_2$. For the class of complete directed VH complexes, we prove
that there are exactly two complexes with six squares having a non-residually
finite fundamental group. In particular, this positively answers to a question
of Wise on whether the main example from his PhD thesis is non-residually
finite. As a by-product, we get finitely presented torsion-free simple groups
which decompose into an amalgamated free product of free groups
$F_7*_{F_{49}}F_7$.
  Our approach relies on the connection between square complexes and automata
discovered by Glasner and Mozes, where complete VH complexes with one vertex
correspond to bireversible automata. We prove that the square complex
associated to a bireversible automaton with two states or over the binary
alphabet generating an infinite automaton group has a non-residually finite
fundamental group. We describe automaton groups associated to CSCs with four
squares and get two simple automaton representations of the free group $F_2$
and the first automaton representation of the free product $C_3*C_3$.
