code: def grid_2d_graph(m, n, periodic=False, create_using=None):
      """Returns the two-dimensional grid graph.
  
      The grid graph has each node connected to its four nearest neighbors.
  
      Parameters
      ----------
      m, n : int or iterable container of nodes
          If an integer, nodes are from `range(n)`.
          If a container, elements become the coordinate of the nodes.
  
      periodic : bool or iterable
          If `periodic` is True, both dimensions are periodic. If False, none
          are periodic.  If `periodic` is iterable, it should yield 2 bool
          values indicating whether the 1st and 2nd axes, respectively, are
          periodic.
  
      create_using : NetworkX graph constructor, optional (default=nx.Graph)
          Graph type to create. If graph instance, then cleared before populated.
  
      Returns
      -------
      NetworkX graph
          The (possibly periodic) grid graph of the specified dimensions.
  
      """
      G = empty_graph(0, create_using)
      row_name, rows = m
      col_name, cols = n
      G.add_nodes_from((i, j) for i in rows for j in cols)
      G.add_edges_from(
          ((i, j), (pi, j)) for pi, i in pairwise(rows) for j in cols
      )
      G.add_edges_from(
          ((i, j), (i, pj)) for i in rows for pj, j in pairwise(cols)
      )
  
      try:
          periodic_r, periodic_c = periodic
      except TypeError:
          periodic_r = periodic_c = periodic
  
      if periodic_r and len(rows) > 2:
          first = rows[0]
          last = rows[-1]
          G.add_edges_from(((first, j), (last, j)) for j in cols)
      if periodic_c and len(cols) > 2:
          first = cols[0]
          last = cols[-1]
          G.add_edges_from(((i, first), (i, last)) for i in rows)
      # both directions for directed
      if G.is_directed():
          G.add_edges_from((v, u) for u, v in G.edges())
      return G
  
file_path: networkx/networkx/generators/lattice.py
imports: from itertools import repeat
  from math import sqrt
  import networkx
  from networkx.classes import set_node_attributes
  from networkx.exception import NetworkXError
  from networkx.generators.classic import cycle_graph, empty_graph, path_graph
  from networkx.relabel import relabel_nodes
  from networkx.utils import flatten, nodes_or_number, pairwise
label_desc: Use image node_id 1 for calling a global function with example usage: grid_2d_graph(m, n, periodic, create_using) and returns: G