In the end, you have the right form for the first terms, $2(a_1a_2 + a_1a_3 + \cdots + a_1a_m)$ and $2(a_2a_3 + a_2a_4 + \cdots + a_2a_m)$, so I think you understood the pattern. But the last term $2(a_{m-1}a_1 + a_{m-1}a_2 + \cdots + a_{m-1}a_m)$ should simply be $2a_{m-1}a_m$. Otherwise, you are counting the term $a_{m-1}a_1$ too many times (it already appears in $(a_1a_2 + a_1a_3 + \cdots + a_1a_{m-1} + a_1a_m)$).
If you know the $\sum$ symbol for sums, it might be easier and more compact to write it using it. You get
$$\left(\sum_{i=1}^m a_i\right)^2 = \sum_{i=1}^m a_i^2 \ + \ 2\sum_{1 \leq i < j \leq m} a_i a_j.$$
The indexation $1 \leq i < j \leq m$ accounts for the fact that $a_ia_j = a_ja_i$ and thus if $i \neq j$, the term $a_i a_j$ should only appear one time in the summation if a factor $2$ is put before. Two alternative forms are
$$\left(\sum_{i=1}^m a_i\right)^2 = \sum_{1 \leq i, j \leq m}^m a_ia_j = \sum_{i=1}^m a_i^2 \ + \ \sum_{1 \leq i, j \leq m, i \neq j} a_i a_j,$$
which also have their usefulness.