This is a more formal construction, building off of the answers by Brian M. Scott and David Ongaro.
The theorem is actually: an n x m grid graph is Hamiltonian if and only if:
A) m or n is even and m > 1 and n > 1
or
B) mn = 1
There are four parts to the proof.
Part 1: If either m or n is even, and both m > 1 and n > 1, the graph is Hamiltonian
This proof is going to be by construction.
If one of the even sides is of length 2, you can form a ring that reaches all vertices, so the graph is Hamiltonian.
Otherwise, there exists an even side of length greater than 2. Let's call that direction "vertical" and the other direction "horizontal". Thus, the graph has an even number of rows. Take one of the vertical edges of the graph and make a starting path connecting all squares on that edge. Then, the ends of the starting path can be extended through the rest of the nodes by creating "switchbacks", paths that run out to the edge of the grid opposite the starting path using one horizontal row of squares and then back to the column adjacent to the starting path using the next horizontal row. A complete switchback will reach all of the nodes in its rows that aren't reached by the starting path and requires 2 rows. Since there are an even number of rows in the graph, we can fill the remainder of the graph with switchbacks and end in a square adjacent to the open end of the starting path. Connecting the final switchback to the open end of the starting path creates a Hamiltonian cycle.
Ex:
* - * - * - * - *
| |
* * - * - * - *
| |
* * - * - * - *
| |
* * - * - * - *
| |
* * - * - * - *
| |
* - * - * - * - *
Part 2: If mn = 1, the graph is Hamiltonian
If both mn = 1, then it is a 1 vertex graph. This is trivially Hamiltonian in that there is a zero length path that visits the vertex. [1]
Part 3: If m = 1 xor n = 1, the graph is not Hamiltonian
All Hamiltonian graphs are biconnected. [2]
If exactly one of the dimensions is 1, then the graph is a line of length at least 2. If the length is 2, then it is a simple graph with 2 vertices. There are no simple 2-node Hamiltonian graphs (OEIS A003216), so this is not Hamiltonian.
If the length is greater than 2, there must be a central vertex of the graph that can be removed and the graph will become disconnected. Thus, the graph is not biconnected and is therefore not Hamiltonian.
Part 4: If both m and n are odd and mn != 1, the graph is not Hamiltonian
Any bipartite graph with unbalanced vertex parity is not Hamiltonian. [3][4]
You can color the grid like a chess board, such that alternating squares are black and white and no two adjacent squares share a color. Noting that squares of the grid are vertices of the graph, it follows that the set of black vertices and the set of white vertices are a partition of the graph into 2 sets such that none of the vertices in the same set are adjacent. Thus, the graph is bipartite. Since both of the dimensions are odd, the total number of squares and therefore vertices is odd, so therefore the partitions must have different numbers of vertices. As a result, the graph is bipartite with unbalanced vertices and isn't Hamiltonian.